24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 | class SolverHandler:
"""Creates and runs a solver instance.
This may be an optimiser (e.g. VMCON) or an equation solver (e.g. fsolve).
Parameters
----------
models : process.main.Models
physics and engineering model objects
solver_name : str
which solver to use, as specified in solver.py
data: DataStructure
data structure object for providing objective/constraint data to
the solver
"""
def __init__(self, models, solver_name, data):
self.models = models
self.solver_name = solver_name
self.data = data
def run(self):
"""Run solver and retry if it fails in certain ways."""
# Initialise iteration variables and bounds in Fortran
load_iteration_variables(self.data)
load_scaled_bounds(self.data)
# Initialise iteration variables and bounds in Python: relies on Fortran
# iteration variables being defined above
# Trim maximum size arrays down to actually used size
x = self.data.numerics.xcm[: self.data.numerics.n_iteration_variables]
bndl = self.data.numerics.itv_scaled_lower_bounds[
: self.data.numerics.n_iteration_variables
]
bndu = self.data.numerics.itv_scaled_upper_bounds[
: self.data.numerics.n_iteration_variables
]
# Evaluators() calculates the objective and constraint functions and
# their gradients for a given vector x
evaluators = Evaluators(self.models, self.data, x)
# Configure solver for problem
self.solver = get_solver(self.data, self.solver_name)
self.solver.set_evaluators(evaluators)
self.solver.set_bounds(bndl, bndu)
self.solver.set_opt_params(x)
# Define total number of constraints and equality constraints
self.solver.set_constraints(
m=self.data.numerics.n_equality_constraints
+ self.data.numerics.n_inequality_constraints,
meq=self.data.numerics.n_equality_constraints,
)
ifail = self.solver.solve()
# If VMCON optimisation has failed then try altering value of epsfcn
if self.solver_name == "vmcon":
if ifail != SolverOutputCondition.CONVERGED:
with epsfcn_context(self.data.numerics, 10):
ifail = self.solver.solve()
if ifail != SolverOutputCondition.CONVERGED:
with epsfcn_context(self.data.numerics, 0.1):
ifail = self.solver.solve()
# If VMCON has exited with error code 5
# (ifail = SolverOutputCondition.NO_SOLUTION) try another run using a
# multiple of the identity matrix as input for the Hessian b(n,n)
# Only do this if VMCON has not iterated (n_solver_iterations=1)
if (
ifail == SolverOutputCondition.NO_SOLUTION
and self.data.numerics.n_solver_iterations < 2
):
print(
"VMCON error code = 5 (SolverOutputCondition.NO_SOLUTION). "
"Rerunning VMCON with a new initial estimate of the second "
"derivative matrix."
)
self.solver.set_b(2.0)
ifail = self.solver.solve()
self.output()
return ifail
def output(self):
"""Store results back in self.data.numerics module.
Objective function value, solution vector and constraints vector.
"""
self.data.numerics.norm_objf = self.solver.objf
# Slicing required due to Fortran arrays being maximum possible, rather
# than required, size
self.data.numerics.xcm[: self.solver.x.shape[0]] = self.solver.x
self.data.numerics.rcm[: self.solver.conf.shape[0]] = self.solver.conf
self._numerics_output()
self._optimisation_parameters_output()
def _numerics_output(self):
nums = self.data.numerics
nums.sqsumsq = sum(r**2 for r in nums.rcm[: nums.n_equality_constraints]) ** 0.5
process_output.oheadr(constants.NOUT, "Numerics")
s_type = (
"fsolve (evaluation)" if self.solver == "fsolve" else "VMCON (optimisation)"
)
process_output.ocmmnt(
constants.NOUT,
f"PROCESS has performed a {s_type} run",
)
ifail = self.solver.info
if ifail != SolverOutputCondition.CONVERGED:
process_output.ovarre(constants.NOUT, "Error flag", "(ifail)", ifail)
process_output.oheadr(
constants.IOTTY, "PROCESS COULD NOT FIND A FEASIBLE SOLUTION"
)
print()
logger.critical("Solver returns with ifail /= 1. %s", ifail)
if self.solver_name == "vmcon":
self.solver.verror()
process_output.oblnkl(constants.NOUT)
print()
else:
# Solution found
descr = "consistent" if self.solver == "fsolve" else "feasible"
process_output.ocmmnt(
constants.NOUT, f"and found a {descr} set of parameters."
)
process_output.oheadr(constants.IOTTY, f"PROCESS found a {descr} solution")
process_output.oblnkl(constants.NOUT)
process_output.ovarre(constants.NOUT, "Error flag", "(ifail)", ifail)
if nums.sqsumsq >= 1.0e-2:
string = (
"WARNING: Constraint residues are HIGH; consider re-running\n"
" with lower values of EPSVMC to confirm convergence...\n"
" (should be able to get down to about 1.0E-8 okay)\n"
)
process_output.ocmmnt(constants.NOUT, ("\n" + string))
print(string)
logger.warning(f"High final constraint residues. {nums.sqsumsq=}")
for d, var, v in (
(
"Number of iteration variables",
"(n_iteration_variables)",
nums.n_iteration_variables,
),
(
"Number of constraints (total)",
"(n_equality_constraints+n_inequality_constraints)",
nums.n_equality_constraints + nums.n_inequality_constraints,
),
("Optimisation switch", "(i_process_run_mode)", nums.i_process_run_mode),
):
process_output.ovarre(constants.NOUT, d, var, v)
process_output.ocmmnt(
constants.NOUT,
f" {PROCESSRunMode(nums.i_process_run_mode).description}",
)
# Objective function output: none for fsolve
if self.solver_name != "fsolve":
process_output.ovarre(
constants.NOUT,
"Figure of merit switch",
"(i_figure_merit)",
nums.i_figure_merit,
)
nums.objf_name = f'"{FiguresOfMerit(abs(nums.i_figure_merit)).description}"'
for d, var, v, o in (
("Objective function name", "(objf_name)", nums.objf_name, ""),
("Normalised objective function", "(norm_objf)", nums.norm_objf, "OP "),
(
"VMCON convergence parameter",
"(convergence_parameter)",
self.data.globals.convergence_parameter,
"OP ",
),
(
"Number of optimising solver iterations",
"(n_solver_iterations)",
nums.n_solver_iterations,
"OP ",
),
):
process_output.ovarre(constants.NOUT, d, var, v, o)
process_output.ovarre(
constants.NOUT,
"Square root of the sum of squares of the constraint residuals",
"(sqsumsq)",
nums.sqsumsq,
"OP ",
)
process_output.oblnkl(constants.NOUT)
if self.solver_name == "fsolve":
process_output.write(
constants.NOUT,
"PROCESS has solved using fsolve.\n"
if ifail == SolverOutputCondition.CONVERGED
else "PROCESS failed to solve using fsolve.\n",
)
else:
process_output.write(
constants.NOUT,
(
(
"PROCESS has successfully optimised"
if ifail == SolverOutputCondition.CONVERGED
else "PROCESS has failed to optimise"
)
+ " the optimisation parameters to"
+ ("minimise" if nums.i_figure_merit > 0 else "maximise")
+ f" the objective function: {nums.objf_name}\n"
),
)
def _optimisation_parameters_output(self):
nums = self.data.numerics
written_warning = False
# Output optimisation parameters
solution_vector_table = []
for i in range(nums.n_iteration_variables):
nums.xcs[i] = nums.xcm[i] * nums.scafc[i]
name = nums.lablxc[nums.ixc[i] - 1]
solution_vector_table.append([name, nums.xcs[i], nums.xcm[i]])
xminn = 1.01 * nums.itv_scaled_lower_bounds[i]
xmaxx = 0.99 * nums.itv_scaled_upper_bounds[i]
# Write to output file if close to optimisation parameter bounds
if nums.xcm[i] < xminn or nums.xcm[i] > xmaxx:
if not written_warning:
written_warning = True
process_output.ocmmnt(
constants.NOUT,
(
"Certain operating limits have been reached,"
"\n as shown by the following optimisation parameters"
" that are"
"\n at or near to the edge of their prescribed range :\n"
),
)
xcval = nums.xcm[i] * nums.scafc[i]
if nums.xcm[i] < xminn:
location, bound = "below", "lower"
bounds = nums.itv_scaled_lower_bounds
else:
location, bound = "above", "upper"
bounds = nums.itv_scaled_upper_bounds
process_output.write(
constants.NOUT,
f" {name:<30}= {xcval} is at or {location} its {bound} bound:"
f" {bounds[i] * nums.scafc[i]}",
)
if nums.boundu[i] == nums.boundl[i]:
xnorm = 1.0
else:
xnorm = min(
max(
(nums.xcm[i] - nums.itv_scaled_lower_bounds[i])
/ (
nums.itv_scaled_upper_bounds[i]
- nums.itv_scaled_lower_bounds[i]
),
0.0,
),
1.0,
)
# Write optimisation parameters to mfile
for d, var, v in (
(nums.lablxc[nums.ixc[i] - 1], f"(itvar{i + 1:03d})", nums.xcs[i]),
(
f"{name} (final value/initial value)",
f"(xcm{i + 1:03d})",
nums.xcm[i],
),
(f"{name} (range normalised)", f"(nitvar{i + 1:03d})", xnorm),
(
f"{name} (upper bound)",
f"(boundu{i + 1:03d})",
nums.itv_scaled_upper_bounds[i] * nums.scafc[i],
),
(
f"{name} (lower bound)",
f"(boundl{i + 1:03d})",
nums.itv_scaled_lower_bounds[i] * nums.scafc[i],
),
):
process_output.ovarre(constants.MFILE, d, var, v)
# Write optimisation parameter headings to output file
process_output.osubhd(
constants.NOUT, "The solution vector is comprised as follows :"
)
process_output.write(
constants.NOUT,
tabulate(
solution_vector_table,
headers=["", "Final value", "Final / initial"],
numalign="left",
),
)
|