iwopy 0.1.5__py3-none-any.whl → 0.1.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,36 +2,39 @@ import numpy as np
2
2
  import matplotlib.pyplot as plt
3
3
 
4
4
  from iwopy.core import Optimizer
5
- from .problem import SingleObjProblem, MultiObjProblem
6
- from .imports import IMPORT_OK, check_import, Callback
5
+ from .problem import get_single_obj_class, get_multi_obj_class
7
6
  from .factory import Factory
7
+ from . import imports
8
8
 
9
- if IMPORT_OK:
10
- from pymoo.optimize import minimize
11
-
12
-
13
- class DefaultCallback(Callback):
14
- def __init__(self):
15
- super().__init__()
16
- self.data["f_best"] = None
17
- self.data["cv_best"] = None
18
-
19
- def notify(self, algorithm):
20
- fvals = algorithm.pop.get("F")
21
- cvals = algorithm.pop.get("CV")
22
- n_obj = fvals.shape[1]
23
- n_con = cvals.shape[1]
24
- i = np.argmin(fvals, axis=0)
25
- if self.data["f_best"] is None:
26
- self.data["f_best"] = fvals[None, i, range(n_obj)]
27
- self.data["cv_best"] = cvals[None, i, range(n_con)]
28
- else:
29
- self.data["f_best"] = np.append(
30
- self.data["f_best"], fvals[None, i, range(n_obj)], axis=0
31
- )
32
- self.data["cv_best"] = np.append(
33
- self.data["cv_best"], cvals[None, i, range(n_con)], axis=0
34
- )
9
+
10
+ def get_default_callback_class(verbosity=1):
11
+
12
+ imports.load(verbosity)
13
+
14
+ class DefaultCallback(imports.Callback):
15
+ def __init__(self):
16
+ super().__init__()
17
+ self.data["f_best"] = None
18
+ self.data["cv_best"] = None
19
+
20
+ def notify(self, algorithm):
21
+ fvals = algorithm.pop.get("F")
22
+ cvals = algorithm.pop.get("CV")
23
+ n_obj = fvals.shape[1]
24
+ n_con = cvals.shape[1]
25
+ i = np.argmin(fvals, axis=0)
26
+ if self.data["f_best"] is None:
27
+ self.data["f_best"] = fvals[None, i, range(n_obj)]
28
+ self.data["cv_best"] = cvals[None, i, range(n_con)]
29
+ else:
30
+ self.data["f_best"] = np.append(
31
+ self.data["f_best"], fvals[None, i, range(n_obj)], axis=0
32
+ )
33
+ self.data["cv_best"] = np.append(
34
+ self.data["cv_best"], cvals[None, i, range(n_con)], axis=0
35
+ )
36
+
37
+ return DefaultCallback
35
38
 
36
39
 
37
40
  class Optimizer_pymoo(Optimizer):
@@ -69,8 +72,6 @@ class Optimizer_pymoo(Optimizer):
69
72
  def __init__(self, problem, problem_pars, algo_pars, setup_pars={}, term_pars={}):
70
73
  super().__init__(problem)
71
74
 
72
- check_import()
73
-
74
75
  self.problem_pars = problem_pars
75
76
  self.algo_pars = algo_pars
76
77
  self.setup_pars = setup_pars
@@ -128,9 +129,13 @@ class Optimizer_pymoo(Optimizer):
128
129
 
129
130
  """
130
131
  if self.problem.n_objectives <= 1:
131
- self.pymoo_problem = SingleObjProblem(self.problem, **self.problem_pars)
132
+ self.pymoo_problem = get_single_obj_class(verbosity)(
133
+ self.problem, **self.problem_pars
134
+ )
132
135
  else:
133
- self.pymoo_problem = MultiObjProblem(self.problem, **self.problem_pars)
136
+ self.pymoo_problem = get_multi_obj_class(verbosity)(
137
+ self.problem, **self.problem_pars
138
+ )
134
139
 
135
140
  if verbosity:
136
141
  print("Initializing", type(self).__name__)
@@ -141,7 +146,7 @@ class Optimizer_pymoo(Optimizer):
141
146
 
142
147
  super().initialize(verbosity)
143
148
 
144
- def solve(self, callback=DefaultCallback(), verbosity=1):
149
+ def solve(self, callback="default", verbosity=1):
145
150
  """
146
151
  Run the optimization solver.
147
152
 
@@ -158,13 +163,15 @@ class Optimizer_pymoo(Optimizer):
158
163
  The optimization results object
159
164
 
160
165
  """
166
+ if callback == "default":
167
+ callback = get_default_callback_class(verbosity)()
161
168
 
162
169
  # check problem initialization:
163
170
  super().solve()
164
171
 
165
172
  # run pymoo solver:
166
173
  self.callback = callback
167
- self.results = minimize(
174
+ self.results = imports.minimize(
168
175
  self.pymoo_problem,
169
176
  algorithm=self.algo,
170
177
  termination=self.term,