8. Optimization with CSDL_alpha 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}\]
import csdl_alpha as csdl

# minimize x^4 + y^4 subject to x>=0, x+y=1, x-y>=1.

rec = csdl.Recorder()
rec.start()

# add design variables
x = csdl.Variable(name = 'x', value=1.)
y = csdl.Variable(name = 'y', value=1.)
x.set_as_design_variable(lower = 0.0)
y.set_as_design_variable(scaler=10.)

# add objective
z = x**4 + y**4
z.add_name('z')
z.set_as_objective(scaler=5.0)

# add constraints
constraint_1 = x + y
constraint_2 = x - y
constraint_1.add_name('constraint_1')
constraint_2.add_name('constraint_2')
constraint_1.set_as_constraint(lower=1., upper=1.)
constraint_2.set_as_constraint(lower=1.)

rec.stop()

# Create a Simulator object from the Recorder object
sim = csdl.experimental.PySimulator(rec)

# Import CSDLAlphaProblem from modopt
from modopt import CSDLAlphaProblem

# Instantiate your problem using the csdl Simulator object and name your problem
prob = CSDLAlphaProblem(problem_name='quartic',simulator=sim)

# Import your preferred optimizer from modopt
from modopt import SQP, SLSQP, SNOPT

# Setup your preferred optimizer (here, SLSQP) with the Problem object 
# Pass in the options for your chosen optimizer
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)

# Solve your optimization problem
optimizer.solve()

# Print the results of optimization
optimizer.print_results()
/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")
Directory  quartic_outputs  already exists
Computing gradient >>>>>>>>>>
---------Computed gradient---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------

----------------------------------------------------------------------------
Derivative type | Calc norm  | FD norm    | Abs error norm | Rel error norm 
----------------------------------------------------------------------------

Gradient        | 2.0100e+01 | 2.0100e+01 | 3.0000e-05     | 1.4925e-06    
Jacobian        | 1.4213e+00 | 1.4213e+00 | 2.8262e-10     | 1.9885e-10    
----------------------------------------------------------------------------

Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing gradient >>>>>>>>>>
---------Computed gradient---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Computing objective >>>>>>>>>>
---------Computed objective---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing constraints >>>>>>>>>>
---------Computed constraints---------
Computing gradient >>>>>>>>>>
---------Computed gradient---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------
Computing Jacobian >>>>>>>>>>
---------Computed Jacobian---------

 	 ==============
	 Scipy summary:
	 ==============
	 Problem                    : quartic
	 Solver                     : scipy_slsqp
	 Success                    : True
	 Message                    : Optimization terminated successfully
	 Objective                  : 5.0000000000003375
	 Gradient norm              : 20.000000000001013
	 Total time                 : 0.004601955413818359
	 Major iterations           : 2
	 Total function evals       : 2
	 Total gradient evals       : 2
	 ==================================================