Non-linear constraints format for GA using solve function
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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.
Accepted Answer
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
Jarek
on 4 Mar 2025
I'm getting the following error:
% Error using optim.internal.problemdef.checkDimensionMatch
% Argument dimensions 6-by-1 and 1-by-6 must agree.
%
% Error in optim.internal.problemdef.ElementwiseOperator/checkIsValid
%
% Error in optim.internal.problemdef.Minus.getMinusOperator
%
% Error in -
%
% Error in Start_OptimizationScript (line 97)
% problem.Constraints.c1=abs(Pos_Input_mtx(:)-Pos_Input_mtx(:)') >= (1-eye(N))*0.5;
I also tried the following and I'm getting the same error:
abs(ones(1,N).*Pos_Input_mtx' - ones(N,1).*Pos_Input_mtx ) >= (1-eye(N))*0.5;
Matt J
on 4 Mar 2025
N=6;
Pos_Input_mtx=optimvar('x',N);
c1 = (Pos_Input_mtx*ones(1,N) - ones(N,1)*Pos_Input_mtx' ).^2 >= (1-eye(N))*0.5
c1 =
6x6 Quadratic OptimizationInequality array with properties:
IndexNames: {{} {}}
Variables: [1x1 struct] containing 1 OptimizationVariable
See inequality formulation with show.
This worked! I'm wondering what is preventing my code from working? Is it because the use of unique function? Why does it work for the GA direct method?
I am using 2024b.
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);
Diagnostic information.
Fitness function: @(x)calcResistance(x,R1,R2)
Nonlinear constraint function: @(x)const_1(x)
6 Variables
6 Integer variables
36 Nonlinear inequality constraints
Options:
CreationFcn: @gacreationuniformint
CrossoverFcn: @crossoverlaplace
SelectionFcn: @selectiontournament
MutationFcn: @mutationpower
Display: 'diagnose'
End of diagnostic information.
Best Mean Stall
Generation Func-count Penalty Penalty Generations
1 120 0.2222 0.7312 0
2 177 0.2222 0.6111 1
3 234 0.2222 10.96 2
4 291 0.2222 11.06 3
5 348 0.2222 10.91 4
6 405 0.2222 10.93 5
7 462 0.2222 14.81 6
8 519 0.2222 14.82 7
9 576 0.2222 10.33 8
10 633 0.2222 10.46 9
11 690 0.2222 10.43 10
12 747 0.2222 14.73 11
13 804 0.2222 10.39 12
14 861 0.2222 11.05 13
15 918 0.2222 10.43 14
16 975 0.2222 10.41 15
17 1032 0.2222 10.37 16
18 1089 0.2222 10.34 17
19 1146 0.2222 10.4 18
20 1203 0.2222 10.49 19
21 1260 0.2222 9.209 20
22 1317 0.2222 9.165 21
23 1374 0.2222 9.18 22
24 1431 0.2222 14.72 23
25 1488 0.2222 12.32 24
26 1545 0.2222 9.187 25
27 1602 0.2222 9.076 26
28 1659 0.2222 9.113 27
29 1716 0.2222 9.146 28
Best Mean Stall
Generation Func-count Penalty Penalty Generations
30 1773 0.2222 9.154 29
31 1830 0.2222 9.146 30
32 1887 0.2222 9.128 31
33 1944 0.2222 9.128 32
34 2001 0.2222 9.124 33
35 2058 0.2222 12.85 34
36 2115 0.2222 9.139 35
37 2172 0.2222 9.072 36
38 2229 0.2222 9.146 37
39 2286 0.2222 9.109 38
40 2343 0.2222 9.146 39
41 2400 0.2222 10.41 40
42 2457 0.2222 9.031 41
43 2514 0.2222 9.131 42
44 2571 0.2222 14.72 43
45 2628 0.2222 11.61 44
46 2685 0.2222 10.97 45
47 2742 0.2222 9.143 46
48 2799 0.2222 9.128 47
49 2856 0.2222 14.79 48
50 2913 0.2222 9.128 49
51 2970 0.2222 9.791 50
ga stopped because the average change in the penalty function value is less than options.FunctionTolerance but
constraints are not satisfied to within options.ConstraintTolerance.
x
x = 1×6
6 2 5 3 4 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval
fval = 14.3333
nonlcon = @(x)const_2(x);
[x,fval,exitflag,output,population,scores] = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options);
Diagnostic information.
Fitness function: @(x)calcResistance(x,R1,R2)
Nonlinear constraint function: @(x)const_2(x)
6 Variables
6 Integer variables
1 Nonlinear inequality constraints
Options:
CreationFcn: @gacreationuniformint
CrossoverFcn: @crossoverlaplace
SelectionFcn: @selectiontournament
MutationFcn: @mutationpower
Display: 'diagnose'
End of diagnostic information.
Best Mean Stall
Generation Func-count Penalty Penalty Generations
1 120 13.67 14.09 0
2 180 10.33 13.97 0
3 240 10.33 13.98 1
4 300 10.33 13.91 2
5 360 10.33 11.36 3
6 420 10.33 13.3 4
7 480 10.33 11.4 5
8 540 10.33 11.35 6
9 600 10.33 11.36 7
10 660 10.33 11.35 8
11 720 10.33 14.44 9
12 780 10.33 10.76 10
13 840 10.33 13.18 11
14 900 10.33 10.73 12
15 960 10.33 10.74 13
16 1020 9.667 13.22 0
17 1080 9.667 10.72 1
18 1140 9.667 12.62 2
19 1200 9 10.7 0
20 1260 9 12.6 1
21 1320 9 10.69 2
22 1380 9 10.69 3
23 1440 9 11.33 4
24 1500 9 11.31 5
25 1560 8.333 11.27 0
26 1620 8.333 15.08 1
27 1680 8.333 12.55 2
28 1740 8.333 10.03 3
29 1800 8.333 10.05 4
Best Mean Stall
Generation Func-count Penalty Penalty Generations
30 1860 8.333 10.04 5
31 1920 8.333 12.5 6
32 1980 8.333 9.401 7
33 2040 8.333 9.394 8
34 2100 8.333 10.63 9
35 2160 7.667 9.372 0
36 2220 7.667 8.716 1
37 2280 7.667 8.726 2
38 2340 7.667 8.716 3
39 2400 7 13.03 0
40 2460 7 14.84 1
41 2520 7 8.67 2
42 2580 7 14.4 3
43 2640 7 8.707 4
44 2700 7 9.947 5
45 2760 7 11.13 6
46 2820 7 13.75 7
47 2880 7 8.707 8
48 2940 7 11.25 9
49 3000 7 8.73 10
50 3060 7 8.697 11
51 3120 7 8.693 12
52 3180 7 8.667 13
53 3240 7 8.693 14
54 3300 7 8.69 15
55 3360 7 8.677 16
56 3420 7 10.55 17
57 3480 7 8.693 18
58 3540 7 10.51 19
59 3600 7 8.039 20
Best Mean Stall
Generation Func-count Penalty Penalty Generations
60 3660 7 8.056 21
61 3720 7 9.944 22
62 3780 7 10.56 23
63 3840 7 8.069 24
64 3900 7 11.23 25
65 3960 7 8.046 26
66 4020 7 11.84 27
67 4080 7 8.042 28
68 4140 7 8.056 29
69 4200 7 8.086 30
70 4260 7 9.942 31
71 4320 7 9.308 32
72 4380 7 8.039 33
73 4440 7 8.042 34
74 4500 7 11.19 35
75 4560 7 8.036 36
76 4620 7 9.299 37
77 4680 7 9.914 38
78 4740 7 8.066 39
79 4800 7 8.039 40
80 4860 7 9.914 41
81 4920 7 9.269 42
82 4980 7 12.45 43
83 5040 7 8.652 44
84 5100 7 13.73 45
85 5160 7 11.82 46
86 5220 7 8.042 47
87 5280 7 8.039 48
88 5340 7 8.076 49
89 5400 7 8.046 50
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.
x
x = 1×6
1 2 3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval
fval = 7
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)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)