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?
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.
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:
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.
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!
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
