# Setting up linear optimization problem

1 ビュー (過去 30 日間)
Inna Pelloso 2022 年 5 月 27 日
コメント済み: Inna Pelloso 2022 年 5 月 28 日
Hi,
I have two 100x1 arrays, X and Y. How do I set this linear problem to run using the optimization toolbox solver?
I want to find the minimum postive value of X, call it M, subject to these constraints:
(1) when the value of X is greater than M, a new variable, Z equals 1
(2) when the value of X is less than -M, the new variable Z equals -1
(3) if -M<X<M, then the new variuable Z equals 0.
I want to find the that maximizes the sum of Y*Z.
Any help would be appreciated!
IP
##### 5 件のコメント表示非表示 4 件の古いコメント
Inna Pelloso 2022 年 5 月 27 日
Correct. I only want to test the values in X.

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

### 採用された回答

Matt J 2022 年 5 月 27 日

X=rand(100,1);
Y=rand(100,1);
timeit(@()doOptimization(X,Y))
ans = 0.0015
function doOptimization(X,Y)
fun=@(M)objective(M,X,Y);
Xs=sort(X(:).');
n=numel(X);
Msamps=abs( interp1( Xs, linspace(1,n,2*n-1) ) ); %optimal M must be one of these
Fsamps=arrayfun(fun,Msamps);
Foptimal=max(Fsamps); %optimal objective
Moptimal=min( Msamps(Fsamps==Foptimal) ); %optimal M
end
function fval=objective(M,X,Y)
Z=(abs(X)>=M);
Z(X<-M)=-1;
fval = Y(:).'*Z(:);
end
##### 2 件のコメント表示非表示 1 件の古いコメント
Matt J 2022 年 5 月 28 日
I guess the optimizer can only take simple contraints.
It has nothing to do with the ability to implement constraints. Because your objective is piecewise constant, the above is what an optimizer would have to do anyway.

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

### その他の回答 (1 件)

Torsten 2022 年 5 月 27 日

Since your vectors X and Y are of moderate size, don't use an optimization tool.
I think it's best to proceed as follows:
1. Extract all positive entries of the X-vector.
2. For each of these x-values, calculate the Z vector and evaluate sum(Z*Y).
3. From the sums obtained, choose the maximum sum. If the maximum sum is only attained once, the
corresponding x-value is the solution. If there are several x-values with the maximum sum, choose the
smallest.
##### 6 件のコメント表示非表示 5 件の古いコメント
Inna Pelloso 2022 年 5 月 28 日
Appreciate the insight!

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

### Community Treasure Hunt

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

Start Hunting!