CSDL (deprecated)

Define a problem in CSDL

This example does not intend to cover the features of CSDL. For more details and tutorials on CSDL, please refer to CSDL documentation. In this example, we solve a constrained problem given by

\[ \begin{align}\begin{aligned} \underset{x_1, x_2 \in \mathbb{R}}{\text{minimize}} \quad x_1^2 + x_2^2\\\newline \text{subject to} \quad x_1 \geq 0 \newline \quad \quad \quad \quad x_1 + x_2 = 1 \newline \quad \quad \quad \quad x_1 - x_2 \geq 1 \end{aligned}\end{align} \]

We know the solution of this problem is \(x_1=1\), and \(x_2=0\). However, we start from an initial guess of \(x_1=0\), and \(x_2=0.0\) for the purposes of this tutorial.

The problem model is written in CSDL as follows:

from csdl import Model

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

class QuadraticFunc(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**2 + y**2

        # 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.)

Once your model is setup in CSDL, create a Simulator object in CSDL and translate the Simulator object to a CSDLProblem object in modOpt.

from csdl_om import Simulator

# Create a Simulator object for your model
sim = Simulator(QuadraticFunc())

from modopt import CSDLProblem

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

Solve your problem using an optimizer

Once your problem is translated to modOpt, import your preferred optimizer from the respective library in modOpt and solve it, following the standard procedure. Here we will use the SLSQP optimizer from the scipy library.

from modopt import SLSQP

# Setup your preferred optimizer (SLSQP) with the Problem object 
# Pass in the options for your chosen optimizer
optimizer = SLSQP(prob, solver_options={'maxiter':20})

# Check first derivatives at the initial guess, if needed
optimizer.check_first_derivatives(prob.x0)

# Solve your optimization problem
optimizer.solve()

# Print results of optimization
optimizer.print_results()

Scaling API

Please refer to the code snippet below as a guide for scaling the design variables, objective, and constraints independent of their definitions.

Warning

The results provided by the optimizer will always be scaled, while the values from the models will remain unscaled.

        # define optimization problem
        self.add_design_variable('x', lower=0., scaler=2.)
        self.add_design_variable('y')
        self.add_objective('z', scaler=5.)
        self.add_constraint('constraint_1', equals=1., scaler=10.)
        self.add_constraint('constraint_2', lower=1., scaler=100.)