foxes 0.7.4.25__py3-none-any.whl → 0.8.1__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 foxes might be problematic. Click here for more details.

Files changed (33) hide show
  1. foxes/VERSION +1 -1
  2. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/METADATA +20 -116
  3. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/RECORD +7 -33
  4. foxes/opt/__init__.py +0 -9
  5. foxes/opt/constraints/__init__.py +0 -6
  6. foxes/opt/constraints/area_geometry.py +0 -214
  7. foxes/opt/constraints/min_dist.py +0 -239
  8. foxes/opt/core/__init__.py +0 -9
  9. foxes/opt/core/farm_constraint.py +0 -96
  10. foxes/opt/core/farm_objective.py +0 -97
  11. foxes/opt/core/farm_opt_problem.py +0 -346
  12. foxes/opt/core/farm_vars_problem.py +0 -219
  13. foxes/opt/core/pop_states.py +0 -206
  14. foxes/opt/objectives/__init__.py +0 -6
  15. foxes/opt/objectives/farm_vars.py +0 -323
  16. foxes/opt/objectives/max_n_turbines.py +0 -142
  17. foxes/opt/problems/__init__.py +0 -7
  18. foxes/opt/problems/layout/__init__.py +0 -9
  19. foxes/opt/problems/layout/farm_layout.py +0 -137
  20. foxes/opt/problems/layout/geom_layouts/__init__.py +0 -10
  21. foxes/opt/problems/layout/geom_layouts/constraints.py +0 -802
  22. foxes/opt/problems/layout/geom_layouts/geom_layout.py +0 -290
  23. foxes/opt/problems/layout/geom_layouts/geom_layout_gridded.py +0 -276
  24. foxes/opt/problems/layout/geom_layouts/geom_reggrid.py +0 -351
  25. foxes/opt/problems/layout/geom_layouts/geom_reggrids.py +0 -482
  26. foxes/opt/problems/layout/geom_layouts/objectives.py +0 -666
  27. foxes/opt/problems/layout/reggrids_layout.py +0 -417
  28. foxes/opt/problems/layout/regular_layout.py +0 -350
  29. foxes/opt/problems/opt_farm_vars.py +0 -586
  30. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/LICENSE +0 -0
  31. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/WHEEL +0 -0
  32. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/top_level.txt +0 -0
  33. {foxes-0.7.4.25.dist-info → foxes-0.8.1.dist-info}/zip-safe +0 -0
@@ -1,346 +0,0 @@
1
- import numpy as np
2
- from iwopy import Problem
3
-
4
- from foxes.utils.runners import DefaultRunner
5
- import foxes.constants as FC
6
- from .pop_states import PopStates
7
-
8
-
9
- class FarmOptProblem(Problem):
10
- """
11
- Abstract base class of wind farm optimization problems.
12
-
13
- Attributes
14
- ----------
15
- algo: foxes.core.Algorithm
16
- The algorithm
17
- runner: foxes.core.Runner
18
- The runner for running the algorithm
19
- calc_farm_args: dict
20
- Additional parameters for algo.calc_farm()
21
- points : numpy.ndarray
22
- The probe points, shape: (n_states, n_points, 3)
23
-
24
- :group: opt.core
25
-
26
- """
27
-
28
- def __init__(
29
- self,
30
- name,
31
- algo,
32
- runner=None,
33
- sel_turbines=None,
34
- calc_farm_args={},
35
- points=None,
36
- **kwargs,
37
- ):
38
- """
39
- Constructor.
40
-
41
- Parameters
42
- ----------
43
- name: str
44
- The problem's name
45
- algo: foxes.core.Algorithm
46
- The algorithm
47
- runner: foxes.core.Runner, optional
48
- The runner for running the algorithm
49
- sel_turbines: list of int, optional
50
- The turbines selected for optimization,
51
- or None for all
52
- calc_farm_args: dict
53
- Additional parameters for algo.calc_farm()
54
- points : numpy.ndarray, optional
55
- The probe points, shape: (n_states, n_points, 3)
56
- kwargs: dict, optional
57
- Additional parameters for `iwopy.Problem`
58
-
59
- """
60
- super().__init__(name, **kwargs)
61
-
62
- self.algo = algo
63
- self.runner = runner
64
- self.calc_farm_args = calc_farm_args
65
- self.points = points
66
-
67
- self._sel_turbines = sel_turbines
68
- self._count = None
69
-
70
- @property
71
- def farm(self):
72
- """
73
- The wind farm
74
-
75
- Returns
76
- -------
77
- foxes.core.WindFarm :
78
- The wind farm
79
-
80
- """
81
- return self.algo.farm
82
-
83
- @property
84
- def sel_turbines(self):
85
- """
86
- The selected turbines
87
-
88
- Returns
89
- -------
90
- list of int :
91
- Indices of the selected turbines
92
-
93
- """
94
- return (
95
- self._sel_turbines
96
- if self._sel_turbines is not None
97
- else list(range(self.farm.n_turbines))
98
- )
99
-
100
- @property
101
- def n_sel_turbines(self):
102
- """
103
- The numer of selected turbines
104
-
105
- Returns
106
- -------
107
- int :
108
- The numer of selected turbines
109
-
110
- """
111
- return len(self.sel_turbines)
112
-
113
- @property
114
- def all_turbines(self):
115
- """
116
- Flag for all turbines optimization
117
-
118
- Returns
119
- -------
120
- bool :
121
- True if all turbines are subject to optimization
122
-
123
- """
124
- return len(self.sel_turbines) == self.algo.n_turbines
125
-
126
- @property
127
- def counter(self):
128
- """
129
- The current value of the application counter
130
-
131
- Returns
132
- -------
133
- int :
134
- The current value of the application counter
135
-
136
- """
137
- return self._count
138
-
139
- @classmethod
140
- def tvar(cls, var, turbine_i):
141
- """
142
- Gets turbine variable name
143
-
144
- Parameters
145
- ----------
146
- var: str
147
- The variable name
148
- turbine_i: int
149
- The turbine index
150
-
151
- Returns
152
- -------
153
- str :
154
- The turbine variable name
155
-
156
- """
157
- return f"{var}_{turbine_i:04d}"
158
-
159
- @classmethod
160
- def parse_tvar(cls, tvr):
161
- """
162
- Parse foxes variable name and turbine index
163
- from turbine variable
164
-
165
- Parameters
166
- ----------
167
- tvr: str
168
- The turbine variable name
169
-
170
- Returns
171
- -------
172
- var: str
173
- The foxes variable name
174
- turbine_i: int
175
- The turbine index
176
-
177
- """
178
- t = tvr.split("_")
179
- return t[0], int(t[1])
180
-
181
- def initialize(self, verbosity=1):
182
- """
183
- Initialize the object.
184
-
185
- Parameters
186
- ----------
187
- verbosity: int
188
- The verbosity level, 0 = silent
189
-
190
- """
191
- if self.runner is None:
192
- self.runner = DefaultRunner()
193
- self.runner.initialize()
194
- elif not self.runner.initialized:
195
- raise ValueError(f"FarmOptProblem '{self.name}': Runner not initialized.")
196
-
197
- if not self.algo.initialized:
198
- self.algo.initialize()
199
- self._org_states_name = self.algo.states.name
200
- self._org_n_states = self.algo.n_states
201
-
202
- self.algo.finalize()
203
-
204
- self._count = 0
205
-
206
- super().initialize(verbosity)
207
-
208
- def _reset_states(self, states):
209
- """
210
- Reset the states in the algorithm
211
- """
212
- if states is not self.algo.states:
213
- if self.algo.initialized:
214
- self.algo.finalize()
215
- self.algo.states = states
216
-
217
- def update_problem_individual(self, vars_int, vars_float):
218
- """
219
- Update the algo and other data using
220
- the latest optimization variables.
221
-
222
- This function is called before running the farm
223
- calculation.
224
-
225
- Parameters
226
- ----------
227
- vars_int: np.array
228
- The integer variable values, shape: (n_vars_int,)
229
- vars_float: np.array
230
- The float variable values, shape: (n_vars_float,)
231
-
232
- """
233
- # reset states, if needed:
234
- if isinstance(self.algo.states, PopStates):
235
- self._reset_states(self.algo.states.states)
236
- self.algo.n_states = self._org_n_states
237
-
238
- def update_problem_population(self, vars_int, vars_float):
239
- """
240
- Update the algo and other data using
241
- the latest optimization variables.
242
-
243
- This function is called before running the farm
244
- calculation.
245
-
246
- Parameters
247
- ----------
248
- vars_int: np.array
249
- The integer variable values, shape: (n_pop, n_vars_int,)
250
- vars_float: np.array
251
- The float variable values, shape: (n_pop, n_vars_float,)
252
-
253
- """
254
- # set/reset pop states, if needed:
255
- n_pop = len(vars_float)
256
- if not isinstance(self.algo.states, PopStates):
257
- self._reset_states(PopStates(self.algo.states, n_pop))
258
- elif self.algo.states.n_pop != n_pop:
259
- ostates = self.algo.states.states
260
- self._reset_states(PopStates(ostates, n_pop))
261
-
262
- def apply_individual(self, vars_int, vars_float):
263
- """
264
- Apply new variables to the problem.
265
-
266
- Parameters
267
- ----------
268
- vars_int: np.array
269
- The integer variable values, shape: (n_vars_int,)
270
- vars_float: np.array
271
- The float variable values, shape: (n_vars_float,)
272
-
273
- Returns
274
- -------
275
- problem_results: Any
276
- The results of the variable application
277
- to the problem
278
-
279
- """
280
- self._count += 1
281
- self.update_problem_individual(vars_int, vars_float)
282
- farm_results = self.runner.run(self.algo.calc_farm, kwargs=self.calc_farm_args)
283
-
284
- if self.points is None:
285
- return farm_results
286
- else:
287
- point_results = self.runner.run(
288
- self.algo.calc_points, args=(farm_results, self.points)
289
- )
290
- return farm_results, point_results
291
-
292
- def apply_population(self, vars_int, vars_float):
293
- """
294
- Apply new variables to the problem,
295
- for a whole population.
296
-
297
- Parameters
298
- ----------
299
- vars_int: np.array
300
- The integer variable values, shape: (n_pop, n_vars_int)
301
- vars_float: np.array
302
- The float variable values, shape: (n_pop, n_vars_float)
303
-
304
- Returns
305
- -------
306
- problem_results: Any
307
- The results of the variable application
308
- to the problem
309
-
310
- """
311
- self._count += 1
312
-
313
- self.update_problem_population(vars_int, vars_float)
314
- farm_results = self.runner.run(self.algo.calc_farm, kwargs=self.calc_farm_args)
315
- farm_results["n_pop"] = len(vars_float)
316
- farm_results["n_org_states"] = self._org_n_states
317
-
318
- if self.points is None:
319
- return farm_results
320
- else:
321
- n_pop = farm_results["n_pop"].values
322
- n_states, n_points = self.points.shape[:2]
323
- pop_points = np.zeros((n_pop, n_states, n_points, 3), dtype=FC.DTYPE)
324
- pop_points[:] = self.points[None, :, :, :]
325
- pop_points = pop_points.reshape(n_pop * n_states, n_points, 3)
326
- point_results = self.runner.run(
327
- self.algo.calc_points, args=(farm_results, pop_points)
328
- )
329
- return farm_results, point_results
330
-
331
- def add_to_layout_figure(self, ax, **kwargs):
332
- """
333
- Add to a layout figure
334
-
335
- Parameters
336
- ----------
337
- ax: matplotlib.pyplot.Axis
338
- The figure axis
339
-
340
- """
341
- for c in self.cons.functions:
342
- ax = c.add_to_layout_figure(ax, **kwargs)
343
- for f in self.objs.functions:
344
- ax = f.add_to_layout_figure(ax, **kwargs)
345
-
346
- return ax
@@ -1,219 +0,0 @@
1
- import numpy as np
2
- from abc import abstractmethod
3
-
4
- from .farm_opt_problem import FarmOptProblem
5
- from foxes.models.turbine_models import SetFarmVars
6
- import foxes.constants as FC
7
-
8
-
9
- class FarmVarsProblem(FarmOptProblem):
10
- """
11
- Abstract base class for models that optimize
12
- farm variables.
13
-
14
- :group: opt.core
15
-
16
- """
17
-
18
- def initialize(self, pre_rotor_vars, post_rotor_vars, verbosity=1, **kwargs):
19
- """
20
- Initialize the object.
21
-
22
- Parameters
23
- ----------
24
- pre_rotor_vars: list of str or dict
25
- The pre_rotor farm variables. If dict, then
26
- key: sub-model str, value: var names as list of str
27
- post_rotor_vars: list of str or dict
28
- The post_rotor farm variables. If dict, then
29
- key: sub-model str, value: var names as list of str
30
- verbosity: int
31
- The verbosity level, 0 = silent
32
- kwargs: dict, optional
33
- Additional parameters for super class init
34
-
35
- """
36
- self._vars_pre = {}
37
- self._vars_post = {}
38
- if isinstance(pre_rotor_vars, dict):
39
- self._vars_pre = {m: v for m, v in pre_rotor_vars.items() if len(v)}
40
- elif len(pre_rotor_vars):
41
- self._vars_pre = {self.name: pre_rotor_vars}
42
- if isinstance(post_rotor_vars, dict):
43
- self._vars_post = {m: v for m, v in post_rotor_vars.items() if len(v)}
44
- elif len(post_rotor_vars):
45
- self._vars_post = {self.name: post_rotor_vars}
46
-
47
- cnt = 0
48
- for src, pre in zip((self._vars_pre, self._vars_post), (True, False)):
49
- for mname, vrs in src.items():
50
- if mname in self.algo.mbook.turbine_models:
51
- m = self.algo.mbook.turbine_models[mname]
52
- if not isinstance(m, SetFarmVars):
53
- raise KeyError(
54
- f"FarmOptProblem '{self.name}': Turbine model entry '{mname}' already exists in model book, and is not of type SetFarmVars"
55
- )
56
- elif m.pre_rotor != pre:
57
- raise ValueError(
58
- f"FarmOptProblem '{self.name}': Turbine model entry '{mname}' exists in model book, and disagrees on pre_rotor = {pre}"
59
- )
60
- else:
61
- self.algo.mbook.turbine_models[mname] = SetFarmVars(pre_rotor=pre)
62
-
63
- found = False
64
- for t in self.algo.farm.turbines:
65
- if mname in t.models:
66
- found = True
67
- break
68
- if not found:
69
- raise ValueError(
70
- f"FarmOptProblem '{self.name}': Missing entry '{mname}' among any of the turbine models"
71
- )
72
- cnt += len(vrs)
73
- if not cnt:
74
- raise ValueError(
75
- f"Problem '{self.name}': Neither pre_rotor_vars not post_rotor_vars containing variables"
76
- )
77
-
78
- super().initialize(verbosity=verbosity, **kwargs)
79
-
80
- @abstractmethod
81
- def opt2farm_vars_individual(self, vars_int, vars_float):
82
- """
83
- Translates optimization variables to farm variables
84
-
85
- Parameters
86
- ----------
87
- vars_int: numpy.ndarray
88
- The integer optimization variable values,
89
- shape: (n_vars_int,)
90
- vars_float: numpy.ndarray
91
- The float optimization variable values,
92
- shape: (n_vars_float,)
93
-
94
- Returns
95
- -------
96
- farm_vars: dict
97
- The foxes farm variables. Key: var name,
98
- value: numpy.ndarray with values, shape:
99
- (n_states, n_sel_turbines)
100
-
101
- """
102
- pass
103
-
104
- @abstractmethod
105
- def opt2farm_vars_population(self, vars_int, vars_float, n_states):
106
- """
107
- Translates optimization variables to farm variables
108
-
109
- Parameters
110
- ----------
111
- vars_int: numpy.ndarray
112
- The integer optimization variable values,
113
- shape: (n_pop, n_vars_int)
114
- vars_float: numpy.ndarray
115
- The float optimization variable values,
116
- shape: (n_pop, n_vars_float)
117
- n_states: int
118
- The number of original (non-pop) states
119
-
120
- Returns
121
- -------
122
- farm_vars: dict
123
- The foxes farm variables. Key: var name,
124
- value: numpy.ndarray with values, shape:
125
- (n_pop, n_states, n_sel_turbines)
126
-
127
- """
128
- pass
129
-
130
- def update_problem_individual(self, vars_int, vars_float):
131
- """
132
- Update the algo and other data using
133
- the latest optimization variables.
134
-
135
- This function is called before running the farm
136
- calculation.
137
-
138
- Parameters
139
- ----------
140
- vars_int: np.array
141
- The integer variable values, shape: (n_vars_int,)
142
- vars_float: np.array
143
- The float variable values, shape: (n_vars_float,)
144
-
145
- """
146
- super().update_problem_individual(vars_int, vars_float)
147
-
148
- # prepare:
149
- n_states = self._org_n_states
150
- fvars = self.opt2farm_vars_individual(vars_int, vars_float)
151
-
152
- # update turbine model that sets vars to opt values:
153
- for src in (self._vars_pre, self._vars_post):
154
- for mname, vrs in src.items():
155
- model = self.algo.mbook.turbine_models[mname]
156
- model.reset()
157
- for v in vrs:
158
- vals = fvars.pop(v)
159
- if self.all_turbines:
160
- model.add_var(v, vals)
161
- else:
162
- data = np.zeros(
163
- (n_states, self.algo.n_turbines), dtype=FC.DTYPE
164
- )
165
- data[:, self.sel_turbines] = vals
166
- model.add_var(v, data)
167
-
168
- if len(fvars):
169
- raise KeyError(
170
- f"Problem '{self.name}': Too many farm vars from opt2farm_vars_individual: {list(fvars.keys())}"
171
- )
172
-
173
- def update_problem_population(self, vars_int, vars_float):
174
- """
175
- Update the algo and other data using
176
- the latest optimization variables.
177
-
178
- This function is called before running the farm
179
- calculation.
180
-
181
- Parameters
182
- ----------
183
- vars_int: np.array
184
- The integer variable values, shape: (n_pop, n_vars_int,)
185
- vars_float: np.array
186
- The float variable values, shape: (n_pop, n_vars_float,)
187
-
188
- """
189
- super().update_problem_population(vars_int, vars_float)
190
-
191
- # prepare:
192
- n_pop = len(vars_float)
193
- n_states = self._org_n_states
194
- n_pstates = n_states * n_pop
195
- fvars = self.opt2farm_vars_population(vars_int, vars_float, n_states)
196
-
197
- # update turbine model that sets vars to opt values:
198
- for src in (self._vars_pre, self._vars_post):
199
- for mname, vrs in src.items():
200
- model = self.algo.mbook.turbine_models[mname]
201
- model.reset()
202
- for v in vrs:
203
- vals = fvars.pop(v)
204
- shp0 = list(vals.shape)
205
- shp1 = [n_pstates] + shp0[2:]
206
- if self.all_turbines:
207
- model.add_var(v, vals.reshape(shp1))
208
- else:
209
- data = np.zeros(
210
- (n_pstates, self.algo.n_turbines), dtype=FC.DTYPE
211
- )
212
- data[:, self.sel_turbines] = vals.reshape(shp1)
213
- model.add_var(v, data)
214
- del data
215
-
216
- if len(fvars):
217
- raise KeyError(
218
- f"Problem '{self.name}': Too many farm vars from opt2farm_vars_population: {list(fvars.keys())}"
219
- )