ismember([e;b],EB)sdpvar e bEB = randn(2,10);optimize(ismember([e;b],EB),e^2+b^2)sdpvar e b
EB = randn(2,10);
d = bnvar(10,1);>> optimize([[e;b] == EB*d, sum(d)==1],e^2+b^2) F = ismember(x,P)
If P is matrix of DOUBLE, a constraint constraining the vector x to equal one of the columns of P is created. This will introduce size(P,2) binary variables >> e=sdpvar(2,1);b = sdpvar(2,1);EB=randn(2);ismember([e(1);b(1)],EB(1))++++++++++++++++++++++++++++++++++++++| ID| Constraint|++++++++++++++++++++++++++++++++++++++| #1| (ismember)|| #2| Element-wise inequality 4x1|++++++++++++++++++++++++++++++++++++++for i=1:2
C=[C, ismember([e(i);b(i)],EB(i))];endmig1 = [];
for i=1:H_s
temp= []; for j=1:W_s O=o_h(i); Z=z(j); temp = [temp alpha(i,j)*mig_cost(Z,O)]; end
mig1 = [mig1;temp] endvalues = [1 2 3 4 5 6 7]';n = length(values);e = sdpvar(1);en = sdpvar(1);
larger = binvar(n,1);position = binvar(n,1);
Model = [e >= en, implies(values >= en,larger), sum(position) == 1];for i = 1:n % If we pick out this position, then e should have this value Model = [Model, implies(position(i), e == values(i))]; % If we pick out this solution, then e(i) must be the smallest % among those that are larger than en theother = setdiff(1:n,i); Model = [Model, implies(position(i) + larger(theother) >= 2, values(theother) >= e)];endModel = [Model, -10 <= [e;en] <= 10]
% This should lead to position 4optimize([Model,en == 3.5], e) value(position)value(e)
% This should lead to position 7optimize([Model,en == 6.5], e) value(position)value(e)
Thank you very much for your attention and help.
I am sorry for having disturbed you and asking many questions. I try to rewrite my code according to your example, but, I have faced some other problems:(
You have explained how I should implement min(values(values>=en)) by an example. Now, if “values” is a matrix, not a vector, and “b” should have the values of "values(2,i)" if we pick up position(i), how do I revise the code? Following code leads to incorrect value for “b”, but, I did not why.
Model=[Model, implies(position(i), b==values(2,i))];
Additionally, the objective function in my problem is not min(values(values>=en)). This is one of the constraints. In other words, I should solve max(b-e*p) subject to: e=min(values(values>=en)) and some other constraints. How can I define it as a constraint, not as an objective function in optimization?
I change some parts of the code to better illustrate the issue. But, I did not know how to revise the code to generate the right result.
Thank you so much.
%======================================================
values = [1,3,9,9.5,12,18;2,5,7,9,3,1];
n = size(values,2);
e = sdpvar(1);
b = sdpvar(1);
en = sdpvar(1);
p=5;
larger = binvar(n,1);
position = binvar(n,1);
Model=[e>=en, implies(values(1,:)>=en,larger), sum(position)==1];
for i=1:n
Model=[Model, implies(position(i), e==values(1,i];
Model=[Model, implies(position(i), b==values(2,i))];
theother =setdiff(1:n,i);
Model=[Model, implies(position(i)+larger(theother)>=2, values(1,theother)>=e)];
end
optimize([Model,en==11.9],-(b-e*p))
value(position)
value(e)
value(b)
K>> [values;value(position)']
ans =
21 25 28 32 35 41 5 8 9 10 61 1 0 0 0 0 1 0
K>> value(e)
ans =
35.0000
K>> value(b)
ans =
61sdpvar x y
Objective = x^2+y^2
Model=[x==1,x>=100]sdpvar x y WL
Objective = -WL
Model=[x==1,x>=WL]