Optimization of a multivariable function within given solution space

38 views (last 30 days)
Hello,
Assume that function ( f(x,y,z) ) I want to minimize depends on 3 variables, and I want to minimize it using only possible values I chose before.
I have a vector with possible values for the variables.
values_possible = [1,2,3,4,5,6,7,8,9,10]
Assume below values are the optimum values to minimize the function. I want to find them.
x = 3
y = 8
z = 1
How do I code such optimization? This application will be useful to fasten the optimization because I want to minimize a function with more than 100 variables.
Thanks.

Answers (2)

John D'Errico
John D'Errico on 5 Dec 2022
Edited: John D'Errico on 5 Dec 2022
100 variable problems are hugely more complex than 3 variable problems on a very limited search space. You can often solve the small problem using an exhaustive search, but the curse of dimensionality kills you if you go higher.
Anyway, you still have not told us about some significant details of your real problem. If is it linear, and any constraints are also linear, then use intlinprog. Use a linear integer solver to solve linear integer problems.
help intlinprog
INTLINPROG Mixed integer linear programming. X = INTLINPROG(f,intcon,A,b) attempts to solve problems of the form min f'*x subject to: A*x <= b x Aeq*x = beq lb <= x <= ub x(i) integer, where i is in the index vector intcon (integer constraints) X = INTLINPROG(f,intcon,A,b) solves the problem with integer variables in the intcon vector and linear inequality constraints A*x <= b. intcon is a vector of positive integers indicating components of the solution X that must be integers. For example, if you want to constrain X(2) and X(10) to be integers, set intcon to [2,10]. X = INTLINPROG(f,intcon,A,b,Aeq,beq) solves the problem above while additionally satisfying the equality constraints Aeq*x = beq. (Set A=[] and b=[] if no inequalities exist.) X = INTLINPROG(f,intcon,A,b,Aeq,beq,LB,UB) defines a set of lower and upper bounds on the design variables, X, so that the solution is in the range LB <= X <= UB. Use empty matrices for LB and UB if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below; set UB(i) = Inf if X(i) is unbounded above. X = INTLINPROG(f,intcon,A,b,Aeq,beq,LB,UB,X0) sets the initial point to X0. X = INTLINPROG(f,intcon,A,b,Aeq,beq,LB,UB,X0,OPTIONS) minimizes with the default optimization parameters replaced by values in OPTIONS, an argument created with the OPTIMOPTIONS function. See OPTIMOPTIONS for details. X = INTLINPROG(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure with the vector 'f' in PROBLEM.f, the integer constraints in PROBLEM.intcon, the linear inequality constraints in PROBLEM.Aineq and PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in PROBLEM.ub, the initial point in PROBLEM.x0, the options structure in PROBLEM.options, and solver name 'intlinprog' in PROBLEM.solver. [X,FVAL] = INTLINPROG(f,intcon,A,b,...) returns the value of the objective function at X: FVAL = f'*X. [X,FVAL,EXITFLAG] = INTLINPROG(f,intcon,A,b,...) returns an EXITFLAG that describes the exit condition. Possible values of EXITFLAG and the corresponding exit conditions are 3 Optimal solution found with poor constraint feasibility. 2 Solver stopped prematurely. Integer feasible point found. 1 Optimal solution found. 0 Solver stopped prematurely. No integer feasible point found. -1 Solver stopped by an output function or plot function. -2 No feasible point found. -3 Root LP problem is unbounded. -9 Solver lost feasibility probably due to ill-conditioned matrix. [X,FVAL,EXITFLAG,OUTPUT] = INTLINPROG(f,A,b,...) returns a structure OUTPUT containing information about the optimization process. OUTPUT includes the number of integer feasible points found and the final gap between internally calculated bounds on the solution. See the documentation for a complete description. See also LINPROG. Documentation for intlinprog doc intlinprog
If your problem is not of that kind, then you CANNOT use fmincon as you say you wanted. fmincon absolutely requires a continuous domain and a continuous objective. A smooth objective too.
For the problem you describe, you probably need to use GA from the global optimization TB.
help ga
GA Constrained optimization using genetic algorithm. GA attempts to solve problems of the following forms: min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints) X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints) LB <= X <= UB X(i) integer, where i is in the index vector INTCON (integer constraints) Note: If INTCON is not empty, then no equality constraints are allowed. That is:- * Aeq and Beq must be empty * Ceq returned from NONLCON must be empty X = GA(FITNESSFCN,NVARS) finds a local unconstrained minimum X to the FITNESSFCN using GA. NVARS is the dimension (number of design variables) of the FITNESSFCN. FITNESSFCN accepts a vector X of size 1-by-NVARS, and returns a scalar evaluated at X. X = GA(FITNESSFCN,NVARS,A,b) finds a local minimum X to the function FITNESSFCN, subject to the linear inequalities A*X <= B. Linear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'. See the documentation for details. X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq) finds a local minimum X to the function FITNESSFCN, subject to the linear equalities Aeq*X = beq as well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.) Linear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'. See the documentation for details. X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, X, so that a solution is found in the range lb <= X <= ub. Use empty matrices for lb and ub if no bounds exist. Set lb(i) = -Inf if X(i) is unbounded below; set ub(i) = Inf if X(i) is unbounded above. Linear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'. See the documentation for details. X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON) subjects the minimization to the constraints defined in NONLCON. The function NONLCON accepts X and returns the vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. GA minimizes FITNESSFCN such that C(X)<=0 and Ceq(X)=0. (Set lb=[] and/or ub=[] if no bounds exist.) Nonlinear constraints are not satisfied when the PopulationType option is set to 'bitString' or 'custom'. See the documentation for details. X = GA(FITNESSFCN,NVARS,A,b,Aeq,beq,lb,ub,NONLCON,options) minimizes with the default optimization parameters replaced by values in OPTIONS. OPTIONS can be created with the OPTIMOPTIONS function. See OPTIMOPTIONS for details. For a list of options accepted by GA refer to the documentation. X = GA(FITNESSFCN,NVARS,A,b,[],[],lb,ub,NONLCON,INTCON) requires that the variables listed in INTCON take integer values. Note that GA does not solve problems with integer and equality constraints. Pass empty matrices for the Aeq and beq inputs if INTCON is not empty. X = GA(FITNESSFCN,NVARS,A,b,[],[],lb,ub,NONLCON,INTCON,options) minimizes with integer constraints and the default optimization parameters replaced by values in OPTIONS. OPTIONS can be created with the OPTIMOPTIONS function. See OPTIMOPTIONS for details. X = GA(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure that has the following fields: fitnessfcn: <Fitness function> nvars: <Number of design variables> Aineq: <A matrix for inequality constraints> bineq: <b vector for inequality constraints> Aeq: <Aeq matrix for equality constraints> beq: <beq vector for equality constraints> lb: <Lower bound on X> ub: <Upper bound on X> nonlcon: <Nonlinear constraint function> intcon: <Index vector for integer variables> options: <Options created with optimoptions('ga',...)> rngstate: <State of the random number generator> [X,FVAL] = GA(FITNESSFCN, ...) returns FVAL, the value of the fitness function FITNESSFCN at the solution X. [X,FVAL,EXITFLAG] = GA(FITNESSFCN, ...) returns EXITFLAG which describes the exit condition of GA. Possible values of EXITFLAG and the corresponding exit conditions are 1 Average change in value of the fitness function over options.MaxStallGenerations generations less than options.FunctionTolerance and constraint violation less than options.ConstraintTolerance. 3 The value of the fitness function did not change in options.MaxStallGenerations generations and constraint violation less than options.ConstraintTolerance. 4 Magnitude of step smaller than machine precision and constraint violation less than options.ConstraintTolerance. This exit condition applies only to nonlinear constraints. 5 Fitness limit reached and constraint violation less than options.ConstraintTolerance. 0 Maximum number of generations exceeded. -1 Optimization terminated by the output or plot function. -2 No feasible point found. -4 Stall time limit exceeded. -5 Time limit exceeded. [X,FVAL,EXITFLAG,OUTPUT] = GA(FITNESSFCN, ...) returns a structure OUTPUT with the following information: rngstate: <State of the random number generator before GA started> generations: <Total generations, excluding HybridFcn iterations> funccount: <Total function evaluations> maxconstraint: <Maximum constraint violation>, if any message: <GA termination message> [X,FVAL,EXITFLAG,OUTPUT,POPULATION] = GA(FITNESSFCN, ...) returns the final POPULATION at termination. [X,FVAL,EXITFLAG,OUTPUT,POPULATION,SCORES] = GA(FITNESSFCN, ...) returns the SCORES of the final POPULATION. Example: Unconstrained minimization of Rastrigins function: function scores = myRastriginsFcn(pop) scores = 10.0 * size(pop,2) + sum(pop.^2 - 10.0*cos(2*pi .* pop),2); numberOfVariables = 2 x = ga(@myRastriginsFcn,numberOfVariables) Display plotting functions while GA minimizes options = optimoptions('ga','PlotFcn',... {@gaplotbestf,@gaplotbestindiv,@gaplotexpectation,@gaplotstopping}); [x,fval,exitflag,output] = ga(fitfcn,2,[],[],[],[],[],[],[],options) An example with inequality constraints and lower bounds A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1); fitfcn = @(x)0.5*x(1)^2 + x(2)^2 -x(1)*x(2) -2*x(1) - 6.0*x(2); % Use mutation function which can handle constraints options = optimoptions('ga','MutationFcn',@mutationadaptfeasible); [x,fval,exitflag] = ga(fitfcn,2,A,b,[],[],lb,[],[],options); If FITNESSFCN or NONLCON are parameterized, you can use anonymous functions to capture the problem-dependent parameters. Suppose you want to minimize the fitness given in the function myfit, subject to the nonlinear constraint myconstr, where these two functions are parameterized by their second argument a1 and a2, respectively. Here myfit and myconstr are MATLAB file functions such as function f = myfit(x,a1) f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + a1); and function [c,ceq] = myconstr(x,a2) c = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - a2]; % No nonlinear equality constraints: ceq = []; To optimize for specific values of a1 and a2, first assign the values to these two parameters. Then create two one-argument anonymous functions that capture the values of a1 and a2, and call myfit and myconstr with two arguments. Finally, pass these anonymous functions to GA: a1 = 1; a2 = 10; % define parameters first % Mutation function for constrained minimization options = optimoptions('ga','MutationFcn',@mutationadaptfeasible); x = ga(@(x)myfit(x,a1),2,[],[],[],[],[],[],@(x)myconstr(x,a2),options) Example: Solving a mixed-integer optimization problem An example of optimizing a function where a subset of the variables are required to be integers: % Define the objective and call GA. Here variables x(2) and x(3) will % be integer. fun = @(x) (x(1) - 0.2)^2 + (x(2) - 1.7)^2 + (x(3) -5.1)^2; x = ga(fun,3,[],[],[],[],[],[],[],[2 3]) See also OPTIMOPTIONS, FITNESSFUNCTION, GAOUTPUTFCNTEMPLATE, PATTERNSEARCH, @. Documentation for ga doc ga
I'm not sure if any of the other solvers in that toolbox are able to handle large problems with integer variables, but GA would be your first choice in any case.

Askic V
Askic V on 5 Dec 2022
You probably want something like this:
% x,y, z are elements of vector X
fun_h = @(X) X(1)^2+2.5*sin(X(2))-X(3)^2*X(1)^2*X(2)^2;
a = 1:10;
b = 1:10;
c = 1:10;
% Create set of all possible solutions
possible_sol = combvec(a,b,c);
% Determine the number of solutions
[~, nr_sol] = size(possible_sol);
min_sol = inf; % +infinity is the start
min_ind = 1;
for ii = 1: nr_sol
% Columns of possible_sol matrix are possible solutions
x_p = possible_sol(:,ii);
% evaluate function
res = feval(fun_h, x_p);
if res < min_sol
min_sol = res;
min_ind = ii;
end
end
solution = possible_sol(:,min_ind)
min_sol
min_ind
  4 Comments
Baris Yilmaz
Baris Yilmaz on 5 Dec 2022
Thank you, I guess I should have asked like that at the first place. I couldnt find a way to use fmincon with kind of inputs that I asked in the question. I was wondering if that is possible.
Askic V
Askic V on 5 Dec 2022
I also highly doubt it would be feasibile. Probably dimensionality reduction or problem rethinking must be done first.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!