Using Optimization toolbox in nonlinear with matrix manipulation
Show older comments
I'm looking for optimization problem as follows that I don't know how to optimize it through Matlab Code or toolbox.
I have a minimum costs problem
The Parameters are follows:
BidPrices=[4735,4730,4727;410,409.7,409;59,58.8,58.5]; % Parameters which include 3 bid prices columns for 3 securities.
BidQuantity=[7000,2500,1081;320,1500,25000;754000,20000,101026]; % Bid Quantity for each bid
OpenPrice=[4735 ;410 ;59.1] % The open Price
Weights=[0.5;0.25;0.25]
Quantity=zeros(3:3) % variable to minimize
f=Qunatity*BidPrices % The function to minimize
The constraints are:
% Positive nonzero interger quantity
Quantity>=0
Quantity=Integer
% Quantity cannot exceed its bid quantity
Quantity(:,1)<=BidQuantity(:,1);
Quantity(:,2)<=BidQuantity(:,2);
Quantity(:,3)<=BidQuantity(:,3);
% The total change in the index from OpenPrice is higher than 1%
% First Step: Define active price matrix because when the quantity from bid1 to bid2 also change the price of the index
for I=1:length(OpenPrice)
if Quantity(I,1)>0 && Quantity(I,2)>0
EffectivePrice(I,1)=0
else
EffectivePrice(I,1)=0
end
if EffectivePrice(I,1)==0 && Quantity(I,2)==0 && Quantity(I,3)==0
EffectivePrice(I,2)=0
else
EffectivePrice(I,2)=0
end
if EffectivePrice(I,1)==0 && Quantity(I,3)>0
EffectivePrice(I,3)=1
else
EffectivePrice(I,3)=0
end
end
% Step 2: Defining the Constraint
Constraint=((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights))-1<=0.01
% How to write it correctly in Matlab code so that matlab can solve it? I didn't succeed with the toolbox.
1 Comment
Matt J
on 5 Jan 2017
f=Qunatity*BidPrices % The function to minimize
This is a 3x3 matrix-valued quantity, so it is not clear what it means to "minimize" it
Answers (2)
Matt J
on 4 Jan 2017
0 votes
You would use INTLINPROG in the Optimziation Toolbox
9 Comments
Yossi
on 4 Jan 2017
Matt J
on 4 Jan 2017
Well, that is your homework assignment so we can't do it for you here, however, it appears that you only have upper and lower bound constraints. Note that intlinprog lets you specify these directly with arguments, lb,ub. From the documentation,
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
Yossi
on 5 Jan 2017
Walter Roberson
on 5 Jan 2017
Use linear indexing and a vector of length 9
Yossi
on 5 Jan 2017
Walter Roberson
on 5 Jan 2017
It is not possible to specify a matrix constraint. The values being processed are converted to vector form.
But it does not matter, because you can rewrite your constraint in terms of linear indexing for any constraint supported short of nonlinear constraints. Nonlinear constraints are expressed by a function handle and inside the function you can reshape if you really need to.
Yossi
on 5 Jan 2017
Walter Roberson
on 5 Jan 2017
Any matrix of variables will be either rejected by the algorithm or reshaped into a vector, depending on which routine you are using.
Yossi
on 5 Jan 2017
Walter Roberson
on 5 Jan 2017
You have
Constraint=((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights))-1<=0.01
and at any one point, EffectivePrice is the vector of trial values and BidPrices and Weights and OpenPrice are inputs.
So start doing some algebra:
((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights))-1<=0.01
impies
((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights)) <= 1.01
implies
((EffectivePrice*BidPrices*Weights) <= 1.01 * (OpenPrice*Weights)
implies
EffectivePrice <= 1.01 * (OpenPrice*Weights) / (BidPrices*Weights)
Everything on the right hand side is known in advance, so it can be computed as an upperbound.
Now at this point we run into the problem that OpenPrice and Weights are both 3 x 1 so you cannot "*" them together. If we guess that the formula should be
1.01 * (OpenPrice.*Weights) / (BidPrices*Weights)
then we get
ans =
0.505346858984519 0 0
0.0218787974850742 0 0
0.00315374861309241 0 0
Now as we go back through the code we can see that EffectivePrice is a binary variable, value 0 except in the case EffectivePrice(I,1)==0 && Quantity(I,3)>0 . And we can see that all of the constraint values are either 0 or a value less than 1. With the value required to be binary, this tells us that EffectivePrice must be identically 0.
This analysis assumes that when you write
Constraint=((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights))-1<=0.01
that even though ((EffectivePrice*BidPrices*Weights)/(OpenPrice*Weights)) is a 3 x 3 matrix, that you want the "-1" to be applied to all 9 entries. The calculations become different if the "-1" should instead be eye(3)
... You still have not explained how you can "minimize" f=Qunatity*BidPrices which is 3 x 3
Categories
Find more on Choose a Solver in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!