How to Set a Minimum Step Size for Variables in GA Solver (Global Optimization Toolbox)?

조회 수: 4 (최근 30일)
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.

채택된 답변

John D'Errico
John D'Errico 2024년 10월 17일
편집: John D'Errico 2024년 10월 17일
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)
ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
xsol = 4
fval = -1.0000
exitflag = 1
At the end, we divide by 10, to get x in the steps we wanted to see.
xsol = xsol/10
xsol = 0.4000
If we had no constraint on x, what would the min have been?
[x2,fval2] = fminbnd(obj,-10,10);
x2 = x2/10
x2 = 0.3927
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
Pavl M.
Pavl M. 2024년 10월 18일
편집: Pavl M. 2024년 10월 18일
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.
Paolo Caraffini
Paolo Caraffini 2024년 10월 18일
편집: Paolo Caraffini 2024년 10월 18일
Given the structure of the problem I’m facing and how my objective function is defined (it’s not a "simple" mathematical function but a series of calculations to optimize a parallel kinematic system based on desired behaviors), I find this option more useful. The number of my variables is high (>50), and I would need to define x_val multiple times (thus adding variables to the workspace), with different upper and lower bounds for each variable. Since I have specific functions to extract everything through vectors, I only need to divide the entire variable vector once during the extraction phase and multiply the matrix containing my limits just once. So for my pourpose seem better and simpler to implement also if maybe this method is more "hard-coded". Anyway, I’m grateful for the time you took to respond to me even though my choice falls on the option proposed by John D'Errico.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Pavl M.
Pavl M. 2024년 10월 17일
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)
x =
OptimizationVariable with properties: Name: 'x' Type: 'continuous' IndexNames: {{} {} {}} LowerBound: -1 UpperBound: 1 See variables with show. See bounds with showbounds.
y = optimvar('y',1,1,1,'Type','continuous','LowerBound',lb,'UpperBound',ub)
y =
OptimizationVariable with properties: Name: 'y' Type: 'continuous' IndexNames: {{} {} {}} LowerBound: -1 UpperBound: 1 See variables with show. See bounds with showbounds.
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)
Solving problem using ga. ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
sol = struct with fields:
x: 0 y: 0
fv =
22.3606797749979
% 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

Torsten
Torsten 2024년 10월 17일
편집: Torsten 2024년 10월 17일
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.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by