MATLAB Answers

wiem abd
0

How to add constraint with condtions using fmincon?

wiem abd
さんによって質問されました 2019 年 10 月 10 日
最新アクティビティ wiem abd
さんによって 回答されました 2019 年 10 月 22 日 8:51
I have am optimization problem with two variables x and y where x and y are vectors.
I would like to add the foollowing constraint to fmincon :
if a<b then x<y
where a and b are known values.
I would like also to add in general x<y for every value of both vectors.
Many thanks!

  0 件のコメント

サインイン to comment.

タグ

製品


リリース

R2018b

4 件の回答

xi
Answer by xi
on 10 Oct 2019
 Accepted Answer

Your constraint could be written as:
x-y < 1/(a<b)-1
when a<b, a<b=1, so, 1/(a<b)-1=0
when a>=b, a<b=0, so, 1/(a<b)-1=Inf, it is equivalent to not having constraint.

  5 件のコメント

Thank you! What if I want to add constraint where x(i)<y(i) for in {1,length(x)}
Assuming your x and y array length is n, you have 2n variables, not 2. So
X= (x1,x2,...xn,y1,y2...yn)
The constraint should be written in the vectorized format of AX-b<0;
for example, x1<y1 and x2<y2,... should be written as
A= [ 1,0,0...-1,0,0,...;
0,1,0...0,-1,0,...;
...]
b= [0,0,...]
I don't quite understand what in {1,length(x)} means. You can either use the trick I described and write A in an (n by 2n) matrix, or find out how many total contraints (=m) you have, and write A in an (m by 2n) matrix, where m<n.
Thank you ! That was exactly what I needed.
Now let us consider that I have a vector L of lenght N where the known values are the elements of L.
I want to check for the vector X of lenght N that
for i=1:N
for j=1:N
if L(i)<L(j) then X(i)<=X(j) or X(i)-X(j)<1/(L(i)<L(j))-1
end
end
How to translate this into Matrix because I need to insert it as a constraint in fmincon

サインイン to comment.


xi
Answer by xi
on 13 Oct 2019

Ask yourself 3 question: How many vairable? how many constraints? and then how to write A and b.
Now you are saying L is a known vector, then, your vairables are just X of length N. you can write your constraint in this way:
-----------------------------
A=zeros(N^2, N);
b=ones(N^2,1);
count=0;
for i=1:N
for j=1:N
count=count+1;
A(count,i)=1;
A(count,j)=-1;
b(count)= 1/(L(i)<L(j))-1;
end
end
---------------------------------
or using N(N-1)/2 constraints instead of N^2
for i=1:N-1
for j=i:N
................
end
end
---------------------------------------
A better way is to sort L first, and get the ordering index using [~,index] = sort(L)
So you define your new variable X'=X(index); and solve X' instead.
then, you only need to write N-1 constraints:
X'(1)<X'(2), X'(2)<X'(3), ... X'(N-1)<X'(N)
b is simply b=zeros(N-1,1); You can figure out A. This should be much faster.

  6 件のコメント

This is the obtained result for N=10. I thing I am missing something
index =
5
3
2
10
1
6
8
4
7
9
>> A
A =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 -1 1
>> size(A)
ans =
100 10
no longer need count, just A(i, index(i)), or you need count++
Thank you very much.
It is working correctly now.

サインイン to comment.


wiem abd
Answer by wiem abd on 14 Oct 2019

Now let us consider that my variable X is of length 2*N and my known values are in a vector L of lenght N.
I want that :
for i=1:N
for j=1:N
if L(i)<L(j) then X(i)<=X(j) and X(N+i)<X(N+j)
end
end
How can I find A such that A*X<zeros(N-1,1).

  2 件のコメント

Now you have 2N variables, and 2*(N-1) constraints, only need small modifications of the above code
A=zeros(2*(N-1), 2*N);
[~,index] = sort(L)
for i=1:N-1 % write two constraints for each i
A(i,index(i))=1;
A(i,index(i+1))=-1;
% ******* just add two lines here, I leave it to you to figure out what the index of A should be.
end
b=zeros(N-1,1);
I think these should be the lines to add :
A(i+N,N+index(i))=1;
A(i+N,N+index(i+1))=-1;
b size should also be modified as follows :
b=zeros(2*N-1,1);
It is working and giving correct results .
Many thanks !

サインイン to comment.


wiem abd
Answer by wiem abd on 22 Oct 2019 at 8:51

Is there a way to formulate this anlytically using equations ?

  0 件のコメント

サインイン to comment.



Translated by