matlab将数扩大为整数,在Matlab中使用遗传算法时如何设置整数约束?(How to set the integer constraints when use the Genetic Algor…

  • Post author:
  • Post category:其他

在Matlab中使用遗传算法时如何设置整数约束?(How to set the integer constraints when use the Genetic Algorithm in Matlab?)

如果有人能告诉我如何设置变量的整数约束(即,它们的变量只能是0或1),我将不胜感激。

x = ga(fitnessfcn,nvars);

It would be much appreciated if anybody can tell me how to set the integer constrain for the variables(i.e., they variables can only be either 0 or 1).

x = ga(fitnessfcn,nvars);

原文:https://stackoverflow.com/questions/37394612

更新时间:2020-02-05 07:53

最满意答案

使用可选的ga函数参数LB (下限), UB (上限)和IntCon (整数约束)。 您要使用的MATLAB遗传算法函数的签名是:

x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)

例如,您可以使用MATLAB GA来解决10个二进制变量问题,如下所示:

% Number of variables

nVars = 10

% Lower and upper bounds

LB = zeros(1, nVars);

UB = ones(1, nVars);

% Variables with integer constraints (all in this case)

IntCon = 1:nVars;

% Run the GA solver

x = ga(fitnessfcn, nVars, [], [], [], [], LB, UB, [], IntCon);

请注意,线性不等式约束A和b以及非线性约束nonlcon是可选的,如果它们不存在,可以用[]替换。

Use the optional ga function parameters LB (lower bound), UB (upper bound) and IntCon (integer constraints). The signature for the MATLAB genetic algorithm function that you want to use is:

x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)

For example, you could use the MATLAB GA to solve a 10 binary variable problem as follows:

% Number of variables

nVars = 10

% Lower and upper bounds

LB = zeros(1, nVars);

UB = ones(1, nVars);

% Variables with integer constraints (all in this case)

IntCon = 1:nVars;

% Run the GA solver

x = ga(fitnessfcn, nVars, [], [], [], [], LB, UB, [], IntCon);

Notice that the linear inequality constraints A and b, and non-linear constraints nonlcon are optional and can be replaced with [] if they don’t exist.

2016-05-23

相关问答

如果你在谈论标准遗传算法,那么假设你有一群N染色体。 在每一代中,通过调用评估函数来计算每种抗体的适合度。 因此,对于G代,G * N中的函数评估总数。 因此,不是设置功能评估的数量,而是设置代数 G =(期望的功能评估数)/(人口规模) 功能评估的数量通常用于比较而不是代数,因为: 不同的算法每代执行不同数量的功能评估。 例如,免疫系统为每种抗体创建C克隆并评估克隆,因此功能评估的数量为G * N * C 评估函数通常是算法中计算量最大的部分。 If you are talking about

当它执行你的函数two_min时,它将传入x的值,并且z在使用之前不会在任何地方定义,因此你的错误。 您可以尝试的一件事是在调用ga之前绑定您的z值: a = 500; %the value that will be used for z

fitness_fun = @(x) two_min(x,a);

n_var = 1;

[x,fval] = ga(fitness_fun,n_var)

请注意,这只会将fitness_fun创建点的a值作为z的值,因此如果稍后再次更改“a”的值,则不会对tw

您可以使用绘图功能 。 您可以使用其中一个预定义的绘图函数: options = gaoptimset(‘PlotFcns’,@gaplotbestf);

x= ga(@f,6,[],[],[],[],[],[],[],options)

或者你可以自己写。 例如: function gademo

options= gaoptimset(‘PlotFcns’,@myplot,’PopulationSize’,10);

x= ga(@f,6,[],[],[],[],[],[],[],

解决这个问题的常用技术是施加“惩罚”,其中,任何多于三个的染色体都会增加罚分。 例如,如果一个染色体具有五个1,则将两倍添加到染色体适应性评分。 在这种情况下,任何有三个以上1的染色体逐渐从人群中移除,并允许其他(具有三个或更少的1个)个体在群体中维持。 A common technique to solve this problem is to impose a “penalty”, wherein, any chromosome that have more than three 1 have

使用可选的ga函数参数LB (下限), UB (上限)和IntCon (整数约束)。 您要使用的MATLAB遗传算法函数的签名是: x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)

例如,您可以使用MATLAB GA来解决10个二进制变量问题,如下所示: % Number of variables

nVars = 10

% Lower and upper bounds

LB = zeros(1, nVars);

UB = ones(

我之前遇到过类似的问题,我希望它和你的问题一样 首先,您需要检查(使用任何测量指标)染色体A是否优于染色体B.这使您可以对人口的染色体进行严格的排序,并能够对您的人口进行排序。 然后,当你产生一个新的染色体(通过突变或交叉)时,你可能会产生一个已经存在于你的人群中的染色体。 请确保不要将其包含在人口列表中。 换句话说,确保您的列表始终包含不同的染色体,并始终从最佳到最差排序! 注意:我使用的遗传算法通常是这样的(这是最常用的算法,也是最常用的算法): 创建P不同的染色体并将它们添加到Pop列表中;

如果你想为每个人分配不同的随机值,那么将一个repmat作为一个分配就不会有帮助,因为这只会复制相同的东西50次。 你最好只使用一个简单的循环: population=50;

individual=struct(‘genes’,[],’fitness’,0);

for m=1:50

individual(m).genes=rand(1,50)>=0.5;

end

这并不比分配所有这些并且然后循环一样有效 – 在每种情况下,基因数组仅被分配一次。 此外,分配和重新分配50个单元格并不会非常

以下是您可以修改的停止条件的完整列表: % default options

options = optimoptions(‘ga’);

% stop criteria

options = optimoptions(options,’MaxGenerations’, MaxGenerationsVal); % number of generations

options = optimoptions(options,’MaxTime’, MaxTimeVal); % time limit

optio

这是一个混合整数优化问题,它可以在MATLAB中使用ga来解决。 如文档中所述: ga can solve problems when certain variables are integer-valued. 并非所有变量都是某些变量。 所以你应该在其他整数中至少有一个实变量。 Whit IntCon选项,您可以指定哪些变量是整数,例如IntCon=[1 3]表示您的第一个和第三个变量是整数。 为了避免两个整数变量同时为0 ,我认为你可以添加一些不等式约束。 例如,请看以下示例: 假设我们想要找

请参阅InitialPopulation选项中的gaoptimset和gaoptimset 。 您可以初始化2到20范围内的整数序列作为初始总体。 然后你可以使用IntCon。 要么 作为myFitnessfcn的第一个声明 model=round(model);

if model > 20 || model < 2

fitness=1e20;

else

% evaluate the original fitness function

end

这样,输入到您的适应度函数的模型参数总是