How to Set a Minimum Step Size for Variables in GA Solver (Global Optimization Toolbox)?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Paolo Caraffini
le 17 Oct 2024
Modifié(e) : Paolo Caraffini
le 18 Oct 2024
Hello,
I am using the GA solver from the Global Optimization Toolbox and I was wondering if it is possible to specify a minimum variation for the variables. I have already set the lower bound (lb) and upper bound (ub), but I would like to define a minimum step size. For example, let's say the variables range from -1 to 1 (ub=1 and lb=-1), but I would like them to take only values that are multiples of 0.1, such as -1, -0.9, -0.8, ..., 0.9, 1, and not values like 0.0X. Thank you in advance.
0 commentaires
Réponse acceptée
John D'Errico
le 17 Oct 2024
Modifié(e) : John D'Errico
le 17 Oct 2024
Really easy.
Don't think of a step size for the GA solver. It does not take steps anyway, not like fmincon or other solvers of that class.
But GA can handle variables with an integer constraint. BUT.... You don't want integer variables. Even so, it is easy, once you start thinking the right way about your problem. Just constrain your variables to be integer, in this case, from -10 to 10. Then divide by 10 when you use them in your objective.
For example. Suppose we want to minimize the simple function -cos(x - pi/8), for x between -1 and 1?, BUT also subject to the constraint that x takes on only values from the set -1:0.1:1? SIMPLE!
intcon = 1; % one unknown, and it will be integer.
nvars = 1;
obj = @(x) -cos(x/10 - pi/8);
lb = -10;
ub = 10;
So x will vary from -10 to 10, in integer steps. But we use x/10 in the objective.
[xsol,fval,exitflag] = ga(obj,nvars,[],[],[],[],lb,ub,[],intcon)
At the end, we divide by 10, to get x in the steps we wanted to see.
xsol = xsol/10
If we had no constraint on x, what would the min have been?
[x2,fval2] = fminbnd(obj,-10,10);
x2 = x2/10
So ga found the minimum solution, subject to the constraint that x comes from the desired set. We are the only ones who care that ga thinks it is dealing with integers, but we know better.
5 commentaires
Pavl M.
le 18 Oct 2024
Modifié(e) : Pavl M.
le 18 Oct 2024
No, why?, sorry, my solutions is better, also since (because) it comprehends also more versatile solve(...) function and doesn't require 1/10 division twice. It is obviously better, I correct even the decision/selection steps here.
Please review your decision. My solution is indeed more readable, comprehensive, integral and adaptable.
Plus de réponses (2)
Pavl M.
le 17 Oct 2024
I solved it complete.
Solution AB1:
close all force;
clc;
clear all
clear global;
tol = 2.7756e-1;
format('long','g') %format of the numeric values: each number represented by about 15 digits
ub = 1;
lb = -1;
pop_size = 40;
step = 0.1;
A = 10;
xval = lb:step:ub;
yval = lb:step:ub;
x = optimvar('x',1,1,1,'Type','continuous','LowerBound',lb,'UpperBound',ub)
y = optimvar('y',1,1,1,'Type','continuous','LowerBound',lb,'UpperBound',ub)
f1 = A*sqrt(y.^2 + x.^2 + 5);
prob = optimproblem(Objective=f1);
vals = optimvalues(prob,x=xval,y=yval);
opts = optimoptions("ga",PopulationSize=400);
[sol,fv] = solve(prob,vals,Solver="ga",Options=opts)
% Your needs = ... []-> to continue ... []-> to keep doing...
%Constructed by P.Mazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://join.skype.com/invite/oXnJhbgys7oW
% https://willwork781147312.wordpress.com/portfolio/cp/
% https://nanohub.org/members/130066
% https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
0 commentaires
Torsten
le 17 Oct 2024
Modifié(e) : Torsten
le 17 Oct 2024
Define 21 integer variables x_i that can take values 0 or 1 (by using the "intcon" input to "ga" and setting the lower and upper bounds for the x_i to 0 and 1, respectively)
Set the linear equality constraint that the sum over these 21 integer variables x_i equals 1 (by using Aeq and beq).
Then the variable
y = -1 + 0.1*sum_{i=0}^{i=20} i*x_i
is the variable you are after: it can only take values -1,-0.9,-0.8,...,1.9,2.
0 commentaires
Voir également
Catégories
En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!