Optimization Toolbox Genetic algorithm

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.

 采纳的回答

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.
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 个)

类别

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by