为什么我的代码(基于遗传算法 optimtool)无法读取值作为输入?
Why my code (based on genetic algorithm optimtool) is unable to read a value as input?
我正在使用 MATLAB optimtool 进行遗传算法优化。
我正在打开一个名为 'm_0a4'
的新脚本
FitnessFunction = @m_0b4;
NumberOfVariables = 1;
[x,fval] = ga(FitnessFunction, numberOfVariables);%Here I minimize the difference y
我正在打开第二个新脚本,将其命名为 '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
函数 m_0b4 请求我输入一个值 'i' 我正在输入,
遗传算法的脚本m_0a4调用'y'。
当我键入值时,MATLAB 继续要求我再次输入值。
当我输入 'Enter' 时,MATLAB 给我带来了一个错误。
赋值的非单例 rhs 维度多于
非单例下标
由以下原因引起:
用户提供的适应度函数评估失败。
GA无法继续。
我想不通为什么 MATLAB 不理解我给出的输入(例如数字 5)。有什么想法吗?
提前致谢!
我不能说为什么你的代码不起作用,但这里有一个解决方法:
function y = m_0b4(x)
i = input('write it down \n')
y = x - i;
我认为你应该做的是在调用 ga
.
之前要求用户输入一次
为了让您的函数 m_0b4
考虑此输入,您可以将 FitnessFunction
声明为 1 个参数的匿名函数:
你的新功能:
function y = m_0b4(x,i)
y = x - i;
end
在主脚本中:
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);
次要注意事项(主要问题):
当 x 趋于 -infinity 时,您试图最小化的函数趋于 -infinity....
工作示例(最低限度)
function y = m_0b4(x,i)
y = abs(x - i);
end
结果:
输入 i
设置为 20(最多 100 代):
x =
20.012668610027522
fval =
0.012668610027522
我正在使用 MATLAB optimtool 进行遗传算法优化。
我正在打开一个名为 'm_0a4'
的新脚本
FitnessFunction = @m_0b4;
NumberOfVariables = 1;
[x,fval] = ga(FitnessFunction, numberOfVariables);%Here I minimize the difference y
我正在打开第二个新脚本,将其命名为 '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
函数 m_0b4 请求我输入一个值 'i' 我正在输入,
遗传算法的脚本m_0a4调用'y'。
当我键入值时,MATLAB 继续要求我再次输入值。 当我输入 'Enter' 时,MATLAB 给我带来了一个错误。
赋值的非单例 rhs 维度多于
非单例下标
由以下原因引起:
用户提供的适应度函数评估失败。
GA无法继续。
我想不通为什么 MATLAB 不理解我给出的输入(例如数字 5)。有什么想法吗?
提前致谢!
我不能说为什么你的代码不起作用,但这里有一个解决方法:
function y = m_0b4(x)
i = input('write it down \n')
y = x - i;
我认为你应该做的是在调用 ga
.
为了让您的函数 m_0b4
考虑此输入,您可以将 FitnessFunction
声明为 1 个参数的匿名函数:
你的新功能:
function y = m_0b4(x,i)
y = x - i;
end
在主脚本中:
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);
次要注意事项(主要问题):
当 x 趋于 -infinity 时,您试图最小化的函数趋于 -infinity....
工作示例(最低限度)
function y = m_0b4(x,i)
y = abs(x - i);
end
结果:
输入 i
设置为 20(最多 100 代):
x =
20.012668610027522
fval =
0.012668610027522