フィルターのクリア

How to arrange the constraints shape to A matrix and b constants automatically ( Ax <= b)??

7 ビュー (過去 30 日間)
연승 김
연승 김 2021 年 10 月 2 日
コメント済み: Sanush 2021 年 10 月 20 日
Hi. I'm not good at MATLAB so please help me^^
I want to solve LP problem using "linprog",
so I have to make inequality constraints to A matrix and b constants. (Ax <= b)
And my inequality constraint's variables are mixed each other, so I have to arrange the constraints.
For example,
x1 + x2 <= x3 + x4 + 1 ; % constraint 1
X3 <= x1 + 10 ; % constraint 2
x2 + x4 <= 7 ; % constraint 3
In this situation, how can I make shape "Ax <= b" automatically??
It means I don't have to code one by one.
  1 件のコメント
Sanush
Sanush 2021 年 10 月 20 日
Rearrange your constraint
Px1 <= Qx2 + c
(P - Q - c)*(x1', x2', 1)' <= 0
% for x1 + x2 <= x3 + x4 + 1
P = x1 + x2
Q = x3 + x4 + 1
% As Ax <= b
A = P - Q
b = zeros(size(A,1),1)

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 10 月 2 日
編集済み: Matt J 2021 年 10 月 2 日
Using the problem-based solvers, you do not have to build things in matrix form, e.g.,
x=optimvar('x',4,'LowerBound',0,'UpperBound',20);
con(1)=x(1) + x(2) <= x(3) + x(4) + 1 ; % constraint 1
con(2)=x(3) <= x(1) + 10 ; % constraint 2
con(3)=x(2) + x(4) <= 7 ; % constraint 3
sol=solve( optimproblem('Objective',-sum(x),'Constraints',con) )
Solving problem using linprog. Optimal solution found.
sol = struct with fields:
x: [4×1 double]
However, if you really must have the matrices, then you can use prob2matrices,
p=prob2matrices({x},'Constraints',con)
p = struct with fields:
intcon: [] lb: [4×1 double] ub: [4×1 double] Aineq: [3×4 double] bineq: [3×1 double] Aeq: [] beq: []

その他の回答 (1 件)

Chunru
Chunru 2021 年 10 月 2 日
% Let x = [x1; x2; x3; x4]
% x1 + x2 <= x3 + x4 + 1 ; % constraint 1
% => [1 1 -1 -1]*x <= 1
% X3 <= x1 + 10 ; % constraint 2
% => [-1 0 1 0]*x <= 10
% x2 + x4 <= 7 ; % constraint 3
% => [0 1 0 1]*x <=7
% Putting together
% [ 1 1 -1 -1] [ 1 ]
% |-1 0 1 0| * x <=|10 ]
% [ 0 1 0 1] [ 7 ]
%
% Therefore, you can specify A and b as follows
A = [1 1 -1 -1; -1 0 1 0; 0 1 0 1];
b = [1; 10; 7];
  2 件のコメント
연승 김
연승 김 2021 年 10 月 2 日
I understant what you mean, but my queation is different.
The inequality constraints make automatically by iteration,
so I want to creat A matrix automatically...
Do you understant what I mean??
I'm sorry that confusing you...
Chunru
Chunru 2021 年 10 月 2 日
No. I don't understand you. Can you explain with examples?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeneral Physics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by