Minimize
x1*x1 + x2*x2
Subject to:
x1 + x2 = 1
x1 * x2 >= 0
0 <= x1, x2 <= 1
using the Solver Platform SDK with its MATLAB Object-Oriented API. Click the links below to see example MATLAB code written using the SDK's other APIs.
To solve a constrained nonlinear optimization problem using the Object-Oriented API, you must write a MATLAB function (called an Evaluator in the terminology of the SDK) that computes values for the objective and constraints. You can access a wide range of information about the problem and the current state of the solution in this Evaluator, and signal whether the SDK should continue or terminate the solution process early. The MATLAB code below closely resembles the code you'd write to solve the same problem in C++, C# or Java.
function ret = NLPEvaluator(evaluator)
var = evaluator.Problem.VarDecision.Value;
fcn = evaluator.Problem.FcnConstraint.Value;
idx = evaluator.Problem.ObjectiveIndex;
fcn(1) = var(1) + var(2); % x1 + x1 = 1
fcn(2) = var(1) * var(2); % x1 * x2 >= 0
evaluator.Problem.FcnConstraint.Value = fcn;
evaluator.Problem.FcnObjective.Value(idx) =
var(1)*var(1) + var(2)*var(2); % objective
ret = 'Continue';
end
You create a Problem object, and add this Evaluator to the Problem object's collection of Evaluators. In this example, we set properties to specify the lower and upper bounds we need for the constraints, thereby defining both inequalities and equalities. The SDK offers several ways to specify constraints -- another way is shown in the Procedural API example code.
For any type of optimization problem, you simply call Optimize. All errors are reported by raising exceptions, which will cause the catch clause to be executed.
try
problem = Problem('Maximize', 2, 2);
problem.FcnConstraint.UpperBound = [ 1 1E+30 ];
problem.FcnConstraint.LowerBound = [ 1 0 ];
problem.VarDecision.UpperBound = [ 1 1 ];
problem.VarDecision.LowerBound = [ 0 0 ];
problem.ProblemType = 'OptNLP';
problem.Evaluators('Function').Evaluator = 'NLPEvaluator';
Optimize(problem.Solver); % solve problem
x = problem.VarDecision.FinalValue;
catch
errstr = lasterror;
disp(errstr.message);
end
if isa(problem, 'Problem')
Destroy(problem);
end
For further information and an example of a mixed-integer linear programming problem written in the SDK's Object-Oriented API for MATLAB, C++, C#, Visual Basic, VB.NET, and Java, click Solver Platform SDK Language/OS Support.