public static void Sample2()
        {
            // See IpoptHowTo.txt how to get Ipopt to work. It's quite easy and Ipopt is very powerful.

            // Variables.
            Variable x1 = new Variable();
            Variable x2 = new Variable();
            Variable x3 = new Variable();
            Variable x4 = new Variable();

            // Objective function and non-linear constraints.
            Function f = x1 * x4 * (x1 + x2 + x3) + x3;
            Function g1 = x1 * x2 * x3 * x4;
            Function g2 = Function.Sqr(x1) + Function.Sqr(x2) + Function.Sqr(x3) + Function.Sqr(x4);

            // Objective function and constrains may be compiled including first and second derivatives.
            //CompiledFunction[] c = Compiler.Compile(new Function[] { f, g1, g2 }, new Variable[] { x1, x2, x3, x4 }, 2);
            //f = c[0];
            //g1 = c[1];
            //g2 = c[2];

            // Prepare the optimizer.
            IpoptOptimizer o = new IpoptOptimizer();
            o.Variables.Add(x1, x2, x3, x4);
            o.ObjectiveFunction = f;
            o.Constraints.Add(g1 >= 25.0);
            o.Constraints.Add(g2 == 40.0);
            o.Constraints.Add(1.0 <= x1, x1 <= 5.0);
            o.Constraints.Add(1.0 <= x2, x2 <= 5.0);
            o.Constraints.Add(1.0 <= x3, x3 <= 5.0);
            o.Constraints.Add(1.0 <= x4, x4 <= 5.0);

            // Verbose mode. Show Ipopt convergence.
            o.PrintLevel = 5;

            // Run optimization. Initial point doesn't have to satisfy the constraints.
            IpoptOptimizerResult or = o.RunIpopt(x1 | 1.0, x2 | 5.0, x3 | 5.0, x4 | 1.0);

            Console.WriteLine(or.ReturnCode);
            Console.WriteLine("x1 = " + or.OptimalPoint[x1]);
            Console.WriteLine("x2 = " + or.OptimalPoint[x2]);
            Console.WriteLine("x3 = " + or.OptimalPoint[x3]);
            Console.WriteLine("x4 = " + or.OptimalPoint[x4]);
            Console.WriteLine("f = " + or.OptimalValue);
            Console.WriteLine("g1 = " + g1.Value(or.OptimalPoint));
            Console.WriteLine("g2 = " + g2.Value(or.OptimalPoint));
        }
        public static VariableAssignment[] Create(Variable[] variables, double[] values)
        {
            int n = variables.Length;

            if (values.Length != n)
            {
                throw new ArgumentException("Number number of variables and the number of values don't agree.");
            }

            List<VariableAssignment> assignments = new List<VariableAssignment>();
            for (int i = 0; i < n; i++)
            {
                assignments.Add(new VariableAssignment(variables[i], values[i]));
            }

            return assignments.ToArray();
        }
Exemple #3
0
        /// <summary>
        /// Computes the partial derivative with respect to a variable.
        /// </summary>
        public Function Derivative(Variable variable)
        {
            // It's has proved very important for efficient computation that the same object is returned
            // if called repeatably. This is especially true if the function is compiled.

            if (derivatives == null)
            {
                derivatives = new Dictionary<Variable, Function>();
            }

            Function derivative;
            if (!derivatives.TryGetValue(variable, out derivative))
            {
                derivative = ComputeDerivative(variable);
                if (!derivative.IsZero)
                {
                    // Only use memory for saving non-zero derivatives.
                    derivatives.Add(variable, derivative);
                }
            }

            return derivative;
        }
        public static void Sample1()
        {
            // Variables. The string names are added for convenience when printing out the optimal point with Point.ToString().
            Variable x = new Variable("x");
            Variable y = new Variable("y");

            // Rosenbrock function http://en.wikipedia.org/wiki/Rosenbrock_function.
            Function f = Function.Sqr(1.0 - x) + 100.0 * Function.Sqr(y - Function.Sqr(x));

            // Use the BFGS optimizer.
            BfgsOptimizer o = new BfgsOptimizer();

            // Specify variables and objective functions and add constraints. Derivatives are computed automatically.
            o.Variables.Add(x, y);
            o.VariableConstraints.Add(x >= 0.0, y >= 0.0);
            o.ObjectiveFunction = f;

            // Start optimizer from a random point.
            Random r = new Random(1);
            IOptimizerResult or = o.Run(x | r.NextDouble(), y | r.NextDouble());

            // Show convergence status and optimal point and value.
            Console.WriteLine(or.Status);
            Console.WriteLine(or.OptimalPoint);
            Console.WriteLine(or.OptimalValue);

            // The result object also contains BFGS convergence status.
            Console.WriteLine(((BfgsOptimizerResult)or).ConvergenceStatus);

            // We may use Prepare if we need to run the optimizer from multiple starting points.
            PreparedOptimizer o2 = o.Prepare();
            for (int i = 0; i < 10; i++)
            {
                IOptimizerResult or2 = o2.Run(x | r.NextDouble(), y | r.NextDouble());
                Console.WriteLine(or2.OptimalPoint);
            }
        }
Exemple #5
0
 protected override Function ComputeDerivative(Variable variable)
 {
     return variable == this ? 1.0 : 0.0;
 }
 public VariableNotAssignedException(Variable variable, string message)
     : base(message)
 {
     Variable = variable;
 }
            protected override Function ComputeDerivative(Variable variable)
            {
                List<Function> derivatives = new List<Function>();
                foreach (Function function in functions)
                {
                    derivatives.Add(function.Derivative(variable));
                }

                return new SumFunction(derivatives.ToArray());
            }
 public VariableEqualityConstraint(Variable variable, double value)
     : base(variable, value, value)
 {
     Value = value;
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return (Function.Sqr(z.re) * z.im.Derivative(variable) - z.im * z.re.Derivative(variable)) / (Function.Sqr(z.re) + Function.Sqr(z.im));
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return new StepDerivativeFunction(f);
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return g * Function.Pow(f, g - 1.0) * f.Derivative(variable) + Function.Log(f) * this * g.Derivative(variable);
 }
Exemple #12
0
            protected override Function ComputeDerivative(Variable variable)
            {
                if (partialEvaluator.Point.ContainsVariable(variable))
                {
                    // This variable is replaced by a constant.
                    return 0.0;
                }

                return innerFunction.Derivative(variable);
            }
Exemple #13
0
 /// <summary>
 /// Computes the partial derivative with respect to a variable. Override this to implement.
 /// </summary>
 protected abstract Function ComputeDerivative(Variable variable);
 protected override Function ComputeDerivative(Variable variable)
 {
     return 0.0;
 }
 public VariableFunctionAssignment(Variable variable, Function function)
 {
     Variable = variable;
     Function = function;
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     // Same derivative as that of the step function.
     return new StepDerivativeFunction(f);
 }
            protected override Function ComputeDerivative(Variable variable)
            {
                if (sin == null)
                {
                    sin = new SinFunction(f, this);
                }

                return -sin * f.Derivative(variable);
            }
 protected override Function ComputeDerivative(Variable variable)
 {
     return 2.0 * f * f.Derivative(variable);
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return a * Function.Pow(f, a - 1.0) * f.Derivative(variable);
 }
 public ComplexFunction Derivative(Variable variable)
 {
     // Currently no support for anything like a ComplexVariable. How to implement this?
     return new ComplexFunction(re.Derivative(variable), im.Derivative(variable));
 }
            protected override Function ComputeDerivative(Variable variable)
            {
                if (cos == null)
                {
                    cos = new CosFunction(f, this);
                }

                return cos * f.Derivative(variable);
            }
 protected override Function ComputeDerivative(Variable variable)
 {
     // As far as not being defined at zero, all derivatives are the same.
     return this;
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     // Reuse the same object here.
     return 0.5 / this * f.Derivative(variable);
 }
Exemple #24
0
        /// <summary>
        /// Computes the higher order partial derivative with respect to a variable.
        /// </summary>
        public Function Derivative(Variable variable, int order)
        {
            if (order < 0)
            {
                throw new ArgumentOutOfRangeException("order");
            }

            Function f = this;
            for (int i = 0; i < order; i++)
            {
                f = f.Derivative(variable);
            }
            return f;
        }
 public ComplexFunction Derivative(Variable variable, int order)
 {
     return new ComplexFunction(re.Derivative(variable, order), im.Derivative(variable, order));
 }
 //: base(variable, value)
 public VariableAssignment(Variable variable, double value)
 {
     Variable = variable;
     Value = value;
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return ComplexFunction.Re(0.5 * new ComplexFunction(ComplexFunction.Re(z).Derivative(variable), ComplexFunction.Im(z).Derivative(variable)) / z05);
 }
 public VariableNotAssignedException(Variable variable)
     : this(variable, null)
 {
 }
 public VariableConstraint(Variable variable, double minValue, double maxValue)
     : base(variable, minValue, maxValue)
 {
     Variable = variable;
 }
 protected override Function ComputeDerivative(Variable variable)
 {
     return a * -f.Derivative(variable) / Function.Sqr(f);
 }