From 732f27be32b7ff95f2c947b751eeeba8368343b0 Mon Sep 17 00:00:00 2001 From: TobyBoyne Date: Fri, 21 Jun 2024 16:18:14 +0100 Subject: [PATCH 1/2] Add random Generator to ProblemConfig --- entmoot/models/enting.py | 9 ++++----- entmoot/problem_config.py | 28 +++++++++++----------------- entmoot/utils.py | 5 +++-- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/entmoot/models/enting.py b/entmoot/models/enting.py index 3ca07d7..53b7c9f 100644 --- a/entmoot/models/enting.py +++ b/entmoot/models/enting.py @@ -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 @@ -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) @@ -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( @@ -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) @@ -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]) diff --git a/entmoot/problem_config.py b/entmoot/problem_config.py index d240def..c7ed9f8 100644 --- a/entmoot/problem_config.py +++ b/entmoot/problem_config.py @@ -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): @@ -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)) @@ -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] diff --git a/entmoot/utils.py b/entmoot/utils.py index 4039158..c23e925 100644 --- a/entmoot/utils.py +++ b/entmoot/utils.py @@ -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: @@ -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 From 7b18b21b6035b60ababeb4e372b19b508b510bf4 Mon Sep 17 00:00:00 2001 From: TobyBoyne Date: Tue, 2 Jul 2024 12:04:46 +0100 Subject: [PATCH 2/2] Update gitignore for build files --- .gitignore | 6 +++++- entmoot/__version__.py | 2 +- setup.py | 8 ++++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a2603c8..e7bbe5b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,8 @@ env docs/_build .coverage Makefile -make.bat \ No newline at end of file +make.bat + +build/ +dist/ +*.egg-info \ No newline at end of file diff --git a/entmoot/__version__.py b/entmoot/__version__.py index 75ae201..49af14e 100644 --- a/entmoot/__version__.py +++ b/entmoot/__version__.py @@ -1,4 +1,4 @@ -__version__ = "1.0.4" +__version__ = "2.0.1" __author__ = "Alexander Thebelt" __author_email__ = "alexander.thebelt18@imperial.ac.uk" __license__ = "BSD 3-Clause License" diff --git a/setup.py b/setup.py index 8a3107b..6f8d791 100644 --- a/setup.py +++ b/setup.py @@ -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" ],