excluding a number before sending x vector to optimization?

1 visualización (últimos 30 días)
Gulcihan Ozdemir
Gulcihan Ozdemir el 17 de Dic. de 2022
Comentada: Torsten el 17 de Dic. de 2022
Hi,
I am using a huristic algorithm for optimization. x-variable vector range is as given below.
xU = [16 16 16 16 1 1 1 1];%upper bound on decision variables
xL = [-16 -16 -16 -16 0 0 0 0];%lower bound on decision variables
If I want to exclude 0 from the first 4-variable how I can estaiblesh?
When I define sub groups from 1:16 and -16:-1 then combine as x vector for the first 4 variable, it increases the number of variables not just removing the 0.
thank you
  5 comentarios
John D'Errico
John D'Errico el 17 de Dic. de 2022
@Gulcihan Ozdemir - Please don't post an answer just to make a comment.
Gulcihan Ozdemir
Gulcihan Ozdemir el 17 de Dic. de 2022
Yes correct. that is the problem I asked. How to handle that

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 17 de Dic. de 2022
Editada: John D'Errico el 17 de Dic. de 2022
You don't tell us WHICH heuristic algorithm you are using, so that means it is a homebrew algorithm of your own choosing or creation. It also tells me I cannot be more accurate about an answer.
And it is not even clear if your problem is an integer one. If the variables are continuous, then no, there is no tool where you can tell it that you have a continuous domain, that DOES not include some interior point of that domain. You cannot tell an optimizer like fmincon to not allow some specific value within the bounds for that variable.
The only optimizers that can solve an integer problem like this are tools like GA, so an algorithm that can handle integer variables. GA can handle it with no problem. And any algorithm that can handle integer variables in a nonlinear context should be ok too. So I'll show how to solve it using GA.
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
For example, suppose I want to find the minimum of a VERY simple, two variable function. So minimize X^2+Y^2, subject to the constraint that X lies in the set [-5 -4 -3 2 4 9 12], and Y lies in the set [-14 -11 -5 3 6 10 16]? These are totally arbitrary sets of integers.
Yesh, I know, utterly trivial. And I can surely find many ways to solve the problem. I might use a table lookup, for example.
xvalid = [-6 -5 -4 -3 2 4 9 12];
yvalid = [-14 -11 -5 3 6 10 16];
fun = @(xyind) xvalid(xyind(1)).^2 + yvalid(xyind(2)).^2;
intcon = [1 2];
LB = [1 1];
UB = [8 7];
[xyind,fval,exitflag] = ga(fun,2,[],[],[],[],LB,UB,[],intcon)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
xyind = 1×2
5 4
fval = 13
exitflag = 1
[xvalid(xyind(1)),yvalid(xyind(2))]
ans = 1×2
2 3
As you can see, there is no problem at all, since the algorithm itself thinks the set of choices are the integers 1:8 for X and 1:7 for Y.
Anything you will do will probably be in some way equivalent to that.
  2 comentarios
Gulcihan Ozdemir
Gulcihan Ozdemir el 17 de Dic. de 2022
Thanks for your effort.
Yes integer variables I am having.
"As you can see, there is no problem at all, since the algorithm itself thinks the set of choices are the integers 1:8 for X and 1:7 for Y."
What if you want to remove the third element in the search space of x above given (1:8 range and you do not want to see in the solution set the value 3, you do not want to include it) , how do you handle?
Torsten
Torsten el 17 de Dic. de 2022
The value "3" does not stem from 1:8 and 1:7. The "2" comes from xvalid and the "3" comes from yvalid. If you don't want to see certain numbers in the solution, you just have to remove these numbers from xvalid resp. yvalid. 1:8 resp. 1:7 are just the number of elements of the two valid number sets.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by