How can i sum my objective function with each iteration
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello everyone,
I am trying to do an maximiztion with optimization toolbox using genetic algorithm solver. What i am trying to do is i have a function with 2 parameters and 1 variable. I have a for loop and in the loop i wanna do this.
for i = 1:1
for j = 1:1296
z = z + (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
end
end
I wanna multpily the x,r, and a for each iteration and sum every iteration.
like this;
for (1,1)
x(1,1)*r(1,1)*a(1,1) = 188
for (1,2)
x(1,2)*r(1,2)*a(1,2)=74 +188= 262 (you get the idea)
Any suggestions on how to do this?
Because when i write it this was i get an error.
z = z + (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
Thank you in advance,
Beyza.
採用された回答
Matt J
2022 年 3 月 22 日
sum(x.*r.*a,'all')
16 件のコメント
Should i write it outside the for loop or inside it?
Matt J
2022 年 3 月 22 日
You should not have any loops.
so here is the full version;
function [xij,fval] = objectivefunc(r,aii,xij)
[xij,fval] = fminunc(@nestedfun,xij);
function z = nestedfun(xij)
for i = 1:1
for j = 1:1296
z = (-1)*(xij(36*i+j-36)*aii(i,j)*r(i,j));
end
end
end
end
What i need to do is;
function [xij,fval] = objectivefunc(r,aii,xij)
[xij,fval] = fminunc(@nestedfun,xij);
function z = nestedfun(xij)
sum(xij.*r.*aii, 'all');
end
end
Is it the correct use?
Do you only want to test "fminunc" ?
The result is obvious:
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
Matt J
2022 年 3 月 22 日
Your code bears no resemblance to your description of the desired operation. This is what you said you wanted:
for (1,1)
x(1,1)*r(1,1)*a(1,1) = 188
for (1,2)
x(1,2)*r(1,2)*a(1,2)=74 +188= 262 (you get the idea)
I am saying that sum(x.*r.*a, 'all') implements that part of your post
sorry if i was not able to clearly define my problem.
I have these matrixes of x,r,a with 1x1296 dimensions and i wanna multpily the same cells of these matrixs and as a result i want the summation of all 1296 multiplication.
No i do not want to test it. Genetic algorithm solver does not allows me to use parameters therefore i ttried to write a nested function in order to pass parameters to my objective function. And since this is a maximization problem i thought i should multiply this with -1 and then it would be maximization instead of minimization(Cause the solver solves it to minimize). And I wanna found the maximum value, and with this format it only gives me the last ones value since there is not summation operation going on.
In my case the last value is 334 and as a solution it always converges to 334. But instead it should converge to 5k or 6k.
Perhaps x is a column vector. Then you'll have to use
z = sum((x.').*r.*a)
But as I said: The optimal x is given by
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
That's why I asked if this is only a test problem for "fminunc".
Three of the matrixes are row vector with dimensions 1x1296.
I could not understand what this supposed to mean to me
x(i,j) = +Inf if a(i,j)*r(i,j) >= 0,
x(i,j) = -Inf if a(i,j)*r(i,j) < 0.
x should be binary variable but could not make it binary right now it takes values between 1-0. r and a are parameters. r is also binary. a is demand.
I used fminunc because i looked it from this website:
Matt J
2022 年 3 月 22 日
I have these matrixes of x,r,a with 1x1296 dimensions and i wanna multpily the same cells of these matrixs and as a result i want the summation of all 1296 multiplication.
That is what I gave you originally.
Torsten
2022 年 3 月 22 日
For a binary problem, fminunc is not suited.
Use intlinprog or bintprog or - if your objective function or other constraints are nonlinear - ga instead.
I just wanna be more clear so the phyton equivalent of what i want to do is like this.
z += x*r*a
What is the matlab way to this?
No, the Python equivalent of what you describe is,
z=sum(x*r*a)
My original answer gives the Matlab equivalent of this.
@Torsten yes i am using ga solver. So ı wrote the objective function entirely wrong? Any suggestions on how to correct it? Like i told earlier i have parameters that i should pass to the objective function and using nested function was recommended to me.
@Matt J okey thank you!
Matt J
2022 年 3 月 22 日
You're welcome. If the problem is now resolved, though, please do Accept-click the answer.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Solver Outputs and Iterative Display についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
