/// <summary> /// Optional function for setting scaling parameter for the NLP. /// This corresponds to the get_scaling_parameters method in TNLP. /// If the pointers x_scaling or g_scaling are null, then no scaling /// for x resp. g is done. /// </summary> /// <param name="obj_scaling">Scaling of the objective function</param> /// <param name="x_scaling">Scaling of the problem variables</param> /// <param name="g_scaling">Scaling of the constraint functions</param> /// <returns>true if scaling succeeded, false otherwise</returns> public bool SetScaling(double obj_scaling, double[] x_scaling, double[] g_scaling) { return(IsInitialized && AddOption("nlp_scaling_method", "user-scaling") && IpoptAdapter.SetIpoptProblemScaling(m_problem, obj_scaling, x_scaling, g_scaling) == IpoptBoolType.True); }
public void IpoptSolve_SpecifiedStartGuess_YieldsExpectedOptimalVariables() { var actual = new[] { 10.0, 10.0, 10.0 }; double obj; var expected = new[] { 24.0, 12.0, 12.0 }; IpoptAdapter.IpoptSolve(_instance, actual, null, out obj, null, null, null, IntPtr.Zero); CollectionAssert.AreEqual(expected, actual, new DoubleComparer(1.0e-5)); }
public void IpoptSolve_SpecifiedStartGuess_ReturnsSucceededStatus() { var x = new[] { 10.0, 10.0, 10.0 }; double obj; const IpoptReturnCode expected = IpoptReturnCode.Solve_Succeeded; var actual = IpoptAdapter.IpoptSolve(_instance, x, null, out obj, null, null, null, IntPtr.Zero); Assert.AreEqual(expected, actual); }
public void Setup() { _hs037 = new HS037(); _instance = IpoptAdapter.CreateIpoptProblem(_hs037._n, _hs037._x_L, _hs037._x_U, _hs037._m, _hs037._g_L, _hs037._g_U, _hs037._nele_jac, _hs037._nele_hess, IpoptIndexStyle.C, _hs037.eval_f, _hs037.eval_g, _hs037.eval_grad_f, _hs037.eval_jac_g, _hs037.eval_h); IpoptAdapter.AddIpoptStrOption(_instance, "hessian_approximation", "limited-memory"); IpoptAdapter.AddIpoptIntOption(_instance, "limited_memory_max_history", 5); }
public IpoptReturnCode SolveProblem(double[] x, out double obj_val, double[] g, double[] mult_g, double[] mult_x_L, double[] mult_x_U) #endif { if (!IsInitialized) { obj_val = PositiveInfinity; return(IpoptReturnCode.Problem_Not_Initialized); } return(IpoptAdapter.IpoptSolve(m_problem, x, g, out obj_val, mult_g, mult_x_L, mult_x_U, IntPtr.Zero)); }
public void IpoptSolve_SpecifiedStartGuess_YieldsExpectedOptimalObjectiveValue() { const double expected = -3456.0; var x = new[] { 10.0, 10.0, 10.0 }; double actual; IpoptAdapter.IpoptSolve(_instance, x, null, out actual, null, null, null, IntPtr.Zero); Assert.AreEqual(expected, actual, 1.0e-3); }
/// <summary> /// Constructor for creating a new Ipopt Problem object using native /// function delegates. This function /// initializes an object that can be passed to the IpoptSolve call. It /// contains the basic definition of the optimization problem, such /// as number of variables and constraints, bounds on variables and /// constraints, information about the derivatives, and the callback /// function for the computation of the optimization problem /// functions and derivatives. During this call, the options file /// ipopt.opt is read as well. /// </summary> /// <param name="n">Number of optimization variables</param> /// <param name="x_L">Lower bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="x_U">Upper bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="m">Number of constraints.</param> /// <param name="g_L">Lower bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="g_U">Upper bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="nele_jac">Number of non-zero elements in constraint Jacobian.</param> /// <param name="nele_hess">Number of non-zero elements in Hessian of Lagrangian.</param> /// <param name="eval_f_cb">Native callback function for evaluating objective function</param> /// <param name="eval_g_cb">Native callback function for evaluating constraint functions</param> /// <param name="eval_grad_f_cb">Native callback function for evaluating gradient of objective function</param> /// <param name="eval_jac_g_cb">Native callback function for evaluating Jacobian of constraint functions</param> /// <param name="eval_h_cb">Native callback function for evaluating Hessian of Lagrangian function</param> public IpoptProblem(int n, double[] x_L, double[] x_U, int m, double[] g_L, double[] g_U, int nele_jac, int nele_hess, Eval_F_CB eval_f_cb, Eval_G_CB eval_g_cb, Eval_Grad_F_CB eval_grad_f_cb, Eval_Jac_G_CB eval_jac_g_cb, Eval_H_CB eval_h_cb) { m_eval_f_cb = eval_f_cb; m_eval_g_cb = eval_g_cb; m_eval_grad_f_cb = eval_grad_f_cb; m_eval_jac_g_cb = eval_jac_g_cb; m_eval_h_cb = eval_h_cb; m_intermediate_cb = null; m_problem = IpoptAdapter.CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess, IpoptIndexStyle.C, m_eval_f_cb, m_eval_g_cb, m_eval_grad_f_cb, m_eval_jac_g_cb, m_eval_h_cb); m_disposed = false; }
public void SetIntermediateCallback_CallbackFunctionDefined_CallbackFunctionCalled() { const bool expected = true; IpoptAdapter.SetIntermediateCallback(_instance, _hs037.intermediate); _hs037.hasIntermediateBeenCalled = false; var x = new[] { 10.0, 10.0, 10.0 }; double obj; IpoptAdapter.IpoptSolve(_instance, x, null, out obj, null, null, null, IntPtr.Zero); var actual = _hs037.hasIntermediateBeenCalled; Assert.AreEqual(expected, actual); }
/// <summary> /// Constructor for creating a new Ipopt Problem object using managed /// function delegates. This function /// initializes an object that can be passed to the IpoptSolve call. It /// contains the basic definition of the optimization problem, such /// as number of variables and constraints, bounds on variables and /// constraints, information about the derivatives, and the callback /// function for the computation of the optimization problem /// functions and derivatives. During this call, the options file /// ipopt.opt is read as well. /// </summary> /// <param name="n">Number of optimization variables</param> /// <param name="x_L">Lower bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="x_U">Upper bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="m">Number of constraints.</param> /// <param name="g_L">Lower bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="g_U">Upper bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="nele_jac">Number of non-zero elements in constraint Jacobian.</param> /// <param name="nele_hess">Number of non-zero elements in Hessian of Lagrangian.</param> /// <param name="eval_f_cb">Managed callback function for evaluating objective function</param> /// <param name="eval_g_cb">Managed callback function for evaluating constraint functions</param> /// <param name="eval_grad_f_cb">Managed callback function for evaluating gradient of objective function</param> /// <param name="eval_jac_g_cb">Managed callback function for evaluating Jacobian of constraint functions</param> /// <param name="eval_h_cb">Managed callback function for evaluating Hessian of Lagrangian function</param> public IpoptProblem(int n, double[] x_L, double[] x_U, int m, double[] g_L, double[] g_U, int nele_jac, int nele_hess, EvaluateObjectiveDelegate eval_f_cb, EvaluateConstraintsDelegate eval_g_cb, EvaluateObjectiveGradientDelegate eval_grad_f_cb, EvaluateJacobianDelegate eval_jac_g_cb, EvaluateHessianDelegate eval_h_cb) { m_eval_f_cb = new ObjectiveEvaluator(eval_f_cb).Evaluate; m_eval_g_cb = new ConstraintsEvaluator(eval_g_cb).Evaluate; m_eval_grad_f_cb = new ObjectiveGradientEvaluator(eval_grad_f_cb).Evaluate; m_eval_jac_g_cb = new JacobianEvaluator(eval_jac_g_cb).Evaluate; m_eval_h_cb = new HessianEvaluator(eval_h_cb).Evaluate; m_intermediate_cb = null; m_problem = IpoptAdapter.CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess, IpoptIndexStyle.C, m_eval_f_cb, m_eval_g_cb, m_eval_grad_f_cb, m_eval_jac_g_cb, m_eval_h_cb); m_disposed = false; }
/// <summary> /// Dispose(bool disposing) executes in two distinct scenarios. /// If disposing equals true, the method has been called directly /// or indirectly by a user's code. Managed and unmanaged resources /// can be disposed. /// If disposing equals false, the method has been called by the /// runtime from inside the finalizer and you should not reference /// other objects. Only unmanaged resources can be disposed. /// </summary> /// <param name="disposing">true if Dispose method is explicitly called, false otherwise.</param> protected virtual void Dispose(bool disposing) { if (!m_disposed) { if (m_problem != IntPtr.Zero) { IpoptAdapter.FreeIpoptProblem(m_problem); } if (disposing) { m_problem = IntPtr.Zero; } m_disposed = true; } }
/// <summary> /// Constructor for creating a subclassed Ipopt Problem object using managed or /// native function delegates. This is the preferred constructor when /// subclassing IpoptProblem. Prerequisite is that the managed/native optimization /// function delegates are implemented in the inheriting class. /// This function /// initializes an object that can be passed to the IpoptSolve call. It /// contains the basic definition of the optimization problem, such /// as number of variables and constraints, bounds on variables and /// constraints, information about the derivatives, and the callback /// function for the computation of the optimization problem /// functions and derivatives. During this call, the options file /// ipopt.opt is read as well. /// </summary> /// <param name="n">Number of optimization variables</param> /// <param name="x_L">Lower bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="x_U">Upper bounds on variables. This array of size n is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="m">Number of constraints.</param> /// <param name="g_L">Lower bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// less or equal than the number specified by option 'nlp_lower_bound_inf' is interpreted to be minus infinity.</param> /// <param name="g_U">Upper bounds on constraints. This array of size m is copied internally, so that the /// caller can change the incoming data after return without that IpoptProblem is modified. Any value /// greater or equal than the number specified by option 'nlp_upper_bound_inf' is interpreted to be plus infinity.</param> /// <param name="nele_jac">Number of non-zero elements in constraint Jacobian.</param> /// <param name="nele_hess">Number of non-zero elements in Hessian of Lagrangian.</param> /// <param name="useNativeCallbackFunctions">If set to true, native callback functions are used to setup /// the Ipopt problem; if set to false, managed callback functions are used.</param> /// <param name="useHessianApproximation">If set to true, the Ipopt optimizer creates a limited memory /// Hessian approximation and the eval_h (managed or native) method need not be implemented. /// If set to false, an exact Hessian should be evaluated using the appropriate Hessian evaluation method.</param> /// <param name="useIntermediateCallback">If set to true, the intermediate method (managed or native) will be called /// after each full iteration. If false, the intermediate callback function will not be called.</param> protected IpoptProblem(int n, double[] x_L, double[] x_U, int m, double[] g_L, double[] g_U, int nele_jac, int nele_hess, bool useNativeCallbackFunctions = false, bool useHessianApproximation = false, bool useIntermediateCallback = false) { if (useNativeCallbackFunctions) { m_eval_f_cb = eval_f; m_eval_g_cb = eval_g; m_eval_grad_f_cb = eval_grad_f; m_eval_jac_g_cb = eval_jac_g; m_eval_h_cb = eval_h; } else { m_eval_f_cb = new ObjectiveEvaluator(eval_f).Evaluate; m_eval_g_cb = new ConstraintsEvaluator(eval_g).Evaluate; m_eval_grad_f_cb = new ObjectiveGradientEvaluator(eval_grad_f).Evaluate; m_eval_jac_g_cb = new JacobianEvaluator(eval_jac_g).Evaluate; m_eval_h_cb = new HessianEvaluator(eval_h).Evaluate; } m_intermediate_cb = null; m_problem = IpoptAdapter.CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess, IpoptIndexStyle.C, m_eval_f_cb, m_eval_g_cb, m_eval_grad_f_cb, m_eval_jac_g_cb, m_eval_h_cb); if (useHessianApproximation) { AddOption("hessian_approximation", "limited-memory"); } if (useIntermediateCallback) { if (useNativeCallbackFunctions) { SetIntermediateCallback((Intermediate_CB)intermediate); } else { SetIntermediateCallback((IntermediateDelegate)intermediate); } } m_disposed = false; }
/// <summary> /// Setting a callback function for the "intermediate callback" /// method in the optimizer. This gives control back to the user once /// per iteration. If set, it provides the user with some /// information on the state of the optimization. This can be used /// to print some user-defined output. It also gives the user a way /// to terminate the optimization prematurely. If the callback /// method returns false, Ipopt will terminate the optimization. /// Calling this set method to set the CB pointer to null disables /// the intermediate callback functionality. /// </summary> /// <param name="intermediate_cb">Native intermediate callback function</param> /// <returns>true if the callback function could be set successfully, false otherwise</returns> public bool SetIntermediateCallback(Intermediate_CB intermediate_cb) { return(IsInitialized && IpoptAdapter.SetIntermediateCallback(m_problem, m_intermediate_cb = intermediate_cb) == IpoptBoolType.True); }
/// <summary> /// Setting a callback function for the "intermediate callback" /// method in the optimizer. This gives control back to the user once /// per iteration. If set, it provides the user with some /// information on the state of the optimization. This can be used /// to print some user-defined output. It also gives the user a way /// to terminate the optimization prematurely. If the callback /// method returns false, Ipopt will terminate the optimization. /// Calling this set method to set the CB pointer to null disables /// the intermediate callback functionality. /// </summary> /// <param name="intermediate_cb">Managed intermediate callback function</param> /// <returns>true if the callback function could be set successfully, false otherwise</returns> public bool SetIntermediateCallback(IntermediateDelegate intermediate_cb) { return(IsInitialized && IpoptAdapter.SetIntermediateCallback( m_problem, m_intermediate_cb = new IntermediateReporter(intermediate_cb).Report) == IpoptBoolType.True); }
/// <summary> /// Method for opening an output file for a given name with given print level. /// </summary> /// <param name="file_name">Name of output file</param> /// <param name="print_level">Level of printed information</param> /// <returns>False, if there was a problem opening the file.</returns> public bool OpenOutputFile(string file_name, int print_level) { return(IsInitialized && IpoptAdapter.OpenIpoptOutputFile(m_problem, file_name, print_level) == IpoptBoolType.True); }
/// <summary> /// Function for adding an integer option. /// </summary> /// <param name="keyword">Name of option</param> /// <param name="val">Integer value of option</param> /// <returns>true if setting option succeeded, false if the option could not be set (e.g., if keyword is unknown)</returns> public bool AddOption(string keyword, int val) { return(IsInitialized && IpoptAdapter.AddIpoptIntOption(m_problem, keyword, val) == IpoptBoolType.True); }
public void Teardown() { IpoptAdapter.FreeIpoptProblem(_instance); _hs037 = null; }