您可以在需要时引入一些松弛变量以允许某些约束违规,如下所示:
largeValue = 100; % choose some large value to penalise the constraint violation
f_ = [f; largeValue; largeValue]; % penalise both slack variables
Aeq_ = [Aeq, 1, -1]; % add a positive and negative slack variable to the constraint
beq_ = beq;
lb_ = [lb; 0; 0]; % limit the constraint to a positive number
ub_ = [ub; inf; inf];
x_ = intlinprog(f_,intcon,[],[],Aeq_,beq_,lb_,ub_); % solve the adapted problem
x = x_(1:3) % extract the solution of the original problem
备注
>我添加了两个(正)松弛变量,一个用于正约束违规,另一个用于负约束违规.
>您应该使用较大的值惩罚松弛变量,否则违反您的约束超出严格要求是有益的.例如,更一般的方法是基于f和Aeq中的值确定良好的惩罚值
largeValue = 2*max(abs(f))/min(abs(Aeq(Aeq ~= 0)))