Optimization Toolbox Genetic algorithm
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
hello everyone, I am currently working on a maximum coverage problem and i would like to solve it with genetic algorithm solver. But i am having trouble. I am using live editor and in the parameters and data section i calculated some values that i want to use in my objective function. In the same page i have objective function space where i can write my function. But the values i previously calculated in the data section is seem to be not recognized by the function below. So any suggestions on how to solve this problem?
thank you in advance.
채택된 답변
Benjamin Thompson
2022년 3월 13일
0 개 추천
The Live Editor has diferent types of "Run" functions. You probably want "Run" all instread of running a single section. Or if you are talking about the input controls like a slider, right click each control and make sure the options match your requirements. There are usually some options like when the control value changes does it just run the current section, the current section and everything after, or curren section and everything before. If you can post the code that would help give a better answer.
댓글 수: 11
Okey thank you for your answer. I will add the objective function part of the code. So in order to help you understand what i am trying to do with the code let me explain.
So my data set is 36x36 so it is quite large. To calculate the matrices i will use for my function i found something like this ( ind = @(i,j) sub2ind(size(@b),i,j); ) and used it to let the MATLAB know what my matrix sizes are.
Then i tried to create a loop with for. My 'ai' is a 36x1 matrix , 'rij' is a 36*36 matrix and finally 'xij' is also 36x36 matrix. I want the code to multpily the relevant cells and gave me a sum both over i and j indices. But like i told earlier this part does not recognize the inputs. I am aware that i have other problems with this function (like the function handle i wrote ind = @(i,j) sub2ind(size(@b),i,j)). But i am very new to MATLAB and i am trying to solve this by looking to the documentations and examples MATLAB provided and i am having a hard time. So any ideas how to solve it?
thank you for your time!
function z = objectivefunct(ai, rij,xij)
ind = @(i,j) sub2ind(size(@b),i,j);
mn = size (@b);
for i = 1:mn
for j = 1:mn
z = (-1)*(ai(ind(i,j))*xij(ind(i,j))*rij(ind(i,j)));
end
end
end
Please give a couple example inputs and outputs so that we understand better.
okey. So the ai values are the demand population so as an example it takes 158. rij and xij are binary variables so they take 0 or 1. if the ai(1,1) = 158, rij(1,1)=1, xij(1,1) = 1 then it should multpily those values and go to the next indice For ai (1,1), rij(1,2), xij (1,2) and again multpily and sum it with the previous one. I have used function handle (ind) to help me create those values in the parameters section (i calculated those matrices with if stucture using some data).
here is that part; I do not have any problems with this part it does what i want. And it stores the values i want in the workspace.
ind = @(i,j) sub2ind(size(b),i,j);
mn = size (b);
for i = 1:mn
for j = 1:mn
if b(ind(i,j)) <= 4000
rij(ind(i,j)) = 1;
ci(ind(i,1)) = MobileNeed(i,6) ;
else
rij(ind(i,j)) = 0;
end
if ci(ind(i,1)) > 0
xij(ind(i,j)) = 1;
ai(ind(i,1)) = MobileNeed(i,5);
else
xij(ind(i,j)) = 0;
end
end
end
But maybe the problem here is the ind. Because when i run the ind in the command window. I get irrelevant values like 37. I do not know how those values assigned to the ind. therefore, when i run the objective func it searches for 37 in the ai. But i do not have those values in my parameters nor do i need them.
I just what code to use the values in the ai, rij and xij. But since they have indices i tried to solve indice problem with ind but i realized that it is causing more problem than to solve one.
Can you just have a matrix R that is i rows and j columns. Then get rij as R(i, j).
If this does not help we need more information. Provide a complete definition of rij, xij, and the desired output Ai.
So, i eliminated ind and it work just fine. But back to the orginal problem the objective function part still does not recognizes the inputs.
I first run the parameters section so the values can get to the workspace. Then i run the objective function section and i get not enough input argument error.
function z = objectivefunct(ai, rij,xij)
for i = 1:36
for j = 1:36
z = (-1)*(ai(i,j)*xij(i,j)*rij(i,j));
end
end
end
Can you post a full example of your code including how you call objectivefunct and how you define ai, rij, and xij?
Okey so this is how i defined ai, rij, xij. here i defined them as zeros(i,j) to create a matrix with all zeros then i used if structure to change the values.
Then the second part is for objective function. Here is a screenshot. 

ai = zeros(36,1);
rij = zeros(36,36);
xij = zeros(36,36);
for i = 1:36
for j = 1:36
if b(i,j) <= 4000
rij(i,j) = 1;
ci(i,1) = MobileNeed(i,6) ;
else
rij(i,j) = 0;
end
if ci(i,1) > 0
xij(i,j) = 1;
ai(i,1) = MobileNeed(i,5);
else
xij(i,j) = 0;
end
end
end
Yes but where is code that actually calls objectivefunct? What is MobileNeed? Please just post the whole file and not parts of it. You can use the paperclip button to attach a file to this post.
Azime Beyza Ari
2022년 3월 14일
편집: Azime Beyza Ari
2022년 3월 14일
Here is the file. I want ai to be equal to the 5th column of MobileNeed if the 6th column is bigger than 0.
It does not look like the Optimize task for Live Editor is meant for matrix-valued inputs. It does not generate anything for the lb lower bound argument to ga other than zeros(V, 1). And if you try setting V to 36x36 to make the problem two-dimensional the call to zeros fails.
Maybe you leave xij as a 1296x1 vector as the input to objectivefunct and in objectivefunct you convert to a temporary 36x36 matrix for your calculation, like this:
function z = objectivefunct(ai, rij,xij)
temp = reshape(xij,36,36);
for i = 1:36
for j = 1:36
z = (-1)*(ai(i,1)*temp(i,j)*rij(i,j));
end
end
end
This seem to run and do something at least. Hopefully it is doing what you want.
Thank you very very much for your time!! I will Try it.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
참고 항목
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)
