Matlab 为什么我的代码(基于遗传算法优化工具)无法读取作为输入的值?

Matlab 为什么我的代码(基于遗传算法优化工具)无法读取作为输入的值?,matlab,optimization,genetic-algorithm,Matlab,Optimization,Genetic Algorithm,我正在使用MATLAB优化工具进行遗传算法优化 我正在打开一个名为“m_0a4”的新脚本 FitnessFunction=@m_0b4 NumberOfVariables=1 [x,fval]=ga(FitnessFunction,numberOfVariables);%在这里,我最小化了差异y 我正在打开第二个新脚本,命名为“m_0b4” 函数y=m_0b4(x) prompt='写下来' i=input(prompt)%input值 y=x-i;%我要最小化的变量 函数nn m_0b4请求我

我正在使用MATLAB优化工具进行遗传算法优化

我正在打开一个名为“m_0a4”的新脚本
FitnessFunction=@m_0b4
NumberOfVariables=1
[x,fval]=ga(FitnessFunction,numberOfVariables);%在这里,我最小化了差异y

我正在打开第二个新脚本,命名为“m_0b4”
函数y=m_0b4(x)

prompt='写下来'
i=input(prompt)%input值
y=x-i;%我要最小化的变量

函数nn 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;

我无法解释为什么您的代码不起作用,但这里有一个解决方法:

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);
在主脚本中:

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趋于无穷大时,你试图最小化的函数趋于无穷大


工作示例(最小允许值) 结果:

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);
输入
i
设置为20(最多100代):

x=

20.012668610027522

fval=

0.012668610027522


我认为您应该做的是在调用
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);
在主脚本中:

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趋于无穷大时,你试图最小化的函数趋于无穷大


工作示例(最小允许值) 结果:

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);
输入
i
设置为20(最多100代):

x=

20.012668610027522

fval=

0.012668610027522


谢谢你的回答。我们遇到了同样的问题。当我在你的路上写信时,我面临着同样的症状。谢谢你的回答。我们遇到了同样的问题。当我在你的路上写作时,我面临着同样的症状。你的健身功能会要求用户输入,这真是太奇怪了。您是否意识到,每次
ga
调用FitnessFunction时,它都会要求用户输入信息?是的,我确实意识到,但是
ga
必须调用我的FitnessFunction一次。顺便说一句,我相信我的健身功能会多次请求用户输入,这很奇怪。我之所以这么说是因为当我简单地键入例如
I=1
my
ga
时,它工作得很好,并且只调用了一次FitnessFunction。
ga
将通过反复调用FitnessFunction来工作,只要调用次数在最小值的默认公差范围内即可(当对fitness函数的两个后续调用给出相同的结果时,它实际上停止)。如果你定义了
i=1
,你就可以通过不请求用户输入来解决这个问题。请看我的回答谢谢你的回答!那么用户就没有办法在所有迭代中键入值
i
。那么,在每次迭代中将函数的定义更改为最小化将毫无意义。什么例子你想做什么?你的健身功能会要求用户输入,这很奇怪。你知道吗,每次
ga
调用你的健身功能,它都会要求用户输入吗?是的,我知道,但是
ga
必须调用我的健身功能一次。顺便说一句,我相信,我的健身功能on将多次请求用户输入。我之所以这么说是因为当我简单地键入例如
I=1
my
ga
时,它工作得很好,并调用FitnessFunction一次。
ga
将通过反复调用适应度函数来工作,只要调用次数在最小值的默认公差范围内即可(当对fitness函数的两个后续调用给出相同的结果时,它实际上停止)。如果你定义了
i=1
,你就可以通过不请求用户输入来解决这个问题。请看我的回答谢谢你的回答!那么用户就没有办法在所有迭代中键入值
i
。那么,在每次迭代中将函数的定义更改为最小化将毫无意义。什么例子你想做什么?为什么有必要通过
FitnessFunction=@(x)m_0b4(x,i);?
的方式将我的适应度函数指定并声明为匿名函数?
这是因为你希望你的适应度函数只依赖于一个变量(
x
),而不是在用户输入
i
上。通过以这种方式声明函数,它将始终采用相同的预定义用户输入
i
,并使
x
在过程中发生变化。为什么有必要通过
FitnessFunction=@(x)m_0b4(x,i);?
的方式将我的适应度函数指定并声明为匿名函数