MATLAB Answers

# How to compute a definite sum of a vector as an LP constrain

1 view (last 30 days)
Roeland Bakker on 12 Aug 2020
Answered: Swatantra Mahato on 11 Sep 2020
Hello,
My own model is very large and complex so I will try to explain the problem by a simple example: The vector P is a V:1 vector. Each element of P is a positive real number. The following linear programme problem needs to be solved:
%optimization variable x:
x=optimvar('x',V,1);
%Objective function f:
f=optimexpr(size(P));
for t=V:1;
f(t)=P(t)*x(t)
end
%optimization problem:
prob1=optimproblem('ObjectiveSense','minimize');
obj=sum(f);
prob1.Objective=obj;
prob1.Constraints.cons1=x<=1;
prob1.Constraints.cons2=x>=-1;
If one solves the optimization problem above, the optimization variable outcome x will be a V:1 vector with all elements having a value of -1.
Now I want to put a constrain on the definite sum of each element. i.e. the sum of vector elements should never exceed the boundary -3: . (note that I don't want to constrain the total sum of vector x ( ) but only the sum up to the t-th element.
I tried introducing the following auxiliary variable to compute the constrain:
%create auxiliary variable
w=optimexpr(size(P));
w(t)=sum(x(1:t))
%create constraint:
prob1.Constraints.cons3=w>=-3
When I put in the constraint, and run the solver, the optimal solution is found. However, the constraint is not taken into account by the solver since each element of vector x has a value of -1.. Is there anyone that knows a way to resolve this problem??
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Swatantra Mahato on 11 Sep 2020
Hi Roeland,
I am assuming you want
w(t) = sum(x(1:t))
for all values of t from 1 to V. In order to do that you may want to insert the above statement inside the for loop. The loop would change as follows:
f=optimexpr(size(P));
w=optimexpr(size(P));
for t=1:V
f(t)=P(t)*x(t)
w(t)=sum(x(1:t))
end
To check
prob1.Constraints.cons3=w>=-3;
MATLAB then compares every element of w with 3.
As an example for V=5 and P= [88.0224, 98.9022, 1.0517, 86.6784, 61.6441]
we get x=[ -1,-1,1,-1,-1]
Hope this helps
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!