0
votes

I am using the MATLAB optimtool for genetic algorithm optimization.

I am opening a new script with the name 'm_0a4'
FitnessFunction = @m_0b4;
NumberOfVariables = 1;
[x,fval] = ga(FitnessFunction, numberOfVariables);%Here I minimize the difference y

I am opening a second new script naming it 'm_0b4'
function y = m_0b4(x)
prompt = 'write it down';
i = input(prompt) %the input value
y = x - i; % the variable I want to minimize

The functionnn m_0b4 request me for a value 'i' and I am typing the input,
The script m_0a4 of genetic algorithm calls the 'y'.

As I type the value, MATLAB keep on requesting me again for a value. When I am typing 'Enter' MATLAB brings me an error.

Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Caused by: Failure in user-supplied fitness function evaluation. GA cannot continue.

I cant think why MATLAB doesn't understand that I give an input (for example the number 5). Is there any idea?

Thank you in advance!

2
It's pretty weird that your fitness function will request an input from the user. Do you realize that everytime ga calls your FitnessFunction it will ask the user for an input? - BillBokeey
Yes, I did realize that but ga must call my FitnessFunction one time. By the way I believe, it's weird that my fitness function will be requesting an input from the user many times. I say that because when I simply type for example i = 1 my ga works well and calls the FitnessFunction one time. - panos deemac
ga will work iteratively by calling the fitness function as many times as needed to be within the default tolerance from the minimum (it actually stops when two subsequent calls to the fitness function give the same result). If you define i=1 you get rid of the problem by not requesting user input. Please have a look at my answer - BillBokeey
Thank you for your answer! Then there isn't any way of user typing the value i for all the iterations. - panos deemac
Well, changing the definition of the function to minimize at each iteration would make no sense at all. What exactly do you want to do? - BillBokeey

2 Answers

2
votes

I think what you should do would be to ask the user for the input once before the call to ga.

In order for your function m_0b4 to take this input into account, you can declare FitnessFunction as an anonymous function of 1 argument :

Your new function :

function y = m_0b4(x,i)
y = x - i;
end

In the Main script :

prompt = 'write it down';
i = input(prompt)

% Declare your fitness function that will be executed with the input entered by the user 
FitnessFunction=@(x) m_0b4(x,i);

% Minimize
NumberOfVariables = 1;
[x,fval] = ga(FitnessFunction, NumberOfVariables);

Minor note (Major problem):

The function you're trying to minimize tends to -infinity when x tends to -infinity....


Working example (One that admits a minimum)

function y = m_0b4(x,i)
y = abs(x - i);
end

Result :

With an input i set to 20 (And a maximum of 100 generations) :

x =

20.012668610027522

fval =

0.012668610027522

1
votes

I cannot say why your code does not work, but here a workaround:

function y = m_0b4(x)
i = input('write it down \n')
y = x - i;