我正在编写一个程序来使用Robert Lang的算法从他的网页上优化打包问题:https://langorigami.com/wp-content/uploads/2015/09/ODS1e_Algorithms.pdf
这用于设计折纸模型,这是带有约束和不同半径的圆形包装的问题。
我创建了一个有5个节点的example,其中4个是终端。看看前面算法中包含的这些definitions,我想使用Scale Optimization(A.3)。
根据符号,在我的例子中我有:
E = {e1, e2, e3, e4,} ---> Edges
U = {u1, u2. u3, u4, u5} ---> Vertices (Each one with x and y coordinates)
Ut = {u2, u3, u4, u5} ---> Terminal Vertices
P = {[u1, u2], [u1, u3], [u1, u4], [u1, u5],
[u2, u3], [u2, u4], [u2, u5],
[u3, u4], [u4, u5]} ---> All paths between vertices from U
Pt = {[u2, u3], [u2, u4], [u2, u5],
[u3, u4], [u3, u5],
[u4, u5]} ---> All paths between terminal vertices Ut
如果我假设σ等于0:
L = {100, 50, 100, 100,
150, 200, 200,
150, 150,
200} --> Length of the paths P
Note that the length of each path is in the same position of P.
所以,现在,定义了所有这些,我必须优化规模。为此,我应该:
最小化(-m)超过{m,ui∈Ut} s.t。:( A-2)
1.-0≤ui,x≤w表示所有ui∈Ut(A-3)
2.-0≤ui,y代表h所有ui∈Ut(A-4)
3.-(A-5)来自我提供的网页。
因此,在示例中,合并(A-3)和(A-4)我们可以为fmincon函数设置Lb和Ub,例如:
Lb = zeros(1,8)
Ub = [ones(1,4)*w, ones(1,4)*h]
fmincon函数的输入x,由于拒绝矩阵,我使用它像:
X = [x1 x2 x3 x4 y1 y2 y3 y4],这就是Lb和Ub具有这种形式的原因。
关于(A-5),我们得到了这组不等式:
m*150 - sqrt((x(2)-x(3))^2 + (y(2)-y(3))^2) <= 0
m*200 - sqrt((x(2)-x(4))^2 + (y(2)-y(4))^2) <= 0
m*200 - sqrt((x(2)-x(5))^2 + (y(2)-y(5))^2) <= 0
m*150 - sqrt((x(3)-x(4))^2 + (y(3)-y(4))^2) <= 0
m*150 - sqrt((x(3)-x(5))^2 + (y(3)-y(5))^2) <= 0
m*200 - sqrt((x(4)-x(5))^2 + (y(4)-y(5))^2) <= 0
我的主程序是testFmincon.m:
[x,fval,exitflag] = Solver(zeros(1,10),zeros(1,10),ones(1,10)*700);
%% w = h = 700
我在Solver.m文件中实现了优化器:
function [x,fval,exitflag,output,lambda,grad,hessian] = Solver(x0, lb, ub)
%% This is an auto generated MATLAB file from Optimization Tool.
%% Start with the default options
options = optimoptions('fmincon');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(@Objs,x0,[],[],[],[],lb,ub,@Objs2,options);
Objs2.m在哪里:
function [c, ceq] = Objs2(xy)
length = size(xy);
length = length(2);
x = xy(1,1:length/2);
y = xy(1,(length/2)+1:length);
c(1) = sqrt((x(2) - x(3))^2 + (y(2)-y(3))^2) - m*150;
c(2) = sqrt((x(2) - x(4))^2 + (y(2)-y(4))^2) - m*200;
c(3) = sqrt((x(2) - x(5))^2 + (y(2)-y(5))^2) - m*200;
c(4) = sqrt((x(3) - x(4))^2 + (y(3)-y(4))^2) - m*150;
c(5) = sqrt((x(3) - x(5))^2 + (y(3)-y(5))^2) - m*150;
c(6) = sqrt((x(4) - x(5))^2 + (y(4)-y(5))^2) - m*200;
ceq=[x(1) y(1)]; %% x1 and y1 are 0.
end
而Objs.m文件是:
function c = Objs(xy)
length = size(xy);
length = length(2);
x = xy(1,1:length/2);
y = xy(1,(length/2)+1:length);
c(1) = sqrt((x(2) - x(3))^2 + (y(2)-y(3))^2) - m*150;
c(2) = sqrt((x(2) - x(4))^2 + (y(2)-y(4))^2) - m*200;
c(3) = sqrt((x(2) - x(5))^2 + (y(2)-y(5))^2) - m*200;
c(4) = sqrt((x(3) - x(4))^2 + (y(3)-y(4))^2) - m*150;
c(5) = sqrt((x(3) - x(5))^2 + (y(3)-y(5))^2) - m*150;
c(6) = sqrt((x(4) - x(5))^2 + (y(4)-y(5))^2) - m*200;
c = m*sum(c);
end
但我没有正常工作,我认为我使用了错误的fmincon功能。我不知道如何优化(-m)...我应该使用syms m还是类似的东西?
编辑:这样的输出总是[0 0 0 0 0 0 0 0 0 0]不应该。见output here。
非常感谢你的建议。
更多观察。
我们可以通过摆脱平方根来简化一些事情。所以约束看起来像:
set c = {x,y}
maximize m2
m2 * sqrpathlen(ut,vt) <= sum(c, sqr(pos(ut,c)-pos(vt,c))) for all paths ut->vt
其中m2是m的平方。
这确实是非凸的。通过全局求解器,我得到了解决方案:
---- 83 VARIABLE m2.L = 12.900 m^2, with m=scale
PARAMETER m = 3.592
---- 83 VARIABLE pos.L positions
x y
u3 700.000 700.000
u4 700.000 161.251
u5 161.251 700.000
(pos(u2,x)和pos(u2,y)为零)。
使用0作为起点的局部求解器,我们看到根本没有移动:
---- 83 VARIABLE m2.L = 0.000 m^2, with m=scale
PARAMETER m = 0.000
---- 83 VARIABLE pos.L positions
( ALL 0.000 )