我正在考虑购买家用的Matlab Home + Optimization模块,不过我不确定它能做到我想要的。
我有一个外部进程(不是Matlab),它接受输入,运行进程并生成输出。我想将输入和输出绑定到Matlab,以便Matlab可以“优化”这些输入,完全不关心离散过程本身。 Matlab是否具有离散优化功能,或者它的所有优化功能是否都依赖于对流程本身的内部访问?
谢谢!
-Stephen
答案 0 :(得分:3)
如果您的外部进程能够使用任何方法(例如命令行或文件)来同化参数并对外部程序作出响应,是的,可以配置您的目标函数来发送和读取参数和响应数据到外部过程。
对于离散优化,优化工具箱不适用于离散优化问题,但文档提供了关于在目标函数内舍入参数然后在响应变量中再次运行的提示。
例如,这可以是优化棱镜体积的功能 它是用python编写的外部程序编写的(仅用于单个目标遗传算法( ga )的演示目的):function f = optim(x)
%Optimization criteria
l = round(x(1));
h = round(x(2));
w = round(x(3));
%String to produce the external proccess call as a system command
commandStr = ['python -c "print ' num2str(l) ' * ' num2str(h) ' * ' num2str(w) ' "'];
%Execute the system command, status = 0 for good execution
[status, commandOut] = system(commandStr);
%Convert the output of the external program from strin to doble and assign as the response of the optimization funcition
f = str2double(commandOut)
然后你可以使用这个函数optimtool
作为目标:
然后将结果导出到工作区并round()
。
或者使用如下代码进行编程:
function [x,fval] = runOptimization(lb,ub)
options = gaoptimset;
options = gaoptimset(options,'Display', 'off');
[x,fval] =ga(@optim,3,[],[],[],[],lb,ub,[],[],options);
x = round(x)
fval = optim(x)
以
运行[x,fval] = runOptimization([1 1 1],[3 4 5])
注意即可。 round()
仅用于演示文档