做最好的机械设备
全国咨询热线:400-123-4567

solve

发布时间:2024-09-09 13:34:55 人气:

全部折叠

求解由优化问题定义的线性规划问题。

x=optimvar('x');
y=optimvar('y');
prob=optimproblem;
prob.Objective=-x - y/3;
prob.Constraints.cons1=x + y <=2;
prob.Constraints.cons2=x + y/4 <=1;
prob.Constraints.cons3=x - y <=2;
prob.Constraints.cons4=x/4 + y >=-1;
prob.Constraints.cons5=x + y >=1;
prob.Constraints.cons6=-x + y <=2;

sol=solve(prob)
Solving problem using linprog.

Optimal solution found.
sol=struct with fields:
    x: 0.6667
    y: 1.3333

x2+y24 区域内,求在 MATLAB® 中包含的 函数的最小值。为此,我们需要创建优化变量 和 。

x=optimvar('x');
y=optimvar('y');

以 作为目标函数,创建一个优化问题。

prob=optimproblem("Objective",peaks(x,y));

将约束作为不等式包含在优化变量中。

prob.Constraints=x^2 + y^2 <=4;

将 的初始点设置为 1,将 设置为 –1,并求解问题。

x0.x=1;
x0.y=-1;
sol=solve(prob,x0)
Solving problem using fmincon.

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
sol=struct with fields:
    x: 0.2283
    y: -1.6255

不受支持的函数要求

如果目标函数或非线性约束函数不完全由初等函数组成,则必须使用 将这些函数转换为优化表达式。请参阅将非线性函数转换为优化表达式Supported Operations for Optimization Variables and Expressions

按如下所示转换当前示例:

convpeaks=fcn2optimexpr(@peaks,x,y);
prob.Objective=convpeaks;
sol2=solve(prob,x0)
Solving problem using fmincon.

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
sol2=struct with fields:
    x: 0.2283
    y: -1.6255

Copyright 2018–2020 The MathWorks, Inc.

比较在具有和没有初始可行点的情况下求解整数规划问题的步数。该问题有八个整数变量和四个线性等式约束,所有变量都限制为正值。

prob=optimproblem;
x=optimvar('x',8,1,'LowerBound',0,'Type','integer');

创建四个线性等式约束,并将它们加入问题中。

Aeq=[22    13    26    33    21     3    14    26
    39    16    22    28    26    30    23    24
    18    14    29    27    30    38    26    26
    41    26    28    36    18    38    16    26];
beq=[ 7872
       10466
       11322
       12058];
cons=Aeq*x==beq;
prob.Constraints.cons=cons;

创建目标函数,并将其加入问题中。

f=[2    10    13    17     7     5     7     3];
prob.Objective=f*x;

在不使用初始点的情况下求解问题,并检查显示以查看分支定界节点的数量。

[x1,fval1,exitflag1,output1]=solve(prob);
Solving problem using intlinprog.
LP:                Optimal objective value is 1554.047531.                                          

Cut Generation:    Applied 8 strong CG cuts.                                                        
                   Lower bound is 1591.000000.                                                      

Branch and Bound:

   nodes     total   num int        integer       relative                                          
explored  time (s)  solution           fval        gap (%)                                         
   10000      0.62         0              -              -                                          
   14739      0.84         1   2.154000e+03   2.593968e+01                                          
   18258      1.05         2   1.854000e+03   1.180593e+01                                          
   18673      1.08         2   1.854000e+03   1.563342e+00                                          
   18829      1.09         2   1.854000e+03   0.000000e+00                                          

Optimal solution found.

Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance=0. The intcon variables are integer within tolerance, options.IntegerTolerance=1e-05.

为了进行比较,使用初始可行点求得解。

x0.x=[8 62 23 103 53 84 46 34]';
[x2,fval2,exitflag2,output2]=solve(prob,x0);
Solving problem using intlinprog.
LP:                Optimal objective value is 1554.047531.                                          

Cut Generation:    Applied 8 strong CG cuts.                                                        
                   Lower bound is 1591.000000.                                                      
                   Relative gap is 59.20%.                                                         

Branch and Bound:

   nodes     total   num int        integer       relative                                          
explored  time (s)  solution           fval        gap (%)                                         
    3627      0.32         2   2.154000e+03   2.593968e+01                                          
    5844      0.45         3   1.854000e+03   1.180593e+01                                          
    6204      0.47         3   1.854000e+03   1.455526e+00                                          
    6400      0.49         3   1.854000e+03   0.000000e+00                                          

Optimal solution found.

Intlinprog stopped because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance=0. The intcon variables are integer within tolerance, options.IntegerTolerance=1e-05.
fprintf('Without an initial point, solve took %d steps.
With an initial point, solve took %d steps.',output1.numnodes,output2.numnodes)
Without an initial point, solve took 18829 steps.
With an initial point, solve took 6400 steps.

给出初始点并不能始终改进问题。对于此问题,使用初始点可以节省时间和计算步数。但是,对于某些问题,初始点可能会导致 采用更多步数。

对于某些求解器,您可以将目标函数和约束函数值(如果有)传递给 中的 参数。这可以节省求解器的时间。传递 对象的向量。使用 函数创建此向量。

可以使用目标函数值的求解器有:

可以使用非线性约束函数值的求解器有:

例如,使用 从初始点网格中的值开始,最小化 函数。使用 变量创建一个从 -10 到 10 的网格,使用 变量创建一个从 到 的网格,间距为 1/2。计算在初始点的目标函数值。

x=optimvar("x",LowerBound=-10,UpperBound=10);
y=optimvar("y",LowerBound=-5/2,UpperBound=5/2);
prob=optimproblem("Objective",peaks(x,y));
xval=-10:10;
yval=(-5:5)/2;
[x0x,x0y]=meshgrid(xval,yval);
peaksvals=peaks(x0x,x0y);

使用 传递 参数中的值。这可以为 节省时间,因为 不需要计算这些值。将这些值作为行向量传递。

x0=optimvalues(prob,'x',x0x(:)','y',x0y(:)',...
    "Objective",peaksvals(:)');

使用 和初始值求解问题。

[sol,fval,eflag,output]=solve(prob,x0,Solver="surrogateopt")
Solving problem using surrogateopt.

Figure Optimization Plot Function contains an axes object. The axes object with title Best Function Value: -6.55113, xlabel Iteration, ylabel Function value contains an object of type scatter. This object represents Best function value.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
sol=struct with fields:
    x: 0.2283
    y: -1.6256

fval=-6.5511
eflag=SolverLimitExceeded

output=struct with fields:
        elapsedtime: 53.7052
          funccount: 200
    constrviolation: 0
               ineq: [1x1 struct]
           rngstate: [1x1 struct]
            message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ...'
             solver: 'surrogateopt'

从点 开始,在范围 -5x,y5 上求 函数的局部最小值。

x=optimvar("x",LowerBound=-5,UpperBound=5);
y=optimvar("y",LowerBound=-5,UpperBound=5);
x0.x=-1;
x0.y=2;
prob=optimproblem(Objective=peaks(x,y));
opts=optimoptions("fmincon",Display="none");
[sol,fval]=solve(prob,x0,Options=opts)
sol=struct with fields:
    x: -3.3867
    y: 3.6341

fval=1.1224e-07

尝试使用 求解器求得更好的解。此求解器多次运行 ,可能会产生更好的解。

ms=GlobalSearch;
[sol2,fval2]=solve(prob,x0,ms)
Solving problem using GlobalSearch.

GlobalSearch stopped because it analyzed all the trial points.

All 15 local solver runs converged with a positive local solver exit flag.
sol2=struct with fields:
    x: 0.2283
    y: -1.6255

fval2=-6.5511

找到一个目标函数值更好(更低)的解。退出消息显示局部求解器 运行了 15 次。返回的解的目标函数值约为 –6.5511,低于第一个解的值 1.1224e–07。

求解问题

minx(-3x1-2x2-x3)subjectto{x3binaryx1,x20x1+x2+x374x1+2x2+x3=12

而不显示迭代输出。

x=optimvar('x',2,1,'LowerBound',0);
x3=optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1);
prob=optimproblem;
prob.Objective=-3*x(1) - 2*x(2) - x3;
prob.Constraints.cons1=x(1) + x(2) + x3 <=7;
prob.Constraints.cons2=4*x(1) + 2*x(2) + x3==12;

options=optimoptions('intlinprog','Display','off');

sol=solve(prob,'Options',options)
sol=struct with fields:
     x: [2x1 double]
    x3: 1

检查解。

sol.x
ans=2×1

         0
    5.5000

sol.x3
ans=1

强制 使用 作为线性规划问题的求解器。

x=optimvar('x');
y=optimvar('y');
prob=optimproblem;
prob.Objective=-x - y/3;
prob.Constraints.cons1=x + y <=2;
prob.Constraints.cons2=x + y/4 <=1;
prob.Constraints.cons3=x - y <=2;
prob.Constraints.cons4=x/4 + y >=-1;
prob.Constraints.cons5=x + y >=1;
prob.Constraints.cons6=-x + y <=2;

sol=solve(prob,'Solver', 'intlinprog')
Solving problem using intlinprog.
LP:                Optimal objective value is -1.111111.                                            


Optimal solution found.

No integer variables specified. Intlinprog solved the linear problem.
sol=struct with fields:
    x: 0.6667
    y: 1.3333

求解使用非默认选项求解整数规划问题中所述的混合整数线性规划问题,并检查所有输出数据。

x=optimvar('x',2,1,'LowerBound',0);
x3=optimvar('x3','Type','integer','LowerBound',0,'UpperBound',1);
prob=optimproblem;
prob.Objective=-3*x(1) - 2*x(2) - x3;
prob.Constraints.cons1=x(1) + x(2) + x3 <=7;
prob.Constraints.cons2=4*x(1) + 2*x(2) + x3==12;

[sol,fval,exitflag,output]=solve(prob)
Solving problem using intlinprog.
LP:                Optimal objective value is -12.000000.                                           


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance=0. The intcon variables are integer within tolerance, options.IntegerTolerance=1e-05.
sol=struct with fields:
     x: [2x1 double]
    x3: 1

fval=-12
exitflag=OptimalSolution

output=struct with fields:
        relativegap: 0
        absolutegap: 0
      numfeaspoints: 1
           numnodes: 0
    constrviolation: 0
            message: 'Optimal solution found....'
             solver: 'intlinprog'

对于没有任何整数约束的问题,您也可以使用第五个输出返回非空拉格朗日乘数结构体。

使用指定的索引变量创建和求解优化问题。问题描述:将水果运往多个机场,使利润加权运量最大化,同时确保加权运量满足约束。

rng(0) % For reproducibility
p=optimproblem('ObjectiveSense', 'maximize');
flow=optimvar('flow', ...
    {'apples', 'oranges', 'bananas', 'berries'}, {'NYC', 'BOS', 'LAX'}, ...
    'LowerBound',0,'Type','integer');
p.Objective=sum(sum(rand(4,3).*flow));
p.Constraints.NYC=rand(1,4)*flow(:,'NYC') <=10;
p.Constraints.BOS=rand(1,4)*flow(:,'BOS') <=12;
p.Constraints.LAX=rand(1,4)*flow(:,'LAX') <=35;
sol=solve(p);
Solving problem using intlinprog.
LP:                Optimal objective value is 1027.472366.                                          

Heuristics:        Found 1 solution using ZI round.                                                 
                   Lower bound is 1027.233133.                                                      
                   Relative gap is 0.00%.                                                          


Optimal solution found.

Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance=0. The intcon variables are integer within tolerance, options.IntegerTolerance=1e-05.

找出运送至纽约和洛杉矶的橙子和浆果的最佳运量。

[idxFruit,idxAirports]=findindex(flow, {'oranges','berries'}, {'NYC', 'LAX'})
idxFruit=1×2

     2     4

idxAirports=1×2

     1     3

orangeBerries=sol.flow(idxFruit, idxAirports)
orangeBerries=2×2

     0   980
    70     0

此结果表示不向 运送橙子,只将 70 份浆果运至 ,同时将 980 份橙子运至 ,而不向 运送浆果。

列出以下最佳运量:

idx=findindex(flow, {'berries', 'apples', 'oranges'}, {'NYC', 'BOS', 'LAX'})
idx=1×3

     4     5    10

optimalFlow=sol.flow(idx)
optimalFlow=1×3

    70    28   980

此结果表示将 70 份浆果运送至 ,将 28 份苹果运送至 ,将 980 份橙子运送至 。

要使用基于问题的方法求解非线性方程组

exp(-exp(-(x1+x2)))=x2(1+x12)x1cos(x2)+x2sin(x1)=12

请首先将 定义为一个二元素优化变量。

x=optimvar('x',2);

创建第一个方程作为优化等式表达式。

eq1=exp(-exp(-(x(1) + x(2))))==x(2)*(1 + x(1)^2);

同样,创建第二个方程作为优化等式表达式。

eq2=x(1)*cos(x(2)) + x(2)*sin(x(1))==1/2;

创建一个方程问题,并将这些方程放入该问题中。

prob=eqnproblem;
prob.Equations.eq1=eq1;
prob.Equations.eq2=eq2;

检查此问题。

show(prob)
  EquationProblem : 

	Solve for:
       x


 eq1:
       exp((-exp((-(x(1) + x(2))))))==(x(2) .* (1 + x(1).^2))

 eq2:
       ((x(1) .* cos(x(2))) + (x(2) .* sin(x(1))))==0.5

从 点开始求解问题。对于基于问题的方法,将初始点指定为结构体,并将变量名称作为结构体的字段。对于此问题,只有一个变量,即 。

x0.x=[0 0];
[sol,fval,exitflag]=solve(prob,x0)
Solving problem using fsolve.

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
sol=struct with fields:
    x: [2x1 double]

fval=struct with fields:
    eq1: -2.4070e-07
    eq2: -3.8255e-08

exitflag=EquationSolved

查看解点。

disp(sol.x)
    0.3532
    0.6061

不受支持的函数要求

如果方程函数不是由初等函数组成的,您必须使用 将函数转换为优化表达式。对于本示例:

ls1=fcn2optimexpr(@(x)exp(-exp(-(x(1)+x(2)))),x);
eq1=ls1==x(2)*(1 + x(1)^2);
ls2=fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x);
eq2=ls2==1/2;

请参阅Supported Operations for Optimization Variables and Expressions将非线性函数转换为优化表达式

产品名称二十二
产品名称十九
产品名称十五

在线留言

平台注册入口