defining simple optimization constraint: When x1 > 0 then x2 has to equal 0

43 views (last 30 days)
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):
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
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.

Accepted Answer

Matt J
Matt J on 30 Aug 2013
Edited: Matt J on 30 Aug 2013
This is a classical application of Dynamic Programming and that might be the most efficient way for you to go about this problem, see
One thing I don't understand, though, is why you have two separate variables, x1 and x2 for Energy_in and Energy_out? Can't you just have one decision variable x in each hourly period and treat x>0 as energy out and x<=0 as energy in? In other words, can't you just view Energy_out as positive energy consumption and Energy_in as negative energy consumption?
John on 3 Sep 2013
Edited: John on 3 Sep 2013
Hello Matt,
Thank you for your reply. I like your idea and I had thought of it myself. It sounds like the best solution. However, I have a small problem that I would be grateful if you could perhaps comment on. It is a coding question.
The objective function of the optimization problem is currently written in symbolic math notation in terms of the 2 variables and 2 constants. The two variables are Energy_in and Energy_out. Both can only be positive as mentioned above. The 2 constants are efficiency (c1=.95) and a battery degradation constant (c2 = 0.000018) which accounts for the battery degradation from repeated charging and discharging.
This is the current code:
N =24;
X = sym('x',[2*N,1]); % Energy_in and Energy_out
L = sym('L',[N,1]); % building load
P = sym('P',[N,1]); % price
c = sym({'c1','c2'}.','r'); % constants
Energyin = X(1:N);
Energyout = X(N+1:end);
%%objective function
Cost = P.*(L-(c(1).*Energyout)+(1/c(1).*Energyin))+c(2).*Energyout
If I've attempted to write the code in terms of one variable below just called "Energy". However, is there a way in symbolic math to specify the following
  • c1 should only be multiplied by a positive energy value (0 otherwise)
  • 1/c1 should only be multiplied by a negative energy value (0 otherwise)
  • c2 should only be multiplied by a positive energy value (0 otherwise)
N =24;
X = sym('x',[N,1]); % Energy_in and Energy_out
L = sym('L',[N,1]); % building load
P = sym('P',[N,1]); % price
c = sym({'c1','c2'}.','r'); % constants
Energy = X(1:N);
%%objective function
Cost = P.*(L-(c(1).*Energy)+(1/c(1).*Energy))+c(2).*Energy
Thank you for your help
Kind regards
Matt J
Matt J on 3 Sep 2013
Edited: Matt J on 3 Sep 2013
John, I don't know why it helps you to work with symbolic variables, assuming you are still working with the Optimization Toolbox. The Toolbox requires the objective function to return a numerical result, not a symbolic one.
A second problem is that, even with the 2-variable formulation (Energy_out and Energy_in), your cost is not differentiable at energy levels equal to zero, if cost goes abruptly to zero there. Unless you're using GA, you need to reconsider your model and make the X-dependence go smoothly/differentiably down to zero.
One possibility would be
Energy = X(1:N).^3;
Cost = (c(2)-P*c(1))*Energy.*(Energy>0) + Energy.*(Energy<=0)/c(1) +P*L;
You would have to change your other constraints as well, consistent with Energy=x^3.
Finally, the objective function you're showing is a vector, not a scalar, so I'm not sure what you intend to do with that.

Sign in to comment.

More Answers (2)

A Jenkins
A Jenkins on 30 Aug 2013
Edited: A Jenkins on 30 Aug 2013
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
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


Find more on Linear Programming and Mixed-Integer Linear Programming 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!