7. Optimization with CSDL models
Problem:
\[\begin{split}
\begin{align}
\underset{x, y \in \mathbb{R}}{\text{minimize}} & \quad x^4 + y^4 \\
\text{subject to} & \quad x\geq0, \\
& \quad x+y=1, \\
& \quad x-y\geq1.
\end{align}
\end{split}\]
from csdl import Model
# minimize x^4 + y^4 subject to x>=0, x+y=1, x-y>=1.
class QuarticFunc(Model):
def initialize(self):
pass
def define(self):
# add_inputs
x = self.create_input('x', val=1.)
y = self.create_input('y', val=1.)
z = x**4 + y**4
# add_outputs
self.register_output('z', z)
constraint_1 = x + y
constraint_2 = x - y
self.register_output('constraint_1', constraint_1)
self.register_output('constraint_2', constraint_2)
# define optimization problem
self.add_design_variable('x', lower=0.)
self.add_design_variable('y')
self.add_objective('z')
self.add_constraint('constraint_1', equals=1.)
self.add_constraint('constraint_2', lower=1.)
if __name__ == "__main__":
# from csdl_om import Simulator
from python_csdl_backend import Simulator
# Create a Simulator object for your model
sim = Simulator(QuarticFunc())
from modopt import CSDLProblem
# Instantiate your problem using the csdl Simulator object and name your problem
prob = CSDLProblem(problem_name='quartic',simulator=sim)
from modopt import SQP, SLSQP, SNOPT, PySLSQP
# Setup your preferred optimizer (here, SLSQP) with the Problem object
# Pass in the options for your chosen optimizer
optimizer = PySLSQP(prob, solver_options={'maxiter': 20, 'acc':1e-6})
# optimizer = SLSQP(prob, solver_options={'maxiter':20, 'ftol':1e-6})
# optimizer = SQP(prob, maxiter=20)
snopt_options = {
'Infinite bound': 1.0e20,
'Verify level': 3,
'Verbose': True,
}
# optimizer = SNOPT(prob, solver_options=snopt_options)
# Check first derivatives at the initial guess, if needed
# optimizer.check_first_derivatives(prob.x0)
# sim.run()
# sim.check_totals()
# Solve your optimization problem
optimizer.solve()
# Print results of optimization (summary_table contains information from each iteration)
optimizer.print_results(summary_table=True)
print(sim['x'])
print(sim['y'])
print(sim['z'])
/Users//modopt/modopt/external_libraries/snopt/snoptc.py:6: UserWarning: snoptc from 'optimize' could not be imported
warnings.warn("snoptc from 'optimize' could not be imported")
/Users//modopt/modopt/external_libraries/snopt/snoptc.py:10: UserWarning: snopt7_python from 'optimize.solvers' could not be imported
warnings.warn("snopt7_python from 'optimize.solvers' could not be imported")
/Users//modopt/modopt/external_libraries/snopt/snopt_optimizer.py:6: UserWarning: SNOPT_options from 'optimize' could not be imported
warnings.warn("SNOPT_options from 'optimize' could not be imported")
Deleting self.pFpx ...
Deleting self.pCpx, pCpx_dict ...
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing gradient >>>>>>>>>>
(('constraint_1', 'constraint_2', 'z'), ('x', 'y')) not found, generating...
generating: DERIVATIVESconstraint_1,constraint_2,z-->x,y
constraint_1--> (1/5) |........ |
constraint_2--> (1/5) |........ |
z--> (3/5) |........................ |
---------Computed gradient---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing gradient >>>>>>>>>>
---------Computed gradient---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Optimization terminated successfully (Exit mode 0)
Final objective value : 1.000000e+00
Final optimality : 3.002483e-15
Final feasibility : 1.110223e-16
Number of major iterations : 2
Number of function evaluations : 2
Number of derivative evaluations : 2
Average Derivative evaluation time : 0.000222 s per evaluation
Average Function evaluation time : 0.000989 s per evaluation
Total Function evaluation time : 0.000443 s [ 5.62%]
Total Derivative evaluation time : 0.001979 s [ 25.07%]
Optimizer time : 0.000843 s [ 10.68%]
Processing time : 0.004627 s [ 58.63%]
Visualization time : 0.000000 s [ 0.00%]
Total optimization time : 0.007892 s [100.00%]
Summary saved to : slsqp_summary.out
[1.]
[1.11022302e-16]
[1.]
/Users//modopt/modopt/external_libraries/csdl/csdl_problem.py:53: UserWarning: This version of CSDL or python_csdl_backend does not support SURF paradigm.
warnings.warn('This version of CSDL or python_csdl_backend does not support SURF paradigm.')
/Users//modopt/modopt/external_libraries/pyslsqp/pyslsqp.py:132: UserWarning: PySLSQP prints the final results by default. To suppress the results, set `solver_options={"iprint":0}`. Check the summary file "slsqp_summary.out" for the summary of optimization.
warnings.warn('PySLSQP prints the final results by default. '