Skip to content

Commit

Permalink
Merge pull request #37 from TobyBoyne/feature/problemconfig_rng
Browse files Browse the repository at this point in the history
Add random Generator to ProblemConfig
  • Loading branch information
TobyBoyne authored Jul 2, 2024
2 parents c7737b1 + 548f11f commit 1ae5042
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ env
docs/_build
.coverage
Makefile
make.bat
make.bat

build/
dist/
*.egg-info
2 changes: 1 addition & 1 deletion entmoot/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.4"
__version__ = "2.0.1"
__author__ = "Alexander Thebelt"
__author_email__ = "[email protected]"
__license__ = "BSD 3-Clause License"
9 changes: 4 additions & 5 deletions entmoot/models/enting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from entmoot.models.uncertainty_models.distance_based_uncertainty import (
DistanceBasedUncertainty,
)
from entmoot.utils import sample

from entmoot.models.model_params import EntingParams
from dataclasses import asdict
import numpy as np
from typing import Union

Expand Down Expand Up @@ -151,7 +152,6 @@ def add_to_gurobipy_model(self, core_model, weights: tuple = None) -> None:
from the tree model.
"""
from gurobipy import GRB
from entmoot.utils import sample

# add uncertainty model part
self.unc_model.add_to_gurobipy_model(core_model)
Expand All @@ -172,7 +172,7 @@ def add_to_gurobipy_model(self, core_model, weights: tuple = None) -> None:
if weights is not None:
moo_weights = weights
else:
moo_weights = sample(len(self._problem_config.obj_list), 1)[0]
moo_weights = sample(len(self._problem_config.obj_list), 1, self._problem_config.rng)[0]

for idx, obj in enumerate(self._problem_config.obj_list):
core_model.addConstr(
Expand All @@ -189,7 +189,6 @@ def add_to_pyomo_model(self, core_model, weights: tuple = None) -> None:
from the tree model.
"""
import pyomo.environ as pyo
from entmoot.utils import sample

# add uncertainty model part
self.unc_model.add_to_pyomo_model(core_model)
Expand All @@ -212,7 +211,7 @@ def add_to_pyomo_model(self, core_model, weights: tuple = None) -> None:
if weights is not None:
moo_weights = weights
else:
moo_weights = sample(len(self._problem_config.obj_list), 1)[0]
moo_weights = sample(len(self._problem_config.obj_list), 1, self._problem_config.rng)[0]

objectives_position_name = list(
enumerate([obj.name for obj in self._problem_config.obj_list])
Expand Down
28 changes: 11 additions & 17 deletions entmoot/problem_config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
from typing import Tuple, List
from typing import Tuple, List, Optional
import numpy as np
import random


class ProblemConfig:
def __init__(self, rnd_seed: int = None):
def __init__(self, rnd_seed: Optional[int] = None):
self._feat_list = []
self._obj_list = []
self._rnd_seed = rnd_seed

if rnd_seed is not None:
assert isinstance(
rnd_seed, int
), f"Argument 'rnd_seed' needs to be an integer. Got type '{type(rnd_seed)}'."
np.random.seed(rnd_seed)
random.seed(rnd_seed)
self.rng = np.random.default_rng(rnd_seed)

@property
def cat_idx(self):
Expand Down Expand Up @@ -187,16 +181,16 @@ def get_rnd_sample_numpy(self, num_samples):
for feat in self.feat_list:
if feat.is_real():
array_list.append(
np.random.uniform(low=feat.lb, high=feat.ub, size=num_samples)
self.rng.uniform(low=feat.lb, high=feat.ub, size=num_samples)
)
elif feat.is_cat():
array_list.append(
np.random.randint(len(feat.cat_list), size=num_samples)
self.rng.integers(0, len(feat.cat_list), size=num_samples)
)
elif feat.is_int() or feat.is_bin():
array_list.append(
np.random.random_integers(
low=feat.lb, high=feat.ub, size=num_samples
self.rng.integers(
low=feat.lb, high=feat.ub+1, size=num_samples
)
)
return np.squeeze(np.column_stack(array_list))
Expand All @@ -208,14 +202,14 @@ def get_rnd_sample_list(self, num_samples=1, cat_enc=False):
sample = []
for feat in self.feat_list:
if feat.is_real():
sample.append(random.uniform(feat.lb, feat.ub))
sample.append(self.rng.uniform(feat.lb, feat.ub))
elif feat.is_cat():
if cat_enc:
sample.append(random.randrange(0, len(feat.cat_list)))
sample.append(self.rng.integers(0, len(feat.cat_list)))
else:
sample.append(random.choice(feat.cat_list))
sample.append(self.rng.choice(feat.cat_list))
elif feat.is_int() or feat.is_bin():
sample.append(random.randint(feat.lb, feat.ub))
sample.append(self.rng.integers(feat.lb, feat.ub+1))
sample_list.append(tuple(sample))
return sample_list if len(sample_list) > 1 else sample_list[0]

Expand Down
5 changes: 3 additions & 2 deletions entmoot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def grid(dimension: int, levels: int) -> np.ndarray:
return out / n


def sample(dimension: int, n_samples: int = 1) -> np.ndarray:
def sample(dimension: int, n_samples: int = 1, rng: np.random.Generator = None) -> np.ndarray:
"""Sample uniformly from the unit simplex.
Args:
Expand All @@ -73,5 +73,6 @@ def sample(dimension: int, n_samples: int = 1) -> np.ndarray:
Returns:
array, shape=(n_samples, dimesnion): Random samples from the unit simplex.
"""
s = np.random.standard_exponential((n_samples, dimension))
if rng is None: rng = np.random.default_rng()
s = rng.standard_exponential((n_samples, dimension))
return (s.T / s.sum(axis=1)).T
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
url="https://github.com/cog-imperial/entmoot",
packages=find_packages(exclude=["tests", "docs"]),
install_requires=[
"numpy",
"lightgbm==4.0.0",
"numpy<=2.0.0",
"lightgbm>=4.0.0",
"gurobipy",
"pyomo"
],
setup_requires=[
"numpy",
"lightgbm==4.0.0",
"numpy<=2.0.0",
"lightgbm>=4.0.0",
"gurobipy",
"pyomo"
],
Expand Down

0 comments on commit 1ae5042

Please sign in to comment.