Skip to content

Commit

Permalink
Return dependent variables for x0 by default
Browse files Browse the repository at this point in the history
  • Loading branch information
r.jaepel committed Jan 10, 2024
1 parent fe68404 commit 7014e67
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
25 changes: 19 additions & 6 deletions CADETProcess/optimization/optimizationProblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,7 +2701,8 @@ def compute_negative_log_likelihood(self, x):
return problem

def create_initial_values(
self, n_samples=1, method='random', seed=None, burn_in=100000):
self, n_samples=1, method='random', seed=None, burn_in=100000,
include_dependent_variables=True):
"""Create initial value within parameter space.
Uses hopsy (Highly Optimized toolbox for Polytope Sampling) to retrieve
Expand All @@ -2720,6 +2721,8 @@ def create_initial_values(
Number of samples that are created to ensure uniform sampling.
The actual initial values are then drawn from this set.
The default is 100000.
include_dependent_variables : bool, optional
If True, include dependent variables in population.
Raises
------
Expand Down Expand Up @@ -2781,21 +2784,25 @@ def create_initial_values(
)
values = states[0, ...]

# Because hopsy doesn't know about dependencies, remove dependencies and recompute them later
independent_indices = [
i for i, variable in enumerate(self.variables)
if variable in self.independent_variables
]
independent_values = values[:, independent_indices]

# from this point onward, `values` contains dependent variables
if n_samples == 1 and method == 'chebyshev':
values = independent_values
if include_dependent_variables:
values = self.get_dependent_values(values[0])
else:
values = []
counter = 0
while len(values) < n_samples:
if counter > burn_in:
raise CADETProcessError(
"Cannot find invididuals that fulfill constraints."
"Cannot find individuals that fulfill constraints."
)

counter += 1
Expand All @@ -2809,15 +2816,21 @@ def create_initial_values(
))
)

if not self.check_bounds(ind, get_dependent_values=True):
ind = self.get_dependent_values(ind)

if not self.check_bounds(ind):
continue
if not self.check_linear_constraints(ind, get_dependent_values=True):
if not self.check_linear_constraints(ind):
continue
if not self.check_linear_equality_constraints(ind, get_dependent_values=True):
if not self.check_linear_equality_constraints(ind):
continue

if not include_dependent_variables:
ind = self.get_independent_values(ind)

values.append(ind)

return np.array(values)
return np.array(values, ndmin=2)

@property
def parameters(self):
Expand Down
6 changes: 3 additions & 3 deletions CADETProcess/optimization/pymooAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def run(self, optimization_problem: OptimizationProblem, x0=None):
pop = x0
else:
pop = optimization_problem.create_initial_values(
pop_size, method='chebyshev', seed=self.seed
pop_size, method='chebyshev', seed=self.seed, include_dependent_variables=True
)

pop = np.array(pop, ndmin=2)
Expand All @@ -107,7 +107,7 @@ def run(self, optimization_problem: OptimizationProblem, x0=None):
)
n_remaining = pop_size - len(pop)
remaining = optimization_problem.create_initial_values(
n_remaining, method='chebyshev', seed=self.seed
n_remaining, method='chebyshev', seed=self.seed, include_dependent_variables=True
)
pop = np.vstack((pop, remaining))
elif len(pop) > pop_size:
Expand Down Expand Up @@ -294,7 +294,7 @@ def _do(self, problem, X, **kwargs):
)
):
if X_new is None:
X_new = self.optimization_problem.create_initial_values(len(X))
X_new = self.optimization_problem.create_initial_values(len(X), include_dependent_variables=False)
x_new = X_new[i, :]
X[i, :] = self.optimization_problem.transform(x_new)

Expand Down
3 changes: 2 additions & 1 deletion CADETProcess/optimization/scipyAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def callback_function(x, state=None):
return False

if x0 is None:
x0 = optimization_problem.create_initial_values(1, method='chebyshev')[0]
x0 = optimization_problem.create_initial_values(1, method='chebyshev',
include_dependent_variables=False)[0]

x0_transformed = optimization_problem.transform(x0)

Expand Down
79 changes: 73 additions & 6 deletions tests/test_optimization_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,10 @@ def transform():
variables = self.optimization_problem.variable_names
self.assertEqual(variables_expected, variables)

def test_initial_values(self):
def test_initial_values_without_dependencies(self):
x0_chebyshev_expected = [[0.79289322, 0.20710678, 0.5]]
x0_chebyshev = self.optimization_problem.create_initial_values(
1, method='chebyshev'
1, method='chebyshev', include_dependent_variables=False
)
np.testing.assert_almost_equal(x0_chebyshev, x0_chebyshev_expected)

Expand All @@ -879,12 +879,12 @@ def test_initial_values(self):

x0_seed_1_expected = [[0.7311044, 0.1727515, 0.1822629]]
x0_seed_1 = self.optimization_problem.create_initial_values(
1, method='random', seed=1
1, method='random', seed=1, include_dependent_variables=False
)
np.testing.assert_almost_equal(x0_seed_1, x0_seed_1_expected)

x0_seed_1_random = self.optimization_problem.create_initial_values(
1, method='random'
1, method='random', include_dependent_variables=False
)

with self.assertRaises(AssertionError):
Expand All @@ -906,12 +906,79 @@ def test_initial_values(self):
[0.8359314486227345, 0.39493879515319996, 0.8128182754300088]
]
x0_seed_10 = self.optimization_problem.create_initial_values(
10, method='random', seed=1
10, method='random', seed=1, include_dependent_variables=False
)
np.testing.assert_almost_equal(x0_seed_10, x0_seed_10_expected)

x0_seed_10_random = self.optimization_problem.create_initial_values(
10, method='random'
10, method='random', include_dependent_variables=False
)

with self.assertRaises(AssertionError):
np.testing.assert_almost_equal(x0_seed_10_random, x0_seed_10_expected)

def test_initial_values(self):
x0_chebyshev_expected = [[0.79289322, 0.20710678, 0.2071068, 0.5]]
x0_chebyshev = self.optimization_problem.create_initial_values(
1, method='chebyshev', include_dependent_variables=True
)
np.testing.assert_almost_equal(x0_chebyshev, x0_chebyshev_expected)

variables_expected = [
0.7928932188134523,
0.2071067811865475,
0.2071067811865475,
0.4999999999999999
]
variables = self.optimization_problem.get_dependent_values(
x0_chebyshev[0, [0, 1, 3]]
)
np.testing.assert_almost_equal(variables, variables_expected)

self.assertTrue(
self.optimization_problem.check_linear_constraints(
x0_chebyshev[0, :], get_dependent_values=False
)
)
self.assertTrue(
self.optimization_problem.check_linear_constraints(variables)
)

x0_seed_1_expected = [[0.7311044, 0.1727515, 0.1727515, 0.1822629]]
x0_seed_1 = self.optimization_problem.create_initial_values(
1, method='random', seed=1, include_dependent_variables=True
)
np.testing.assert_almost_equal(x0_seed_1, x0_seed_1_expected)

x0_seed_1_random = self.optimization_problem.create_initial_values(
1, method='random', include_dependent_variables=True
)

with self.assertRaises(AssertionError):
np.testing.assert_almost_equal(x0_seed_1_random, x0_seed_1_expected)

with self.assertRaises(AssertionError):
np.testing.assert_almost_equal(x0_seed_1_random, x0_chebyshev_expected)

x0_seed_10_expected = [
[0.7311043824888657, 0.1727515432673712, 0.1727515432673712, 0.18226293643057073],
[0.9836918383919191, 0.8152389217047241, 0.8152389217047241, 0.8560016844195478],
[0.7358144798470049, 0.2574714423019172, 0.2574714423019172, 0.49387609464567295],
[0.34919171897183954, 0.05751800197656948, 0.05751800197656948, 0.3237260675631758],
[0.9265061673265441, 0.4857572549618687, 0.4857572549618687, 0.8149444448089398],
[0.9065669851023331, 0.1513817591204391, 0.1513817591204391, 0.7710992332649812],
[0.8864554240066591, 0.4771068979697068, 0.4771068979697068, 0.5603893963194555],
[0.6845940550232432, 0.2843172686185149, 0.2843172686185149, 0.6792904559788712],
[0.923735889273789, 0.6890814170651027, 0.6890814170651027, 0.7366940211809302],
[0.8359314486227345, 0.39493879515319996, 0.39493879515319996, 0.8128182754300088]
]
x0_seed_10 = self.optimization_problem.create_initial_values(
10, method='random', seed=1, include_dependent_variables=True
)
np.testing.assert_almost_equal(x0_seed_10, x0_seed_10_expected)

x0_seed_10_random = self.optimization_problem.create_initial_values(
10, method='random', include_dependent_variables=True
)

with self.assertRaises(AssertionError):
Expand Down

0 comments on commit 7014e67

Please sign in to comment.