Main Content

optimvalues

Create values for optimization problem

Description

example

val = optimvalues(prob,dataname1,dataval1,...) creates an OptimizationValues object for the problem prob. Specify all variable names and their associated values, and optionally objective or constraint values, by using name-value arguments. For example, to specify that x takes odd values from 1 through 99,

val = optimvalues(prob,x=1:2:99);

Use val as an initial point or initial population for prob.

Examples

collapse all

To create initial points for ga (genetic algorithm solver) in the problem-based approach, create an OptimizationValues object using optimvalues.

Create optimization variables for a 2-D problem with Rosenbrock's function as the fitness (objective) function.

x = optimvar("x",LowerBound=-5,UpperBound=5);
y = optimvar("y",LowerBound=-5,UpperBound=5);
rosenbrock = (10*(y - x.^2)).^2 + (1-x).^2;
prob = optimproblem(Objective=rosenbrock);

Create 100 random 2-D points within the bounds. The points must be row vectors.

rng default % For reproducibility
xval = -5 + 10*rand(1,100);
yval = -5 + 10*rand(1,100);

Create the initial point values object. Because you do not calculate the fitness values, the values appear as NaN in the display.

vals = optimvalues(prob,x=xval,y=yval)
vals = 
  1x100 OptimizationValues vector with properties:

   Variables properties:
            x: [3.1472 4.0579 -3.7301 4.1338 1.3236 -4.0246 -2.2150 ... ]
            y: [-3.3782 2.9428 -1.8878 0.2853 -3.3435 1.0198 -2.3703 ... ]

   Objective properties:
    Objective: [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... ]

Solve the problem using ga starting from the initial point vals. Set ga options to have a population of 100.

opts = optimoptions("ga",PopulationSize=100);
[sol,fv] = solve(prob,vals,Solver="ga",Options=opts)
Solving problem using ga.
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
sol = struct with fields:
    x: 1.0000
    y: 1.0000

fv = 4.1061e-09

ga returns a solution very near the true solution x = 1, y = 1 with a fitness value near 0.

To create initial points for surrogateopt in the problem-based approach, create an OptimizationValues object using optimvalues.

Create optimization variables for a 2-D problem with Rosenbrock's function as the objective function.

x = optimvar("x",LowerBound=-5,UpperBound=5);
y = optimvar("y",LowerBound=-5,UpperBound=5);
rosenbrock = (10*(y - x.^2)).^2 + (1 - x).^2;
prob = optimproblem(Objective=rosenbrock);

Create constraints that the solution is in a disc of radius 2 about the origin and lies below the line y = 1 + x.

disc = x^2 + y^2 <= 2^2;
prob.Constraints.disc = disc;
line = y <= 1 + x;
prob.Constraints.line = line;

Create 40 random 2-D points within the bounds. The points must be row vectors.

rng default % For reproducibility
N = 40;
xval = -5 + 10*rand(1,N);
yval = -5 + 10*rand(1,N);

Evaluate Rosenbrock's function on the random points. The function values must be a row vector. This step is optional. If you do not provide the function values, surrogateopt evaluates the objective function at the points (xval,yval). When you have the function values, you can save time for the solver by providing the values as data.

fval = zeros(1,N);
for i = 1:N
    p0 = struct('x',xval(i),'y',yval(i));
    fval(i) = evaluate(rosenbrock,p0);
end

Evaluate the constraints on the points. The constraint values must be row vectors. This step is optional. If you do not provide the constraint values, surrogateopt evaluates the constraint functions at the points (xval,yval).

discval = zeros(1,N);
lineval = zeros(1,N);
for i = 1:N
    p0 = struct('x',xval(i),'y',yval(i));
    discval(i) = infeasibility(disc,p0);
    lineval(i) = infeasibility(line,p0);
end

Create the initial point values object.

vals = optimvalues(prob,x=xval,y=yval,Objective=fval,disc=discval,line=lineval)
vals = 
  1x40 OptimizationValues vector with properties:

   Variables properties:
            x: [3.1472 4.0579 -3.7301 4.1338 1.3236 -4.0246 -2.2150 ... ]
            y: [-0.6126 -1.1844 2.6552 2.9520 -3.1313 -0.1024 -0.5441 ... ]

   Objective properties:
    Objective: [1.1067e+04 3.1166e+04 1.2698e+04 1.9992e+04 2.3846e+03 ... ]

   Constraints properties:
         disc: [6.2803 13.8695 16.9638 21.8023 7.5568 12.2078 1.2024 0 ... ]
         line: [0 0 5.3853 0 0 2.9222 0.6709 0 0 0 0.1841 0 0 0 0 2.5648 ... ]

Solve the problem using surrogateopt starting from the initial point vals.

[sol,fv] = solve(prob,vals,Solver="surrogateopt")
Solving problem using surrogateopt.

{"String":"Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: 0.0415877 contains an object of type line. This object represents Best function value.","Tex":[],"LaTex":[]}

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
sol = struct with fields:
    x: 0.7961
    y: 0.6340

fv = 0.0416

surrogateopt returns a solution somewhat near the true solution x = 1, y = 1 with an objective function value near 0.

Input Arguments

collapse all

Optimization problem, specified as an OptimizationProblem object. Create prob using optimproblem.

To obtain useful output from optimvalues, you must also include some data in name-value arguments.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: val = optimvalues(x=xvals,y=yvals)

Data for a variable, named objective, or named constraint, specified as a real double array. Specify all data names for the variables. The objective and constraint function names are optional.

When you specify nval points, the values for each dataname argument must have the following dimensions.

prob.property.namesize(value)
Scalar or vectornumel(prob.property.name)-by-nval
Matrix or arraysize(prob.property.name)-by-nval

In particular, if dataname is a vector, the value of the dataname argument is a matrix with nval columns. For example, if the 'x' variable is a row vector of length 2, and nval is 3, then the 'x' variable specification might be:

val = optimvalues(prob,'x',[1 2 3; 4 5 -6]);

This specification means that 'x' takes the three values [1,4], [2,5], and [3,-6].

Example: For scalar 'x' and two-element row vector 'y' with nval = 2: val = optimvalues(prob,x=[5,3],y=[1 2;3 4]). The output val has two values: x = 5, y = [1 3] and x = 3, y = [2 4].

Data Types: double

Values for an unnamed objective function, specified as a real double array. The size of the values is the same as in dataname.

You can specify values of multiple objective functions for optimization problems in two ways:

  • The Objective property of the optimization problem is a function handle, where the function returns a vector or array. In this case, specify the value as a matrix. Each matrix row represents the values of one objective at the various points. Each column represents the values of the various objectives at one point.

  • The Objective property of the optimization problem has multiple named objectives. In this case, specify the values for each named objective using its name as a dataname argument.

These solvers use any supplied objective function values:

  • ga

  • gamultiobj

  • paretosearch

  • surrogateopt

Example: For one objective and two points, val = optimvalues(prob,x=[3,5],Objective=[exp(3)+1,exp(5)-1])

Data Types: double

Values for an unnamed constraint function, specified as a real double array. The size of the values is the same as in dataname.

You can specify values of multiple constraint functions for optimization problems in two ways:

  • The Constraints property of the optimization problem is a function handle, where the function returns an array. In this case, specify the values as an array with one more dimension than the function returns.

  • The 'Constraints' property of the optimization problem has multiple named constraints. In this case, specify the values for each named constraint using its name as a dataname argument.

These solvers use any supplied nonlinear constraint function values:

  • paretosearch

  • surrogateopt

These solvers ensure that linear constraints are satisfied at all iterations or for all population members:

  • ga

  • gamultiobj

  • paretosearch

  • patternsearch

  • surrogateopt

Example: For two points and three constraints, val = optimvalues(prob,x=[3,5],Objective=[exp(3)+1,exp(5)-1],Constraints=[4 5;-7 -2;0.2 12])

Data Types: double

Output Arguments

collapse all

Point and function values, returned as a vector of OptimizationValues objects. The vector has nval entries, where nval is the number of points in val.

Version History

Introduced in R2022a