哪个是制作调用仿真软件并有权访问结果的 MATLAB ga 函数的最佳方法?
Which is the best way to make a MATLAB ga function that calls the simulation software and has access to the result?
我使用
MATLAB遗传算法应用optimtool.
主要:
function [x, fval, exitFlag, Output, population, SCORE] = main()
nVar = 1; FF = @opt;
k = 1;
opts = gaoptimset;
opts = gaoptimset(opts,'InitialPopulation',k,'PopulationSize',1);
[x, fval, exitFlag, Output, population, SCORE] = ga(FF,nVar,opts);
Objective函数:
function [y] = opt(t)
y = abs( t - 1 );
我观察到,当我将 objective 函数作为一个简单的方程进行操作时,ga 运行良好。在每次迭代中,我期望 objective 函数的变量 t
被分配给个体 k
的值。 但是我提到这个分配是自动发生的。
我的最终目的是利用这个简单的问题在方程y = abs( t - 1 )
之前插入一个调用模拟软件的命令。该软件读取总体 k
中的个体作为输入并输出变量 r
。等式 y = abs( t - 1 )
的变量 t
赋值给 r
.
问题:
我想让ga让我制作我的模拟软件来读取k
的值
手动,因为这在前面的示例中发生 自动 (t <-- k)。
使用 ga 应用程序是否可行?有什么想法吗?
提前致谢!
您可以通过使用外部函数来做到这一点,该函数使用系统调用 运行 您的模拟软件并操纵它的输出。
例如:
ga(@(x1)my_external_obj_fn(x1,param1,param2,...),nelements, nvars,A,b,[],[],LB,UB,@my_nonlcon_fn,@my_IntCon_fn,options)
...
在你的 output = my_external_obj_fn
...
sim_fx = sprintf('your_sim_exec param1 param2... %s > outputfile');
system(sim_fx);
getoutput = sprintf('grep outputfile | sed > out_fitness')
system(getoutput)
fID = fopen(getoutput)
if fID ~= -1
output = textscan(fopen(fx2), '%s');
output = str2double(output)
...
希望对您有所帮助
我使用 MATLAB遗传算法应用optimtool.
主要:
function [x, fval, exitFlag, Output, population, SCORE] = main()
nVar = 1; FF = @opt;
k = 1;
opts = gaoptimset;
opts = gaoptimset(opts,'InitialPopulation',k,'PopulationSize',1);
[x, fval, exitFlag, Output, population, SCORE] = ga(FF,nVar,opts);
Objective函数:
function [y] = opt(t)
y = abs( t - 1 );
我观察到,当我将 objective 函数作为一个简单的方程进行操作时,ga 运行良好。在每次迭代中,我期望 objective 函数的变量 t
被分配给个体 k
的值。 但是我提到这个分配是自动发生的。
我的最终目的是利用这个简单的问题在方程y = abs( t - 1 )
之前插入一个调用模拟软件的命令。该软件读取总体 k
中的个体作为输入并输出变量 r
。等式 y = abs( t - 1 )
的变量 t
赋值给 r
.
问题:
我想让ga让我制作我的模拟软件来读取k
的值
手动,因为这在前面的示例中发生 自动 (t <-- k)。
使用 ga 应用程序是否可行?有什么想法吗?
提前致谢!
您可以通过使用外部函数来做到这一点,该函数使用系统调用 运行 您的模拟软件并操纵它的输出。
例如:
ga(@(x1)my_external_obj_fn(x1,param1,param2,...),nelements, nvars,A,b,[],[],LB,UB,@my_nonlcon_fn,@my_IntCon_fn,options)
...
在你的 output = my_external_obj_fn
...
sim_fx = sprintf('your_sim_exec param1 param2... %s > outputfile');
system(sim_fx);
getoutput = sprintf('grep outputfile | sed > out_fitness')
system(getoutput)
fID = fopen(getoutput)
if fID ~= -1
output = textscan(fopen(fx2), '%s');
output = str2double(output)
...
希望对您有所帮助