Search code examples
matlabconstraintsmathematical-optimizationminimization

How to use ‘not equal to’ inequality constraint in fmincon?


How can I set the constraints for the solution (x1,x2), so that x1 ~= x2 (x1 is not x2) in the MATLAB function fmincon?
In numerical sense: x1 should not get close to x2.


Solution

  • To express the constraint x1 ≠ x2 in the form of A*x ≤ b, it would be either of these:

      x(1) – x(2) ≤ –eps    % x(2) ≥ x(1) + eps   
    – x(1) + x(2) ≤ –eps    % x(1) ≥ x(2) + eps
    

    As per the first one, x(2) should be greater than x(1). And as per the second one, x(1) should be greater than x(2). The combination of above would be:

    A = [1, -1; -1,  1];  
    b = [-eps; -eps];
    

    But you cannot use this A and b directly because it would imply that x(1) + x(2) = eps which is not correct. So to find the minimum value of the function and the value of x for which it is minimum, consider both the conditions separately, i.e. one at a time, and see which minimises more. i.e.

    [x(1,:), fval(1)] = fmincon(Fcn, x0, A(1,:), b(1), [], [], lb, ub, [], options);
    [x(2,:), fval(2)] = fmincon(Fcn, x0, A(2,:), b(2), [], [], lb, ub, [], options);
    [Minfval, tmpInd] = min(fval);   %Finding the minimum value of these
    Req_x = x(tmpInd,:);             %Required solution