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.
@@ -1,336 +1,387 @@
1
1
  import numpy as np
2
2
 
3
3
  from iwopy.core import SingleObjOptResults, MultiObjOptResults
4
- from .imports import check_import, Problem, Real, Integer
5
-
6
-
7
- class SingleObjProblem(Problem):
8
- """
9
- Wrapper around the pymoo problem for a single
10
- objective.
11
-
12
- At the moment this interface only supports
13
- pure int or pure float problems (not mixed).
14
-
15
- Parameters
16
- ----------
17
- problem : iwopy.core.Problem
18
- The iwopy problem to solve
19
- vectorize : bool, optional
20
- Switch for vectorized calculations, wrt
21
- population individuals
22
-
23
- Attributes
24
- ----------
25
- problem : iwopy.core.Problem
26
- The iwopy problem to solve
27
- vectorize : bool
28
- Switch for vectorized calculations, wrt
29
- population individuals
30
- is_mixed : bool
31
- Flag for mixed integer/float problems
32
- is_intprob : bool
33
- Flag for integer problems
34
-
35
- """
36
-
37
- def __init__(self, problem, vectorize):
38
-
39
- check_import()
40
-
41
- self.problem = problem
42
- self.vectorize = vectorize
43
-
44
- if self.problem.n_vars_float > 0 and self.problem.n_vars_int == 0:
45
-
46
- self.is_mixed = False
47
- self.is_intprob = False
48
-
49
- super().__init__(
50
- n_var=self.problem.n_vars_float,
51
- n_obj=self.problem.n_objectives,
52
- n_ieq_constr=self.problem.n_constraints,
53
- xl=self.problem.min_values_float(),
54
- xu=self.problem.max_values_float(),
55
- elementwise=not vectorize,
56
- type_var=np.float64,
57
- )
58
-
59
- elif self.problem.n_vars_float == 0 and self.problem.n_vars_int > 0:
60
-
61
- self.is_mixed = False
62
- self.is_intprob = True
63
-
64
- super().__init__(
65
- n_var=self.problem.n_vars_int,
66
- n_obj=self.problem.n_objectives,
67
- n_ieq_constr=self.problem.n_constraints,
68
- xl=self.problem.min_values_int(),
69
- xu=self.problem.max_values_int(),
70
- elementwise=not vectorize,
71
- type_var=np.int32,
72
- )
73
-
74
- else:
75
-
76
- self.is_mixed = True
77
- self.is_intprob = False
78
-
79
- vars = {}
80
-
81
- nami = self.problem.var_names_int()
82
- inii = self.problem.initial_values_int()
83
- mini = self.problem.min_values_int()
84
- maxi = self.problem.max_values_int()
85
- for i, v in enumerate(nami):
86
- vars[v] = Integer(value=inii[i], bounds=(mini[i], maxi[i]))
87
-
88
- namf = self.problem.var_names_float()
89
- inif = self.problem.initial_values_float()
90
- minf = self.problem.min_values_float()
91
- maxf = self.problem.max_values_float()
92
- for i, v in enumerate(namf):
93
- vars[v] = Real(value=inif[i], bounds=(minf[i], maxf[i]))
94
-
95
- super().__init__(
96
- vars=vars,
97
- n_obj=self.problem.n_objectives,
98
- n_ieq_constr=self.problem.n_constraints,
99
- elementwise=not vectorize,
100
- )
101
-
102
- if self.problem.n_constraints:
103
-
104
- self._cmi = self.problem.min_values_constraints
105
- self._cma = self.problem.max_values_constraints
106
- cnames = self.problem.cons.component_names
107
-
108
- sel = np.isinf(self._cmi) & np.isinf(self._cma)
109
- if np.any(sel):
110
- raise RuntimeError(f"Missing boundaries for constraints {cnames[sel]}")
111
-
112
- sel = (~np.isinf(self._cmi)) & (~np.isinf(self._cma))
113
- if np.any(sel):
114
- raise RuntimeError(
115
- f"Constraints {cnames[sel]} have both lower and upper bounds"
116
- )
4
+ from . import imports
117
5
 
118
- def _evaluate(self, x, out, *args, **kwargs):
119
- """
120
- Overloading the abstract evaluation function
121
- of the pymoo base class.
122
- """
123
6
 
124
- # vectorized run:
125
- if self.vectorize:
7
+ def get_single_obj_class(verbosity=1):
126
8
 
127
- if self.is_mixed:
128
- xi = np.array([[dct[v] for v in self.problem.var_names_int()] for dct in x], dtype=np.int32)
129
- xf = np.array([[dct[v] for v in self.problem.var_names_float()] for dct in x], dtype=np.float64)
130
- out["F"], out["G"] = self.problem.evaluate_population(xi, xf)
131
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
132
- else:
133
- n_pop = x.shape[0]
134
- if self.is_intprob:
135
- dummies = np.zeros((n_pop, 0), dtype=np.float64)
136
- out["F"], out["G"] = self.problem.evaluate_population(x, dummies)
137
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
138
- else:
139
- dummies = np.zeros((n_pop, 0), dtype=np.int32)
140
- out["F"], out["G"] = self.problem.evaluate_population(dummies, x)
141
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)[None, :]
9
+ imports.load(verbosity)
142
10
 
143
- if self.problem.n_constraints:
11
+ class SingleObjProblem(imports.Problem):
12
+ """
13
+ Wrapper around the pymoo problem for a single
14
+ objective.
144
15
 
145
- sel = ~np.isinf(self._cma)
146
- out["G"][:, sel] = out["G"][:, sel] - self._cma[None, sel]
16
+ At the moment this interface only supports
17
+ pure int or pure float problems (not mixed).
147
18
 
148
- sel = ~np.isinf(self._cmi)
149
- out["G"][:, sel] = self._cmi[None, sel] - out["G"][:, sel]
19
+ Parameters
20
+ ----------
21
+ problem : iwopy.core.Problem
22
+ The iwopy problem to solve
23
+ vectorize : bool, optional
24
+ Switch for vectorized calculations, wrt
25
+ population individuals
150
26
 
151
- # individual run:
152
- else:
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
153
38
 
154
- if self.is_mixed:
155
- xi = np.array([x[v] for v in self.problem.var_names_int()], dtype=np.int32)
156
- xf = np.array([x[v] for v in self.problem.var_names_float()], dtype=np.float64)
157
- out["F"], out["G"] = self.problem.evaluate_individual(xi, xf)
158
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
159
- else:
160
- n_pop = x.shape[0]
161
- if self.is_intprob:
162
- dummies = np.zeros(0, dtype=np.float64)
163
- out["F"], out["G"] = self.problem.evaluate_individual(x, dummies)
164
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
165
- else:
166
- dummies = np.zeros(0, dtype=np.int32)
167
- out["F"], out["G"] = self.problem.evaluate_individual(dummies, x)
168
- out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
39
+ """
169
40
 
170
- if self.problem.n_constraints:
41
+ def __init__(self, problem, vectorize):
171
42
 
172
- sel = ~np.isinf(self._cma)
173
- out["G"][sel] = out["G"][sel] - self._cma[sel]
43
+ self.problem = problem
44
+ self.vectorize = vectorize
174
45
 
175
- sel = ~np.isinf(self._cmi)
176
- out["G"][sel] = self._cmi[sel] - out["G"][sel]
46
+ if self.problem.n_vars_float > 0 and self.problem.n_vars_int == 0:
177
47
 
178
- def finalize(self, pymoo_results, verbosity=1):
179
- """
180
- Finalize the problem.
48
+ self.is_mixed = False
49
+ self.is_intprob = False
181
50
 
182
- Parameters
183
- ----------
184
- pymoo_results: pymoo.Results
185
- The results from the solver
186
- verbosity : int
187
- The verbosity level, 0 = silent
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,
59
+ )
188
60
 
189
- Returns
190
- -------
191
- results: iwopy.SingleObjOptResults
192
- The optimization results object
61
+ elif self.problem.n_vars_float == 0 and self.problem.n_vars_int > 0:
193
62
 
194
- """
63
+ self.is_mixed = False
64
+ self.is_intprob = True
65
+
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
+ )
195
75
 
196
- # prepare:
197
- r = pymoo_results
198
- suc = True
199
-
200
- # case no solution from pymoo:
201
- if r.X is None:
202
- suc = False
203
- xi = None
204
- xf = None
205
- res = None
206
- objs = None
207
- cons = None
208
-
209
- # evaluate pymoo final solution:
210
- else:
211
- if self.is_mixed:
212
- xi = np.array([r.X[v] for v in self.problem.var_names_int()], dtype=np.int32)
213
- xf = np.array([r.X[v] for v in self.problem.var_names_float()], dtype=np.float64)
214
76
  else:
215
- if self.is_intprob:
216
- xi = r.X
217
- xf = np.zeros(0, dtype=np.float64)
218
- else:
219
- xi = np.zeros(0, dtype=np.int32)
220
- xf = np.array(r.X, dtype=np.float64)
221
77
 
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,
102
+ )
103
+
104
+ if self.problem.n_constraints:
105
+
106
+ self._cmi = self.problem.min_values_constraints
107
+ self._cma = self.problem.max_values_constraints
108
+ cnames = self.problem.cons.component_names
109
+
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
+ )
115
+
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
+ )
121
+
122
+ def _evaluate(self, x, out, *args, **kwargs):
123
+ """
124
+ Overloading the abstract evaluation function
125
+ of the pymoo base class.
126
+ """
127
+
128
+ # vectorized run:
222
129
  if self.vectorize:
223
130
 
224
131
  if self.is_mixed:
225
- pxi = np.array([[p.X[v] for v in self.problem.var_names_int()] for p in r.pop], dtype=np.int32)
226
- pxf = np.array([[p.X[v] for v in self.problem.var_names_float()] for p in r.pop], dtype=np.float64)
227
- self.problem.finalize_population(pxi, pxf, verbosity)
228
- del pxi, pxf
229
-
132
+ xi = np.array(
133
+ [[dct[v] for v in self.problem.var_names_int()] for dct in x],
134
+ dtype=np.int32,
135
+ )
136
+ xf = np.array(
137
+ [[dct[v] for v in self.problem.var_names_float()] for dct in x],
138
+ dtype=np.float64,
139
+ )
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, :]
230
142
  else:
231
- n_pop = len(r.pop)
232
- n_vars = len(r.X)
233
- vars = np.zeros((n_pop, n_vars), dtype=np.float64)
234
- for pi, p in enumerate(r.pop):
235
- vars[pi] = p.X
143
+ n_pop = x.shape[0]
236
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:
237
153
  dummies = np.zeros((n_pop, 0), dtype=np.int32)
238
- self.problem.finalize_population(
239
- vars.astype(np.int32), dummies, verbosity
154
+ out["F"], out["G"] = self.problem.evaluate_population(
155
+ dummies, x
240
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]
168
+
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
+ else:
182
+ n_pop = x.shape[0]
183
+ if self.is_intprob:
184
+ dummies = np.zeros(0, dtype=np.float64)
185
+ out["F"], out["G"] = self.problem.evaluate_individual(
186
+ x, dummies
187
+ )
188
+ out["F"] *= np.where(self.problem.maximize_objs, -1.0, 1.0)
241
189
  else:
242
- dummies = np.zeros((n_pop, 0), dtype=np.float64)
243
- self.problem.finalize_population(dummies, vars, verbosity)
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)
244
195
 
245
- res, objs, cons = self.problem.finalize_individual(xi, xf, verbosity)
196
+ if self.problem.n_constraints:
246
197
 
247
- if verbosity:
248
- print()
249
- suc = np.all(self.problem.check_constraints_individual(cons, False))
250
- if verbosity:
251
- print()
198
+ sel = ~np.isinf(self._cma)
199
+ out["G"][sel] = out["G"][sel] - self._cma[sel]
252
200
 
253
- return SingleObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
201
+ sel = ~np.isinf(self._cmi)
202
+ out["G"][sel] = self._cmi[sel] - out["G"][sel]
254
203
 
204
+ def finalize(self, pymoo_results, verbosity=1):
205
+ """
206
+ Finalize the problem.
255
207
 
256
- class MultiObjProblem(SingleObjProblem):
257
- """
258
- Wrapper around the pymoo problem for a multiple
259
- objectives.
208
+ Parameters
209
+ ----------
210
+ pymoo_results: pymoo.Results
211
+ The results from the solver
212
+ verbosity : int
213
+ The verbosity level, 0 = silent
260
214
 
261
- At the moment this interface only supports
262
- pure int or pure float problems (not mixed).
215
+ Returns
216
+ -------
217
+ results: iwopy.SingleObjOptResults
218
+ The optimization results object
263
219
 
264
- Parameters
265
- ----------
266
- problem : iwopy.core.Problem
267
- The iwopy problem to solve
268
- vectorize : bool, optional
269
- Switch for vectorized calculations, wrt
270
- population individuals
220
+ """
271
221
 
272
- Attributes
273
- ----------
274
- problem : iwopy.core.Problem
275
- The iwopy problem to solve
276
- vectorize : bool
277
- Switch for vectorized calculations, wrt
278
- population individuals
279
- is_intprob : bool
280
- Flag for integer problems
222
+ # prepare:
223
+ r = pymoo_results
224
+ suc = True
281
225
 
282
- """
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
283
234
 
284
- def __init__(self, problem, vectorize):
285
- super().__init__(problem, vectorize)
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
286
299
 
287
- def finalize(self, pymoo_results, verbosity=1):
300
+
301
+ def get_multi_obj_class(verbosity=1):
302
+
303
+ class MultiObjProblem(get_single_obj_class(verbosity)):
288
304
  """
289
- Finalize the problem.
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).
290
310
 
291
311
  Parameters
292
312
  ----------
293
- pymoo_results: pymoo.Results
294
- The results from the solver
295
- verbosity : int
296
- The verbosity level, 0 = silent
313
+ problem : iwopy.core.Problem
314
+ The iwopy problem to solve
315
+ vectorize : bool, optional
316
+ Switch for vectorized calculations, wrt
317
+ population individuals
297
318
 
298
- Returns
299
- -------
300
- results: iwopy.SingleObjOptResults
301
- The optimization results object
319
+ Attributes
320
+ ----------
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
302
328
 
303
329
  """
304
330
 
305
- # prepare:
306
- r = pymoo_results
307
- suc = True
308
-
309
- # case no solution from pymoo:
310
- if r.X is None:
311
- suc = False
312
- xi = None
313
- xf = None
314
- res = None
315
- objs = None
316
- cons = None
317
-
318
- # evaluate pymoo final solution:
319
- else:
320
- n_pop = len(r.pop)
321
-
322
- if self.is_intprob:
323
- xi = r.X
324
- xf = np.zeros((n_pop, 0), dtype=np.float64)
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:
325
366
  else:
326
- xi = np.zeros((n_pop, 0), dtype=int)
327
- xf = r.X
367
+ n_pop = len(r.pop)
368
+
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()
328
384
 
329
- res, objs, cons = self.problem.finalize_population(xi, xf, verbosity)
330
- if verbosity:
331
- print()
332
- suc = np.all(self.problem.check_constraints_population(cons, False), axis=1)
333
- if verbosity:
334
- print()
385
+ return MultiObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
335
386
 
336
- return MultiObjOptResults(self.problem, suc, xi, xf, objs, cons, res)
387
+ return MultiObjProblem
@@ -0,0 +1 @@
1
+ from .optimizer import Optimizer_scipy