Esempio n. 1
0
    def solve(self):
        """Solve the optimization problem and return the optimized parameters."""

        if self.problem.constraints is None:
            num_equality_constraints = 0
            num_inequality_constraints = 0 + len(
                self.bound_inequality_constraints)
        else:
            num_equality_constraints = self.problem.constraints.equality_constraints(
            )._get_constraint_dim()
            num_inequality_constraints = self.problem.constraints.inequality_constraints(
            )._get_constraint_dim() + len(self.bound_inequality_constraints)

        # No constraints
        if num_equality_constraints == 0 and num_inequality_constraints == 0:
            Optizelle.Unconstrained.Algorithms.getMin(DolfinVectorSpace,
                                                      Optizelle.Messaging(),
                                                      self.fns, self.state)

        # Equality constraints only
        elif num_equality_constraints > 0 and num_inequality_constraints == 0:
            Optizelle.EqualityConstrained.Algorithms.getMin(
                DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(),
                self.fns, self.state)

        # Inequality constraints only
        elif num_equality_constraints == 0 and num_inequality_constraints > 0:
            Optizelle.InequalityConstrained.Algorithms.getMin(
                DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(),
                self.fns, self.state)

        # Inequality and equality constraints
        else:
            Optizelle.Constrained.Algorithms.getMin(DolfinVectorSpace,
                                                    DolfinVectorSpace,
                                                    DolfinVectorSpace,
                                                    Optizelle.Messaging(),
                                                    self.fns, self.state)

        # Print out the reason for convergence
        # FIXME: Use logging
        print("The algorithm stopped due to: %s" %
              (Optizelle.StoppingCondition.to_string(self.state.opt_stop)))

        # Return the optimal control
        list_type = self.problem.reduced_functional.controls
        return delist(self.state.x, list_type)
Esempio n. 2
0
import Optizelle
#---Import0---
import Optizelle.EqualityConstrained.State
import Optizelle.EqualityConstrained.Functions
import Optizelle.EqualityConstrained.Algorithms
import Optizelle.EqualityConstrained.Restart
import Optizelle.json.EqualityConstrained
#---Import1---
import numpy
import math

# Create some type shortcuts
XX = Optizelle.Rm
YY = Optizelle.Rm
msg = Optizelle.Messaging()

# Create some arbitrary vector in R^2
x = numpy.array([1.2, 2.3])
x0 = numpy.array([2.3, 1.2])

# Create a different arbitrary vector in R^3
y = numpy.array([3.4, 4.5, 5.6])
y0 = numpy.array([5.6, 4.5, 3.4])

# Create a state based on this vector
#---State0---
state = Optizelle.EqualityConstrained.State.t(XX, YY, msg, x, y)
#---State1---

# Read in some parameters
Esempio n. 3
0
if len(sys.argv) != 2:
    sys.exit("simple_constrained.py <parameters>")
fname = sys.argv[1]

# Generate an initial guess
x = numpy.array([2.1, 1.1])

# Allocate memory for the equality multiplier
y = numpy.array([0.])

# Allocate memory for the inequality multiplier
z = numpy.array([0.])

# Create an optimization state
state = Optizelle.Constrained.State.t(Optizelle.Rm, Optizelle.Rm, Optizelle.Rm,
                                      Optizelle.Messaging(), x, y, z)

# Read the parameters from file
Optizelle.json.Constrained.read(Optizelle.Rm, Optizelle.Rm, Optizelle.Rm,
                                Optizelle.Messaging(), fname, state)

# Create a bundle of functions
fns = Optizelle.Constrained.Functions.t()
fns.f = MyObj()
fns.g = MyEq()
fns.h = MyIneq()

# Solve the optimization problem
Optizelle.Constrained.Algorithms.getMin(Optizelle.Rm,
                                        Optizelle.Rm, Optizelle.Rm,
                                        Optizelle.Messaging(), fns, state)
Esempio n. 4
0
# Read in the name for the input file
if len(sys.argv) != 2:
    sys.exit("simple_equality.py <parameters>")
fname = sys.argv[1]

#---State0---
# Generate an initial guess
x = numpy.array([2.1, 1.1])

# Allocate memory for the equality multiplier
y = numpy.array([0.])

# Create an optimization state
state = Optizelle.EqualityConstrained.State.t(Optizelle.Rm, Optizelle.Rm,
                                              Optizelle.Messaging(), x, y)
#---State1---

#---Parameters0---
# Read the parameters from file
Optizelle.json.EqualityConstrained.read(Optizelle.Rm, Optizelle.Rm,
                                        Optizelle.Messaging(), fname, state)
#---Parameters1---

#---Functions0---
# Create a bundle of functions
fns = Optizelle.EqualityConstrained.Functions.t()
fns.f = MyObj()
fns.g = MyEq()
fns.PSchur_left = MyPrecon()
#---Functions1---
Esempio n. 5
0
    def __build_optizelle_state(self):

        # Optizelle does not support maximization problem directly,
        # hence we negate the functional instead
        if isinstance(self.problem, MaximizationProblem):
            scale = -1
        else:
            scale = +1

        bound_inequality_constraints = []
        if self.problem.bounds is not None:
            # We need to process the damn bounds
            for (control,
                 bound) in zip(self.problem.reduced_functional.controls,
                               self.problem.bounds):
                (lb, ub) = bound

                if lb is not None:
                    bound_inequality_constraints.append(
                        OptizelleBoundConstraint(control.data(), lb, 'lower'))

                if ub is not None:
                    bound_inequality_constraints.append(
                        OptizelleBoundConstraint(control.data(), ub, 'upper'))

        self.bound_inequality_constraints = bound_inequality_constraints

        # Create the appropriate Optizelle state, taking into account which
        # type of constraints we have (unconstrained, (in)-equality constraints).
        if self.problem.constraints is None:
            num_equality_constraints = 0
            num_inequality_constraints = 0 + len(bound_inequality_constraints)
        else:
            num_equality_constraints = self.problem.constraints.equality_constraints(
            )._get_constraint_dim()
            num_inequality_constraints = self.problem.constraints.inequality_constraints(
            )._get_constraint_dim() + len(bound_inequality_constraints)

        x = [p.data() for p in self.problem.reduced_functional.controls]

        # Unconstrained case
        if num_equality_constraints == 0 and num_inequality_constraints == 0:
            self.state = Optizelle.Unconstrained.State.t(
                DolfinVectorSpace, Optizelle.Messaging(), x)
            self.fns = Optizelle.Unconstrained.Functions.t()
            self.fns.f = OptizelleObjective(self.problem.reduced_functional,
                                            scale=scale)

            log(INFO, "Found no constraints.")

        # Equality constraints only
        elif num_equality_constraints > 0 and num_inequality_constraints == 0:

            # Allocate memory for the equality multiplier
            equality_constraints = self.problem.constraints.equality_constraints(
            )
            y = equality_constraints.output_workspace()

            self.state = Optizelle.EqualityConstrained.State.t(
                DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), x,
                y)
            self.fns = Optizelle.Constrained.Functions.t()

            self.fns.f = OptizelleObjective(self.problem.reduced_functional,
                                            scale=scale)
            self.fns.g = OptizelleConstraints(self.problem,
                                              equality_constraints)

            log(
                INFO, "Found no equality and %i inequality constraints." %
                equality_constraints._get_constraint_dim())

        # Inequality constraints only
        elif num_equality_constraints == 0 and num_inequality_constraints > 0:

            # Allocate memory for the inequality multiplier
            if self.problem.constraints is not None:
                inequality_constraints = self.problem.constraints.inequality_constraints(
                )
                all_inequality_constraints = constraints.MergedConstraints(
                    inequality_constraints.constraints +
                    bound_inequality_constraints)
            else:
                all_inequality_constraints = constraints.MergedConstraints(
                    bound_inequality_constraints)
            z = all_inequality_constraints.output_workspace()

            self.state = Optizelle.InequalityConstrained.State.t(
                DolfinVectorSpace, DolfinVectorSpace, Optizelle.Messaging(), x,
                z)
            self.fns = Optizelle.InequalityConstrained.Functions.t()

            self.fns.f = OptizelleObjective(self.problem.reduced_functional,
                                            scale=scale)
            self.fns.h = OptizelleConstraints(self.problem,
                                              all_inequality_constraints)

            log(
                INFO, "Found no equality and %i inequality constraints." %
                all_inequality_constraints._get_constraint_dim())

        # Inequality and equality constraints
        else:
            # Allocate memory for the equality multiplier
            equality_constraints = self.problem.constraints.equality_constraints(
            )
            y = equality_constraints.output_workspace()

            # Allocate memory for the inequality multiplier
            if self.problem.constraints is not None:
                inequality_constraints = self.problem.constraints.inequality_constraints(
                )
                all_inequality_constraints = constraints.MergedConstraints(
                    inequality_constraints.constraints +
                    bound_inequality_constraints)
            else:
                all_inequality_constraints = constraints.MergedConstraints(
                    bound_inequality_constraints)
            z = all_inequality_constraints.output_workspace()

            self.state = Optizelle.Constrained.State.t(DolfinVectorSpace,
                                                       DolfinVectorSpace,
                                                       DolfinVectorSpace,
                                                       Optizelle.Messaging(),
                                                       x, y, z)
            self.fns = Optizelle.Constrained.Functions.t()

            self.fns.f = OptizelleObjective(self.problem.reduced_functional,
                                            scale=scale)
            self.fns.g = OptizelleConstraints(self.problem,
                                              equality_constraints)
            self.fns.h = OptizelleConstraints(self.problem,
                                              all_inequality_constraints)

            log(
                INFO, "Found %i equality and %i inequality constraints." %
                (equality_constraints._get_constraint_dim(),
                 all_inequality_constraints._get_constraint_dim()))

        # Set solver parameters
        self.__set_optizelle_parameters()
Esempio n. 6
0
rname = sys.argv[2] if len(sys.argv) == 3 else ""

# Generate an initial guess for Rosenbrock
x = array.array('d', [-1.2, 1.0])

# Create an unconstrained state based on this vector
state = Optizelle.Unconstrained.State.t(MyVS, MyMessaging(), x)

#---ReadRestart0---
# If we have a restart file, read in the parameters
if len(sys.argv) == 3:
    Optizelle.json.Unconstrained.read_restart(MyVS, MyMessaging(), rname, x,
                                              state)

# Read additional parameters from file
Optizelle.json.Unconstrained.read(MyVS, Optizelle.Messaging(), pname, state)
#---ReadRestart1---

# Create the bundle of functions
fns = Optizelle.Unconstrained.Functions.t()
fns.f = Rosenbrock()
fns.PH = RosenHInv()

#---Solver0---
# Solve the optimization problem
Optizelle.Unconstrained.Algorithms.getMin(MyVS, MyMessaging(), fns, state,
                                          MyRestartManipulator())
#---Solver1---

# Print out the reason for convergence
print("The algorithm converged due to: %s" %
Esempio n. 7
0
    # z=(g''(x)dx)*dy
    def pps(self,x,dx,dy,z):
        z[0] = ((-cos(x[0])*dx[0]*sin(x[1])-sin(x[0])*cos(x[1])*dx[1])*dy[0]
               +(6.*dx[0]*x[1] + 6.*x[0]*dx[1])*dy[1]
               +(-1./sq(x[0])*dx[0])*dy[2])
        z[1] = ((-sin(x[0])*dx[0]*cos(x[1])-cos(x[0])*sin(x[1])*dx[1])*dy[0]
               +(6.*x[0]*dx[0]+6.*x[1]*dx[1])*dy[1]
               +(60.*cub(x[1])*dx[1])*dy[2])

# Allocate memory for an initial guess and equality multiplier 
x = numpy.array([1.2,2.3])
y = numpy.zeros(3)

# Create an optimization state
state=Optizelle.EqualityConstrained.State.t(
    Optizelle.Rm,Optizelle.Rm,Optizelle.Messaging(),x,y)

# Modify the state so that we just run our diagnostics and exit
state.dscheme = Optizelle.DiagnosticScheme.DiagnosticsOnly;
state.f_diag = Optizelle.FunctionDiagnostics.SecondOrder;
state.g_diag = Optizelle.FunctionDiagnostics.SecondOrder;

# Create a bundle of functions
fns=Optizelle.EqualityConstrained.Functions.t()
fns.f=Rosenbrock()
fns.g=Utility()

# Even though this looks like we're solving an optimization problem,
# we're actually just going to run our diagnostics and then exit.
Optizelle.EqualityConstrained.Algorithms.getMin(
    Optizelle.Rm,Optizelle.Rm,Optizelle.Messaging(),fns,state)
Esempio n. 8
0
        result[0]=one_over_det*(200.*dx[0]+400.*x[0]*dx[1])
        result[1]=(one_over_det*
            (400.*x[0]*dx[0]+(1200.*x[0]*x[0]-400.*x[1]+2.)*dx[1]))
#---Preconditioner1---
    
# Read in the name for the input file
if len(sys.argv)!=2:
    sys.exit("python rosenbrock.py <parameters>")
fname = sys.argv[1]

#---State0---
# Generate an initial guess for Rosenbrock
x = numpy.array([-1.2,1.0])

# Create an unconstrained state based on this vector
state=Optizelle.Unconstrained.State.t(Optizelle.Rm,Optizelle.Messaging(),x)
#---State1---
    
#---Parameters0---
# Read the parameters from file
Optizelle.json.Unconstrained.read(Optizelle.Rm,Optizelle.Messaging(),
    fname,state)
#---Parameters1---

#---Functions0---
# Create the bundle of functions 
fns=Optizelle.Unconstrained.Functions.t()
fns.f=Rosenbrock()
fns.PH=RosenHInv()
#---Functions1---