How can I use absolute values in constraints to fmincon?

12 views (last 30 days)
How can I use absolute values in constraints to fmincon?
I'm trying to maximize a linear function that is the sum over P_i * x_i where P_i are known coefficients and the x_i are what I'm solving for.
So it looks like this: f = P1 * x1 + P2 * x2 + P3 * x3 ...
There are two constraints for the maximization. The first is that the sum of P_i must be less than a known constant. No problems here.
The second constraint is my issue: The sum of the absolute values of the P_i must be less than a different known constant.
I found a webpage that talks about rewriting the absolute value constraint as two other constraints, but I can't figure out how to do this in Matlab.
But basically it says if your constraint is:
abs(x) + abs(y) < K
then introduce two new variables xP and yP and replace the above constraint with these:
xP > x
xP > -x
yP > y
yP > -y
xP + yP < K
Any idea how I do this? Should the extra variables appear in the constraint function? Or do I need another variable for the argument to fmincon?
Here's where I am so far:
In my main program:
[x,fval,exitflag,output] = fmincon( @(x)myObjectiveFunction( x, constants ), ...
startPoints, ...
[],[],[],[],[],[], ...
@(x)myConstraints(x, paramsStruct), ...
options);
%--------------------------
myObjectiveFunction.m :
function f = myObjectiveFunction( x, constants )
%the -1 is because we want to maximize
f = -1 * sum( constants' .* x);
end
%--------------------------
myConstraints.m
function [c, ceq] = myConstraints(x, paramsStruct)
k1 = paramsStruct.k1;
k2 = paramsStruct.k2;
%no equality constraints
ceq = [];
%first constraint.
c(1) = sum(x) - k1;
%absolute value constraint?
xP = ??? %what do I do here?
c(2) = x - xP;
c(3) = -x - xP;
c(4) = sum(xP) - k2;
end
Any thoughts appreciated!
Thanks, Dennis
  2 Comments
Matt J
Matt J on 21 Jul 2014
where P_i are known coefficients and the x_i are what I'm solving for.
I think you really meant that the P_i are what you're solving for. The constraints you cite are in terms of P_i not x_i.
Matt J
Matt J on 21 Jul 2014
Edited: Matt J on 22 Jul 2014
You should really be using linprog instead of fmincon. The objective function is linear and also all constraints become linear after you transform your absolute value constraints using the xP variables.

Sign in to comment.

Answers (1)

anegm
anegm on 21 Jul 2014
Edited: anegm on 21 Jul 2014
SA,
The idea in the link that you provided can be applied by increasing the size of your vector, in your case the size of the new vector will be double that of the original one. The objective function will remain the same, however the constraints will change. Actually I did not deal with constraints in the form of a function as you do, but I will tell you how to do it without a function for constraints. See the help information for these matrices: A,b. For A and b:
if N is the number of parameters to be optimized: First Constraint: A1 = [ones(1,N) zeros(1,N)] b1 = k1
for the second constraint, for each xi you have: xpi>xi and xpi>-xi or xi-xpi<0 and -xi-xpi<0, i.e. you will have two constraints for each parameter. Now your new vector will have additional N parameters xp1 to xpN. For example if N = 3, the matrix form of the constraints will be: A2 = [1 0 0 -1 0 0;-1 0 0 -1 0 0;0 1 0 0 -1 0;0 -1 0 0 -1 0;0 0 1 0 0 -1;0 0 -1 0 0 -1;0 0 0 1 1 1]; b2 = [0;0;0;0;0;0;k2]
As you observe the number of columns of A2 is 6, double of N. And observe how the last constraint will apply the limit value k2. Final step is to concatenate both constraints together: A = [A1;A2] b = [b1;b2]
And now you are ready. Hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!