Matlab中的遗传算法可以将第二个返回值从适应度函数传递到约束吗? [英] Can the genetic-algorithm in Matlab pass a second return value from the fitness-function to the constraints?

查看:301
本文介绍了Matlab中的遗传算法可以将第二个返回值从适应度函数传递到约束吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Matlab中模拟间歇式蒸发器.遗传算法会改变多个初始变量x(例如大小,工作流体的最大质量,使用的蒸发器的总数...),目的是使效率最大化.因此,我的函数evaporator(x)返回的负效率已通过算法最小化.
除了效率,还计算了其他几个值.一个是模拟周期的持续时间(不是计算本身的运行时间!).作为约束条件,整个蒸发周期的持续时间应视蒸发器的数量而定(例如,如果使用三个蒸发器,则整个周期至少应为3 * 5s = 15s).
我知道我可以轻松使用ga(nonlcon)的非线性约束选项.在这种情况下,我只需要再次根据evaporator(x)进行相同的计算,但是这次返回计算出的持续时间.问题是,每次运行我都必须调用一个外部DLL数千次,并且计算变得非常缓慢.因此,我真的想避免两次运行所有计算.
evaporator(x)是否有可能同时返回负效率和持续时间? ga然后应最小化负效率,并评估有关约束的持续时间.我曾考虑过将evaporator(x)嵌套在一个匿名函数中,但是我认为我的函数仍然必须被调用两次吗?

I am simulating a batch evaporator in Matlab. The genetic algorithm varies several starting variables x (such as size, max. mass of working fluid, the overall number of evaporators used ...) and the goal is to maximize the efficiency. So my function evaporator(x) returns the negative efficiency which is minimized by the algorithm.
Besides the efficiency, there are several other values calculated. One is the duration of the simulated cycle (NOT the runtime of the calculation itself!). As a constraint, the duration of the whole evaporation cycle should, depending on the number of evaporators, not be to short (e.g. if three evaporators are used, the whole cycle should take at least 3*5s = 15s).
I know that I can easily use the nonlinear constraints option of ga (nonlcon). In this case I would just have to do the same calculations from evaporator(x) again, but return the calculated duration this time. The problem is, that I have to call an external DLL several thousand times per run and the calculations become really slow. Therefore I really want to avoid running all the calculations twice.
Is it somehow possible, that evaporator(x) returns both the negative efficiency and the duration at the same time? ga should then minimize the negative efficiency and evaluate the duration regarding the constraints. I thought about nesting the evaporator(x) inside an anonymous function, but I think my function still has to be called twice then?

我同时具有效率和持续时间的多目标优化方面的丰富经验,但是不幸的是,gamultiobj算法无法处理整数变量.

I had good experience with the Multiobjective Optimization of the efficiency and the duration at the same time, but unfortunately the gamultiobj algorithm cannot handle integer variables.

目前,我正在对evaporator(x)内的短持续时间使用惩罚函数,但我认为ga算法的约束处理会比这更好.

At the moment I am using a penalty function on short durations inside evaporator(x), but I think, that the constraints handling of the ga algorithm would be better than this.

修改: 因此它变得有点复杂,但最终它可以正常工作:

So it got a bit more complicated, but in the end it is working:

function [ returnValue ] = switchHXoutput( requestedCase, inputs )
%SwitchHXoutput returns fitness value or duration depending on requested
%case

persistent fitnessValue duration oldInputs;

if isempty(oldInputs)
    oldInputs = {}; %initialize oldInputs as cell
end


[isAllreadyCalculated, iOldInput] =...
ismember(cell2mat(inputs), cell2mat([oldInputs{:}]), 'rows'); 

if isempty(oldInputs) || ~isAllreadyCalculated
    [fitnessValue(end+1), duration(end+1)] = lengthyCalculation(inputs); %add current results to persistent array
    oldInputs(end+1) = {inputs}; %add current inputs to persistent array
    returnValue = [fitnessValue(end), duration(end)];
else
    returnValue = [fitnessValue(iOldInput), duration(iOldInput)]; % return old values
end

if strcmp(requestedCase, 'FitnessValue')
    returnValue = returnValue(1);
elseif strcmp(requestedCase, 'duration')
    returnValue = returnValue(2);
else
    error('MyApp:SelectOutput','Requested case not supported')
end

end %function

我知道,不断增长的细胞阵列并不是很快.但是由于我的lengthyCalculation每次通话大约需要2分钟,因此我仍然节省了很多时间.此外,每一代的最佳个人将在下一代中再次使用.因此,在这种情况下,可以使用保存的值,而不必再次重新计算它们.该代码在并行计算中也可以正常工作.通常,健身值和持续时间是由同一名工人计算的.仅对于子孙后代,结果可以在其他工作线程上进行.

I know that a growing cell array isn't exactly fast. But as my lengthyCalculation takes about 2 minutes for each call I am still saving a lot of time. Furthermore the best individuals of each generation are used again in the next generation. So in this case the saved values can be used instead of recalculating them again. The code works also fine with parallel computation. Normally the fitness value and the duration are calculated by the same worker. Only for following generations the results can be on another worker.

推荐答案

一个选项是包括一些持久状态,该状态记录上次运行时计算出的效率和持续时间(这是 memoization )并根据功能输入确定要返回的内容.

One option is to include some persistent state, which records both the efficiency and duration computed on the last run (this is memoization) and decide which to return based on the function inputs.

例如,

function return_value = evaporator(method, value, inputs)

  persistent efficiency duration;

  if strcmp(method, 'recalculate')
      % Some lengthy calculation goes here
      efficiency = inputs + 1;
      duration   = inputs + 10;
      pause(5);
  end

  switch value
    case 'efficiency'
      return_value = efficiency;
    case 'duration'
      return_value = duration;
  end

end

结果是

>> evaporator('recalculate', 'efficiency', 10)
11
>> evaporator('memoized', 'duration')
20
>> evaporator('recalculate', 'efficiency', 20)
21
>> evaporator('memoized', 'duration')
30

这篇关于Matlab中的遗传算法可以将第二个返回值从适应度函数传递到约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆