iwopy 0.1.9__py3-none-any.whl → 0.2__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.

Potentially problematic release.


This version of iwopy might be problematic. Click here for more details.

Files changed (43) hide show
  1. iwopy/VERSION +1 -1
  2. iwopy/__init__.py +6 -1
  3. iwopy/benchmarks/branin/__init__.py +1 -0
  4. iwopy/benchmarks/{branin.py → branin/branin.py} +29 -19
  5. iwopy/benchmarks/rosenbrock/__init__.py +1 -0
  6. iwopy/benchmarks/{rosenbrock.py → rosenbrock/rosenbrock.py} +35 -27
  7. iwopy/core/base.py +14 -8
  8. iwopy/core/constraint.py +20 -14
  9. iwopy/core/function.py +66 -60
  10. iwopy/core/function_list.py +51 -45
  11. iwopy/core/function_subset.py +33 -28
  12. iwopy/core/memory.py +43 -35
  13. iwopy/core/objective.py +4 -1
  14. iwopy/core/opt_results.py +79 -68
  15. iwopy/core/optimizer.py +15 -9
  16. iwopy/core/problem.py +116 -104
  17. iwopy/interfaces/pygmo/__init__.py +3 -0
  18. iwopy/interfaces/pygmo/algos.py +5 -2
  19. iwopy/interfaces/pygmo/imports.py +11 -0
  20. iwopy/interfaces/pygmo/optimizer.py +24 -18
  21. iwopy/interfaces/pygmo/problem.py +24 -19
  22. iwopy/interfaces/pymoo/__init__.py +4 -1
  23. iwopy/interfaces/pymoo/factory.py +6 -0
  24. iwopy/interfaces/pymoo/imports.py +11 -0
  25. iwopy/interfaces/pymoo/optimizer.py +75 -48
  26. iwopy/interfaces/pymoo/problem.py +330 -314
  27. iwopy/interfaces/scipy/optimizer.py +26 -20
  28. iwopy/optimizers/gg.py +41 -35
  29. iwopy/utils/discretization.py +106 -100
  30. iwopy/utils/stdout.py +2 -0
  31. iwopy/wrappers/discretize_reg_grid.py +65 -59
  32. iwopy/wrappers/local_fd.py +40 -34
  33. iwopy/wrappers/problem_wrapper.py +43 -37
  34. iwopy/wrappers/simple_constraint.py +47 -41
  35. iwopy/wrappers/simple_objective.py +42 -36
  36. iwopy/wrappers/simple_problem.py +40 -34
  37. {iwopy-0.1.9.dist-info → iwopy-0.2.dist-info}/METADATA +12 -3
  38. iwopy-0.2.dist-info/RECORD +50 -0
  39. iwopy-0.1.9.dist-info/RECORD +0 -48
  40. {iwopy-0.1.9.dist-info → iwopy-0.2.dist-info}/LICENSE +0 -0
  41. {iwopy-0.1.9.dist-info → iwopy-0.2.dist-info}/WHEEL +0 -0
  42. {iwopy-0.1.9.dist-info → iwopy-0.2.dist-info}/top_level.txt +0 -0
  43. {iwopy-0.1.9.dist-info → iwopy-0.2.dist-info}/zip-safe +0 -0
@@ -4,384 +4,400 @@ from iwopy.core import SingleObjOptResults, MultiObjOptResults
4
4
  from . import imports
5
5
 
6
6
 
7
- def get_single_obj_class(verbosity=1):
8
-
9
- imports.load(verbosity)
10
-
11
- class SingleObjProblem(imports.Problem):
7
+ class SingleObjProblemTemplate:
8
+ """
9
+ Template for a wrapper around the pymoo problem
10
+ for a single objective.
11
+
12
+ At the moment this interface only supports
13
+ pure int or pure float problems (not mixed).
14
+
15
+ Attributes
16
+ ----------
17
+ problem: iwopy.core.Problem
18
+ The iwopy problem to solve
19
+ vectorize: bool
20
+ Switch for vectorized calculations, wrt
21
+ population individuals
22
+ is_mixed: bool
23
+ Flag for mixed integer/float problems
24
+ is_intprob: bool
25
+ Flag for integer problems
26
+
27
+ :group: interfaces.pymoo
28
+
29
+ """
30
+
31
+ def __init__(self, problem, vectorize):
12
32
  """
13
- Wrapper around the pymoo problem for a single
14
- objective.
15
-
16
- At the moment this interface only supports
17
- pure int or pure float problems (not mixed).
33
+ Constructor
18
34
 
19
35
  Parameters
20
36
  ----------
21
- problem : iwopy.core.Problem
37
+ problem: iwopy.core.Problem
22
38
  The iwopy problem to solve
23
- vectorize : bool, optional
39
+ vectorize: bool, optional
24
40
  Switch for vectorized calculations, wrt
25
41
  population individuals
26
42
 
27
- Attributes
28
- ----------
29
- problem : iwopy.core.Problem
30
- The iwopy problem to solve
31
- vectorize : bool
32
- Switch for vectorized calculations, wrt
33
- population individuals
34
- is_mixed : bool
35
- Flag for mixed integer/float problems
36
- is_intprob : bool
37
- Flag for integer problems
38
-
39
43
  """
44
+ self.problem = problem
45
+ self.vectorize = vectorize
46
+
47
+ if self.problem.n_vars_float > 0 and self.problem.n_vars_int == 0:
48
+
49
+ self.is_mixed = False
50
+ self.is_intprob = False
51
+
52
+ self._pargs = dict(
53
+ n_var=self.problem.n_vars_float,
54
+ n_obj=self.problem.n_objectives,
55
+ n_ieq_constr=self.problem.n_constraints,
56
+ xl=self.problem.min_values_float(),
57
+ xu=self.problem.max_values_float(),
58
+ elementwise=not vectorize,
59
+ type_var=np.float64,
60
+ )
61
+
62
+ elif self.problem.n_vars_float == 0 and self.problem.n_vars_int > 0:
63
+
64
+ self.is_mixed = False
65
+ self.is_intprob = True
66
+
67
+ self._pargs = dict(
68
+ n_var=self.problem.n_vars_int,
69
+ n_obj=self.problem.n_objectives,
70
+ n_ieq_constr=self.problem.n_constraints,
71
+ xl=self.problem.min_values_int(),
72
+ xu=self.problem.max_values_int(),
73
+ elementwise=not vectorize,
74
+ type_var=np.int32,
75
+ )
76
+
77
+ else:
78
+
79
+ self.is_mixed = True
80
+ self.is_intprob = False
81
+
82
+ vars = {}
83
+
84
+ nami = self.problem.var_names_int()
85
+ inii = self.problem.initial_values_int()
86
+ mini = self.problem.min_values_int()
87
+ maxi = self.problem.max_values_int()
88
+ for i, v in enumerate(nami):
89
+ vars[v] = imports.Integer(value=inii[i], bounds=(mini[i], maxi[i]))
90
+
91
+ namf = self.problem.var_names_float()
92
+ inif = self.problem.initial_values_float()
93
+ minf = self.problem.min_values_float()
94
+ maxf = self.problem.max_values_float()
95
+ for i, v in enumerate(namf):
96
+ vars[v] = imports.Real(value=inif[i], bounds=(minf[i], maxf[i]))
97
+
98
+ self._pargs = dict(
99
+ vars=vars,
100
+ n_obj=self.problem.n_objectives,
101
+ n_ieq_constr=self.problem.n_constraints,
102
+ elementwise=not vectorize,
103
+ )
104
+
105
+ if self.problem.n_constraints:
106
+
107
+ self._cmi = self.problem.min_values_constraints
108
+ self._cma = self.problem.max_values_constraints
109
+ cnames = self.problem.cons.component_names
110
+
111
+ sel = np.isinf(self._cmi) & np.isinf(self._cma)
112
+ if np.any(sel):
113
+ raise RuntimeError(f"Missing boundaries for constraints {cnames[sel]}")
114
+
115
+ sel = (~np.isinf(self._cmi)) & (~np.isinf(self._cma))
116
+ if np.any(sel):
117
+ raise RuntimeError(
118
+ f"Constraints {cnames[sel]} have both lower and upper bounds"
119
+ )
40
120
 
41
- def __init__(self, problem, vectorize):
42
-
43
- self.problem = problem
44
- self.vectorize = vectorize
45
-
46
- if self.problem.n_vars_float > 0 and self.problem.n_vars_int == 0:
121
+ def _evaluate(self, x, out, *args, **kwargs):
122
+ """
123
+ Overloading the abstract evaluation function
124
+ of the pymoo base class.
125
+ """
47
126
 
48
- self.is_mixed = False
49
- self.is_intprob = False
127
+ # vectorized run:
128
+ if self.vectorize:
50
129
 
51
- super().__init__(
52
- n_var=self.problem.n_vars_float,
53
- n_obj=self.problem.n_objectives,
54
- n_ieq_constr=self.problem.n_constraints,
55
- xl=self.problem.min_values_float(),
56
- xu=self.problem.max_values_float(),
57
- elementwise=not vectorize,
58
- type_var=np.float64,
130
+ if self.is_mixed:
131
+ xi = np.array(
132
+ [[dct[v] for v in self.problem.var_names_int()] for dct in x],
133
+ dtype=np.int32,
59
134
  )
135
+ xf = np.array(
136
+ [[dct[v] for v in self.problem.var_names_float()] for dct in x],
137
+ dtype=np.float64,
138
+ )
139
+ out["F"], out["G"] = self.problem.evaluate_population(xi, xf)
140
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
141
+ else:
142
+ n_pop = x.shape[0]
143
+ if self.is_intprob:
144
+ dummies = np.zeros((n_pop, 0), dtype=np.float64)
145
+ out["F"], out["G"] = self.problem.evaluate_population(x, dummies)
146
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
147
+ else:
148
+ dummies = np.zeros((n_pop, 0), dtype=np.int32)
149
+ out["F"], out["G"] = self.problem.evaluate_population(dummies, x)
150
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
60
151
 
61
- elif self.problem.n_vars_float == 0 and self.problem.n_vars_int > 0:
152
+ if self.problem.n_constraints:
62
153
 
63
- self.is_mixed = False
64
- self.is_intprob = True
154
+ sel = ~np.isinf(self._cma)
155
+ out["G"][:, sel] = out["G"][:, sel] - self._cma[None, sel]
65
156
 
66
- super().__init__(
67
- n_var=self.problem.n_vars_int,
68
- n_obj=self.problem.n_objectives,
69
- n_ieq_constr=self.problem.n_constraints,
70
- xl=self.problem.min_values_int(),
71
- xu=self.problem.max_values_int(),
72
- elementwise=not vectorize,
73
- type_var=np.int32,
74
- )
157
+ sel = ~np.isinf(self._cmi)
158
+ out["G"][:, sel] = self._cmi[None, sel] - out["G"][:, sel]
75
159
 
76
- else:
160
+ # individual run:
161
+ else:
77
162
 
78
- self.is_mixed = True
79
- self.is_intprob = False
80
-
81
- vars = {}
82
-
83
- nami = self.problem.var_names_int()
84
- inii = self.problem.initial_values_int()
85
- mini = self.problem.min_values_int()
86
- maxi = self.problem.max_values_int()
87
- for i, v in enumerate(nami):
88
- vars[v] = imports.Integer(value=inii[i], bounds=(mini[i], maxi[i]))
89
-
90
- namf = self.problem.var_names_float()
91
- inif = self.problem.initial_values_float()
92
- minf = self.problem.min_values_float()
93
- maxf = self.problem.max_values_float()
94
- for i, v in enumerate(namf):
95
- vars[v] = imports.Real(value=inif[i], bounds=(minf[i], maxf[i]))
96
-
97
- super().__init__(
98
- vars=vars,
99
- n_obj=self.problem.n_objectives,
100
- n_ieq_constr=self.problem.n_constraints,
101
- elementwise=not vectorize,
163
+ if self.is_mixed:
164
+ xi = np.array(
165
+ [x[v] for v in self.problem.var_names_int()], dtype=np.int32
102
166
  )
167
+ xf = np.array(
168
+ [x[v] for v in self.problem.var_names_float()], dtype=np.float64
169
+ )
170
+ out["F"], out["G"] = self.problem.evaluate_individual(xi, xf)
171
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
172
+ else:
173
+ n_pop = x.shape[0]
174
+ if self.is_intprob:
175
+ dummies = np.zeros(0, dtype=np.float64)
176
+ out["F"], out["G"] = self.problem.evaluate_individual(x, dummies)
177
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
178
+ else:
179
+ dummies = np.zeros(0, dtype=np.int32)
180
+ out["F"], out["G"] = self.problem.evaluate_individual(dummies, x)
181
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
103
182
 
104
183
  if self.problem.n_constraints:
105
184
 
106
- self._cmi = self.problem.min_values_constraints
107
- self._cma = self.problem.max_values_constraints
108
- cnames = self.problem.cons.component_names
185
+ sel = ~np.isinf(self._cma)
186
+ out["G"][sel] = out["G"][sel] - self._cma[sel]
109
187
 
110
- sel = np.isinf(self._cmi) & np.isinf(self._cma)
111
- if np.any(sel):
112
- raise RuntimeError(
113
- f"Missing boundaries for constraints {cnames[sel]}"
114
- )
188
+ sel = ~np.isinf(self._cmi)
189
+ out["G"][sel] = self._cmi[sel] - out["G"][sel]
115
190
 
116
- sel = (~np.isinf(self._cmi)) & (~np.isinf(self._cma))
117
- if np.any(sel):
118
- raise RuntimeError(
119
- f"Constraints {cnames[sel]} have both lower and upper bounds"
120
- )
191
+ def finalize(self, pymoo_results, verbosity=1):
192
+ """
193
+ Finalize the problem.
121
194
 
122
- def _evaluate(self, x, out, *args, **kwargs):
123
- """
124
- Overloading the abstract evaluation function
125
- of the pymoo base class.
126
- """
195
+ Parameters
196
+ ----------
197
+ pymoo_results: pymoo.Results
198
+ The results from the solver
199
+ verbosity: int
200
+ The verbosity level, 0 = silent
201
+
202
+ Returns
203
+ -------
204
+ results: iwopy.SingleObjOptResults
205
+ The optimization results object
206
+
207
+ """
208
+
209
+ # prepare:
210
+ r = pymoo_results
211
+ suc = True
212
+
213
+ # case no solution from pymoo:
214
+ if r.X is None:
215
+ suc = False
216
+ xi = None
217
+ xf = None
218
+ res = None
219
+ objs = None
220
+ cons = None
221
+
222
+ # evaluate pymoo final solution:
223
+ else:
224
+ if self.is_mixed:
225
+ xi = np.array(
226
+ [r.X[v] for v in self.problem.var_names_int()], dtype=np.int32
227
+ )
228
+ xf = np.array(
229
+ [r.X[v] for v in self.problem.var_names_float()],
230
+ dtype=np.float64,
231
+ )
232
+ else:
233
+ if self.is_intprob:
234
+ xi = r.X
235
+ xf = np.zeros(0, dtype=np.float64)
236
+ else:
237
+ xi = np.zeros(0, dtype=np.int32)
238
+ xf = np.array(r.X, dtype=np.float64)
127
239
 
128
- # vectorized run:
129
240
  if self.vectorize:
130
241
 
131
242
  if self.is_mixed:
132
- xi = np.array(
133
- [[dct[v] for v in self.problem.var_names_int()] for dct in x],
243
+ pxi = np.array(
244
+ [[p.X[v] for v in self.problem.var_names_int()] for p in r.pop],
134
245
  dtype=np.int32,
135
246
  )
136
- xf = np.array(
137
- [[dct[v] for v in self.problem.var_names_float()] for dct in x],
247
+ pxf = np.array(
248
+ [
249
+ [p.X[v] for v in self.problem.var_names_float()]
250
+ for p in r.pop
251
+ ],
138
252
  dtype=np.float64,
139
253
  )
140
- out["F"], out["G"] = self.problem.evaluate_population(xi, xf)
141
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
142
- else:
143
- n_pop = x.shape[0]
144
- if self.is_intprob:
145
- dummies = np.zeros((n_pop, 0), dtype=np.float64)
146
- out["F"], out["G"] = self.problem.evaluate_population(
147
- x, dummies
148
- )
149
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[
150
- None, :
151
- ]
152
- else:
153
- dummies = np.zeros((n_pop, 0), dtype=np.int32)
154
- out["F"], out["G"] = self.problem.evaluate_population(
155
- dummies, x
156
- )
157
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[
158
- None, :
159
- ]
160
-
161
- if self.problem.n_constraints:
162
-
163
- sel = ~np.isinf(self._cma)
164
- out["G"][:, sel] = out["G"][:, sel] - self._cma[None, sel]
165
-
166
- sel = ~np.isinf(self._cmi)
167
- out["G"][:, sel] = self._cmi[None, sel] - out["G"][:, sel]
254
+ self.problem.finalize_population(pxi, pxf, verbosity)
255
+ del pxi, pxf
168
256
 
169
- # individual run:
170
- else:
171
-
172
- if self.is_mixed:
173
- xi = np.array(
174
- [x[v] for v in self.problem.var_names_int()], dtype=np.int32
175
- )
176
- xf = np.array(
177
- [x[v] for v in self.problem.var_names_float()], dtype=np.float64
178
- )
179
- out["F"], out["G"] = self.problem.evaluate_individual(xi, xf)
180
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
181
257
  else:
182
- n_pop = x.shape[0]
258
+ n_pop = len(r.pop)
259
+ n_vars = len(r.X)
260
+ vars = np.zeros((n_pop, n_vars), dtype=np.float64)
261
+ for pi, p in enumerate(r.pop):
262
+ vars[pi] = p.X
183
263
  if self.is_intprob:
184
- dummies = np.zeros(0, dtype=np.float64)
185
- out["F"], out["G"] = self.problem.evaluate_individual(
186
- x, dummies
264
+ dummies = np.zeros((n_pop, 0), dtype=np.int32)
265
+ self.problem.finalize_population(
266
+ vars.astype(np.int32), dummies, verbosity
187
267
  )
188
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
189
268
  else:
190
- dummies = np.zeros(0, dtype=np.int32)
191
- out["F"], out["G"] = self.problem.evaluate_individual(
192
- dummies, x
193
- )
194
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
269
+ dummies = np.zeros((n_pop, 0), dtype=np.float64)
270
+ self.problem.finalize_population(dummies, vars, verbosity)
195
271
 
196
- if self.problem.n_constraints:
272
+ res, objs, cons = self.problem.finalize_individual(xi, xf, verbosity)
197
273
 
198
- sel = ~np.isinf(self._cma)
199
- out["G"][sel] = out["G"][sel] - self._cma[sel]
274
+ if verbosity:
275
+ print()
276
+ suc = np.all(self.problem.check_constraints_individual(cons, False))
277
+ if verbosity:
278
+ print()
200
279
 
201
- sel = ~np.isinf(self._cmi)
202
- out["G"][sel] = self._cmi[sel] - out["G"][sel]
280
+ return SingleObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
203
281
 
204
- def finalize(self, pymoo_results, verbosity=1):
205
- """
206
- Finalize the problem.
282
+ @classmethod
283
+ def get_class(cls):
284
+ """
285
+ Creates the class, dynamically derived from pymoo.Problem
286
+ """
287
+ imports.load()
288
+ attrb = {v: d for v, d in cls.__dict__.items()}
289
+ init0 = cls.__init__
207
290
 
208
- Parameters
209
- ----------
210
- pymoo_results: pymoo.Results
211
- The results from the solver
212
- verbosity : int
213
- The verbosity level, 0 = silent
291
+ def init1(self, *args, **kwargs):
292
+ init0(self, *args, **kwargs)
293
+ imports.Problem.__init__(self, **self._pargs)
214
294
 
215
- Returns
216
- -------
217
- results: iwopy.SingleObjOptResults
218
- The optimization results object
295
+ attrb["__init__"] = init1
296
+ attrb["__doc__"] = attrb["__doc__"].replace("Template for a w", "W")
297
+ del attrb["get_class"]
298
+ return type("SingleObjProblem", (imports.Problem,), attrb)
219
299
 
220
- """
221
300
 
222
- # prepare:
223
- r = pymoo_results
224
- suc = True
301
+ class MultiObjProblemTemplate:
302
+ """
303
+ Template for a wrapper around the pymoo problem
304
+ for a multiple objectives problem.
225
305
 
226
- # case no solution from pymoo:
227
- if r.X is None:
228
- suc = False
229
- xi = None
230
- xf = None
231
- res = None
232
- objs = None
233
- cons = None
306
+ At the moment this interface only supports
307
+ pure int or pure float problems (not mixed).
234
308
 
235
- # evaluate pymoo final solution:
236
- else:
237
- if self.is_mixed:
238
- xi = np.array(
239
- [r.X[v] for v in self.problem.var_names_int()], dtype=np.int32
240
- )
241
- xf = np.array(
242
- [r.X[v] for v in self.problem.var_names_float()],
243
- dtype=np.float64,
244
- )
245
- else:
246
- if self.is_intprob:
247
- xi = r.X
248
- xf = np.zeros(0, dtype=np.float64)
249
- else:
250
- xi = np.zeros(0, dtype=np.int32)
251
- xf = np.array(r.X, dtype=np.float64)
252
-
253
- if self.vectorize:
254
-
255
- if self.is_mixed:
256
- pxi = np.array(
257
- [
258
- [p.X[v] for v in self.problem.var_names_int()]
259
- for p in r.pop
260
- ],
261
- dtype=np.int32,
262
- )
263
- pxf = np.array(
264
- [
265
- [p.X[v] for v in self.problem.var_names_float()]
266
- for p in r.pop
267
- ],
268
- dtype=np.float64,
269
- )
270
- self.problem.finalize_population(pxi, pxf, verbosity)
271
- del pxi, pxf
272
-
273
- else:
274
- n_pop = len(r.pop)
275
- n_vars = len(r.X)
276
- vars = np.zeros((n_pop, n_vars), dtype=np.float64)
277
- for pi, p in enumerate(r.pop):
278
- vars[pi] = p.X
279
- if self.is_intprob:
280
- dummies = np.zeros((n_pop, 0), dtype=np.int32)
281
- self.problem.finalize_population(
282
- vars.astype(np.int32), dummies, verbosity
283
- )
284
- else:
285
- dummies = np.zeros((n_pop, 0), dtype=np.float64)
286
- self.problem.finalize_population(dummies, vars, verbosity)
287
-
288
- res, objs, cons = self.problem.finalize_individual(xi, xf, verbosity)
289
-
290
- if verbosity:
291
- print()
292
- suc = np.all(self.problem.check_constraints_individual(cons, False))
293
- if verbosity:
294
- print()
295
-
296
- return SingleObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
297
-
298
- return SingleObjProblem
309
+ Attributes
310
+ ----------
311
+ problem: iwopy.core.Problem
312
+ The iwopy problem to solve
313
+ vectorize: bool
314
+ Switch for vectorized calculations, wrt
315
+ population individuals
316
+ is_intprob: bool
317
+ Flag for integer problems
299
318
 
319
+ :group: interfaces.pymoo
300
320
 
301
- def get_multi_obj_class(verbosity=1):
321
+ """
302
322
 
303
- class MultiObjProblem(get_single_obj_class(verbosity)):
323
+ def __init__(self, problem, vectorize):
304
324
  """
305
- Wrapper around the pymoo problem for a multiple
306
- objectives.
307
-
308
- At the moment this interface only supports
309
- pure int or pure float problems (not mixed).
325
+ Constructor template, will be overwritten by get_class
310
326
 
311
327
  Parameters
312
328
  ----------
313
- problem : iwopy.core.Problem
329
+ problem: iwopy.core.Problem
314
330
  The iwopy problem to solve
315
- vectorize : bool, optional
331
+ vectorize: bool, optional
316
332
  Switch for vectorized calculations, wrt
317
333
  population individuals
318
334
 
319
- Attributes
335
+ """
336
+ pass
337
+
338
+ def finalize(self, pymoo_results, verbosity=1):
339
+ """
340
+ Finalize the problem.
341
+
342
+ Parameters
320
343
  ----------
321
- problem : iwopy.core.Problem
322
- The iwopy problem to solve
323
- vectorize : bool
324
- Switch for vectorized calculations, wrt
325
- population individuals
326
- is_intprob : bool
327
- Flag for integer problems
344
+ pymoo_results: pymoo.Results
345
+ The results from the solver
346
+ verbosity: int
347
+ The verbosity level, 0 = silent
348
+
349
+ Returns
350
+ -------
351
+ results: iwopy.SingleObjOptResults
352
+ The optimization results object
328
353
 
329
354
  """
330
355
 
331
- def __init__(self, problem, vectorize):
332
- super().__init__(problem, vectorize)
333
-
334
- def finalize(self, pymoo_results, verbosity=1):
335
- """
336
- Finalize the problem.
337
-
338
- Parameters
339
- ----------
340
- pymoo_results: pymoo.Results
341
- The results from the solver
342
- verbosity : int
343
- The verbosity level, 0 = silent
344
-
345
- Returns
346
- -------
347
- results: iwopy.SingleObjOptResults
348
- The optimization results object
349
-
350
- """
351
-
352
- # prepare:
353
- r = pymoo_results
354
- suc = True
355
-
356
- # case no solution from pymoo:
357
- if r.X is None:
358
- suc = False
359
- xi = None
360
- xf = None
361
- res = None
362
- objs = None
363
- cons = None
364
-
365
- # evaluate pymoo final solution:
356
+ # prepare:
357
+ r = pymoo_results
358
+ suc = True
359
+
360
+ # case no solution from pymoo:
361
+ if r.X is None:
362
+ suc = False
363
+ xi = None
364
+ xf = None
365
+ res = None
366
+ objs = None
367
+ cons = None
368
+
369
+ # evaluate pymoo final solution:
370
+ else:
371
+ n_pop = len(r.pop)
372
+
373
+ if self.is_intprob:
374
+ xi = r.X
375
+ xf = np.zeros((n_pop, 0), dtype=np.float64)
366
376
  else:
367
- n_pop = len(r.pop)
377
+ xi = np.zeros((n_pop, 0), dtype=int)
378
+ xf = r.X
368
379
 
369
- if self.is_intprob:
370
- xi = r.X
371
- xf = np.zeros((n_pop, 0), dtype=np.float64)
372
- else:
373
- xi = np.zeros((n_pop, 0), dtype=int)
374
- xf = r.X
375
-
376
- res, objs, cons = self.problem.finalize_population(xi, xf, verbosity)
377
- if verbosity:
378
- print()
379
- suc = np.all(
380
- self.problem.check_constraints_population(cons, False), axis=1
381
- )
382
- if verbosity:
383
- print()
380
+ res, objs, cons = self.problem.finalize_population(xi, xf, verbosity)
381
+ if verbosity:
382
+ print()
383
+ suc = np.all(self.problem.check_constraints_population(cons, False), axis=1)
384
+ if verbosity:
385
+ print()
386
+
387
+ return MultiObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
388
+
389
+ @classmethod
390
+ def get_class(cls):
391
+ """
392
+ Creates the class, dynamically derived from SingleObjProblem
393
+ """
394
+ scls = SingleObjProblemTemplate.get_class()
395
+ attrb = {v: d for v, d in cls.__dict__.items()}
384
396
 
385
- return MultiObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
397
+ def init(self, *args, **kwargs):
398
+ scls.__init__(self, *args, **kwargs)
386
399
 
387
- return MultiObjProblem
400
+ attrb["__init__"] = init
401
+ attrb["__doc__"] = attrb["__doc__"].replace("Template for a w", "W")
402
+ del attrb["get_class"]
403
+ return type("MultiObjProblem", (scls,), attrb)