问题背景如下
clc,clear;
prob=optimproblem('ObjectiveSense','max');
%限定自变量的类型为整数
x=optimvar('x',4,1,'Type','integer','LowerBound',-1,'UpperBound',1);
c=[6,8,4,2];
Q=[-1,2,0,0;
2,-1,2,0;
0,2,-1,2;
0,0,2,-1];
prob.Objective=c*x+0.5*x'*Q*x;
con=optimconstr(4);
con(1)=x(1)*x(2)+x(3)*x(4)<=1;
con(2)=-1<=x(1)*x(2)+x(3)*x(4);
con(3)=sum(x)<=2;
con(4)=-3<=sum(x);
show(con)
prob.Constraints.con=con;
prob.Constraints.con1=x.^2-1==0;%在这里给出了等式约束
x0.x=rand(4,1);
[s,f,exitflag]=solve(prob,x0),s.x
这样运行会报错
错误使用 optim.problemdef.OptimizationProblem/solve
GA does not solve problems with integer and equality constraints.
For more help see No Equality Constraints in the documentation.
主要是遗传算法(ga)无法用在整数问题和等式约束并存的情况,因此只能将第4行代码改为
x=optimvar('x',4,1,'LowerBound',-1,'UpperBound',1);
也就是说把整数约束去掉,就可以得出结果,但是必须得多次运行才能得到最优解,不知道有没有大佬有解决整数问题和等式约束并存的情况