Optimization With Genetic Algorithm. Could not define objective Function!

6 views (last 30 days)
Hello everyone,
I am working on a project where i do maximization. I m having trouble with genetic algorithm solver. It is my first time to use both genetic algorithm solver and MATLAB! I have done my research but could not solve a few problems i have. So here are my questions and problems:
1: My problem is an indexed problem where i have multiple matrices (36x36) and my decision variable is x is also a matrix (36x36) so i have 1296 decision variables! I have learned that the solver excepts a vector and gives scalar output. And it is okey But further in the constraints section i need decision variables as a matrix so i can write my constraints. ( Because they also have summation operation).
2: another point is that i have another decsision variable which is not in the objective function directly but in the constraints. This variable is dependent to the x variable. Where the column summation of x is equal to y(j). So i need my x's to be a matrix not an row vector.
3: My objective function does not works! If i just write my variables and parameters as a row vector it gives' not enough input argument error' ! But if i wirte them as matrix's with dimensions 36x36 it gives 'Index in poisiton 1 exceeds array bounds. Index must not exceed 1' error. Just unable to do it right!!!
4: Also my decsision variable should be binary (1 or 0). I have searched this topic but couldnt find any useful information sadly.
Any solution suggestions and opinions are welcome. The file will be attached! Thank you in advance,
Best,
Beyza.

Answers (2)

Matt J
Matt J on 20 Mar 2022
Edited: Matt J on 20 Mar 2022
1: But further in the constraints section i need decision variables as a matrix so i can write my constraints. ( Because they also have summation operation).
You are free to reshape() your variables and/or assign them to separate variables within inside the body of your objective and nonlinear constraint functions.
2: another point is that i have another decsision variable which is not in the objective function directly but in the constraints. This variable is dependent to the x variable. Where the column summation of x is equal to y(j). So i need my x's to be a matrix not an row vector.
x and y must be concatenated together and organized as a row vector when passed to your obejctive and constraints, but again, you can reshape and reorganize them any way you wish inside the workspace of those functions.
3: My objective function does not works! If i just write my variables and parameters as a row vector it gives' not enough input argument error' ! But if i wirte them as matrix's with dimensions 36x36 it gives 'Index in poisiton 1 exceeds array bounds. Index must not exceed 1' error. Just unable to do it right!!!
Your functions should have only one input argument, a row vector containing both x and y.
4: Also my decsision variable should be binary (1 or 0). I have searched this topic but couldnt find any useful information sadly.
Use the intcon input argument to force certain variables to be integers. Use the lb and ub argument to force them to lie between 0 and 1.
  3 Comments
Azime Beyza Ari
Azime Beyza Ari on 20 Mar 2022
Edited: Azime Beyza Ari on 20 Mar 2022
Thank you for your help!
So i went and wrote a nested function in an another file. ( objectivefunc.m )
This time a different error saying that 'Problem appears unbounded'
Fminunc stopped becasue the objective function value is less than or equal to the value of the objective function.
'Fitness function must return a scalar value'
The files are attached. Any ideas why receiving these errors and how to solve it?
Ps.: Where should i write intcon. In the parameters section, or inside the objective function? Cen you please give little example about how to implement intcon.
Best,
Beyza.

Sign in to comment.


Burhan Burak AKMAN
Burhan Burak AKMAN on 20 Mar 2022
Hello,
I try solve your problem. I detected problem int the your local functions. Because dimensions of xij,rij and ai is not same,
xij is 1x1296, rij is 36x36 and ai is 36x1 so that function give error. So that I changed you objective funtion at line 140 like below code. After that code is running.
function z = objectivefunc(xij,rij,ai)
% wanna sum x*r*aii for each i and j indices. So there is double summation
% here. The answer i want is around 5k-6k.
for i = 36
for j = 1:36
z = (-1)*(xij(36*i+j-36)*rij(i,j)*ai(i,1));
end
end
end
  1 Comment
Azime Beyza Ari
Azime Beyza Ari on 20 Mar 2022
Thank you for your suggestion. It helped me to run my code. But still got an error saying not enough input argument. So I tried addtitional things. I commented on Matt J's answer. If you can have a look i would be so appreciated.
Best,
Beyza.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!