Non-linear constraints format for GA using solve function
12 views (last 30 days)
Show older comments
I am trying to optimize integer variables with non-linear constraints using GA optimization. Specifically, the constraint is meant to make sure that each integer variable is unique (see example_2). I used to the following code to ensure that each integer is unique
-size(unique(x),2) + size(x,2)
when I include this in the solve function with ga, it gives me error stating:
% Error using optim.problemdef.OptimizationProblem.checkValidConstraints
% Constraints must be an OptimizationConstraint or a struct containing OptimizationConstraints.
%
% Error in indexing
%
% Error in indexing
%
% Error in example_2 (line 34)
% problem.Constraints.c1 = -size(unique(Pos_Input_mtx),2) + size(Pos_Input_mtx,2)<=0;%const_2(Pos_Input_mtx);
I tried couple of different ways to format this without any success. The work around I had was to use ga function directly with the same constraint function (see example_1). This demostrates that the GA function is able to intepret my constraint function. I would prefer to use solve function as it is more versatile and was wondering how I can format the constraint properly without the error shown above.
I have included two sets of code as example.
- example_1.m uses const_1.m and calcResistance.m. Example_1 is the format using ga function directly that runs.
- example_2.m uses const_2.m and calcResistance.m. Example_2 is the format using solve function that does not run.
0 Comments
Accepted Answer
Matt J
on 4 Mar 2025
Edited: Matt J
on 4 Mar 2025
I would formulate it as,
N=numel(Pos_Input_mtx);
problem.Constraints.c1=abs(Pos_Input_mtx(:)-Pos_Input_mtx(:)') >= (1-eye(N))*0.5;
5 Comments
Torsten
on 4 Mar 2025
Edited: Torsten
on 4 Mar 2025
Both versions work with the solver-based approach. For the problem-based approach, I guess you have to use "fcn2optimexpr" to make the constraint work in your formulation.
R1 = 2;
R2 = 1;
Pos_Input_init = [1 3 2 4 6 5];
r_stator = calcResistance(Pos_Input_init, R1, R2);
%% Optimization Problem
LB = 1;
UB = 6;
fun = @(x)calcResistance(x, R1,R2);
nvars = 6;
A = [];
b = [];
Aeq = [];
beq = [];
lb = ones(1,6)*LB;
ub = ones(1,6)*UB;
nonlcon = @(x)const_1(x);
intcon = 1:6;
options = optimoptions('ga','Display','diagnose');
[x,fval,exitflag,output,population,scores] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options);
x
fval
nonlcon = @(x)const_2(x);
[x,fval,exitflag,output,population,scores] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options);
x
fval
function [c,ceq] = const_1(x)
N = numel(x);
c = (1-eye(N))*0.5 - abs(x(:)-x(:)');
c = c(:);
ceq = [];
end
function [c,ceq] = const_2(x)
c = -size(unique(x),2) + size(x,2);
ceq = [];
end
function [R_all] = calcResistance(R_pos_input, R1,R2)
parallel = @(varargin)1/sum(1./[varargin{:}]);
R_group = zeros(3,3);
R_phase = zeros(3,1);
for ph = 1:3
ind = (ph - 1) * 2 + 1;
R_out(ind) = (R_pos_input(ind)-1) *R1;
R_out(ind + 1) = abs((R_pos_input(ind) - R_pos_input(ind+1))) *R1;
for i = 1:3
R_group(i,ph) = R_out(ind) + R_out(ind + 1) + R2 ;
end
R_phase(ph) = parallel(R_group(1,ph),R_group(2,ph),R_group(3,ph));
end
R_all = sum(R_phase);
end
More Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!