defining simple optimization constraint: When x1 > 0 then x2 has to equal 0
Show older comments
Hi there,
I'm working on an optimization problem with 2 decision variables x1 and x2. The problem optimizes the values of x1 and x2 each hour over a 24 hour period to minimize the objective function.
I've already defined 2 constraints as follows (using the code below):
Constraints:
0 <= x1 <= 3
0 <= x2 <= 3
N =24;
lb = zeros(2*N,1); % lower bound is 0
ub = 3*ones(2*N,1); % upper bound is 3
Now, I'm trying to define a constraint that says that only x1 or x2 can be greater than 0 at any one time. i.e. If x1 is greater than 0 then x2 has to equal 0 and vice versa. In the problem x1 and x2 refer to the energy flow in and out of a battery, therefore only one variable can be positive at a time.
if x1 > 0 then x2=0
or
if x2 > 0 then x1 = 0
I'm scratching my head but I can't seem to figure out how to define this constraint in matrix form.
I'd welcome any suggestions and thank you for your help.
Sincerely
Accepted Answer
More Answers (2)
That is going to be horribly non-linear and drive the solver mad.
Wouldn't it be simpler to write it as two separate problems, one with x1 fixed at zero and one with x2 fixed at zero and then write an outer routine to pick which is best?
Alan Weiss
on 30 Aug 2013
You can represent this as a simple nonlinear constraint
sum(x1.*x2) = 0
However, the resulting problem will very likely be extremely sensitive to initial conditions.
Sometimes it is worth reformulating your problem as a lower-dimensional one, say
y = x1 - x2
where there is a lower bound of -3 on y and an upper bound of 3. If the solution y > 0 then x1 > 0 and x2 = 0. If y < 0 then x1 = 0 and x2 > 0. Of course, you need to reformulate your objective function in terms of y, not x1 and x2. And you have to be careful that the derivative of the objective function is smooth at y = 0, which might not be possible, or might require some smoothing.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!