⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 walkthrough.html

📁 This demo shows how to use MATLAB, Optimization Toolbox, and Genetic Algorithm and Direct Search Too
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<span class="comment">% Modify options setting</span><span class="comment">% Note: You can use the defaults as well, this will speed up the solution</span>options = optimset(options,<span class="string">'Display'</span> ,<span class="string">'iter'</span>);options = optimset(options,<span class="string">'TolFun'</span> ,0.1);options = optimset(options,<span class="string">'LargeScale'</span> ,<span class="string">'off'</span>);<span class="comment">% Request plots, add my custom plot to the mix</span>options = optimset(options,<span class="string">'OutputFcn'</span> ,{ @updatePlot });options = optimset(options,<span class="string">'PlotFcns'</span> ,{  @optimplotx @optimplotfval });tic[x_x0] = fmincon(@(x) wbObjFun(x,time,idealProfile),x0,Aineq,bineq,<span class="keyword">...</span>        [],[],lb,ub,[],options);tocx_x0</pre><pre class="codeoutput">                                max     Line search  Directional  First-order  Iter F-count        f(x)   constraint   steplength   derivative   optimality Procedure     0     16      8.37961     -0.01086                                             1     39      7.07475     -0.01458      0.00781          221          101       2     60      6.31931     -0.08378       0.0313         13.2         9.91       3     78      4.63623      -0.1544         0.25        -2.12         5.63       4     94      2.82522    -0.006032            1         1.71         6.79       5    110       1.7788    2.22e-016            1       -0.313         13.4       6    126      48.3345      -0.5437            1         99.1           69       7    142      27.5839      -0.4806            1         20.4         14.6       8    158      9.69029  -4.441e-016            1         29.3           15       9    175      9.19751      -0.1622          0.5         24.6         36.5      10    192      7.11708      -0.1193          0.5         11.1         33.3      11    208      2.28753            0            1         8.86           24      12    225     0.326541            0          0.5         1.36         23.9      13    246     0.273352  -4.441e-016       0.0313        0.166         19.6   Optimization terminated: magnitude of directional derivative in search direction less than 2*options.TolFun and maximum constraint violation  is less than options.TolCon.Active inequalities (to within options.TolCon = 1e-006):  lower      upper     ineqlin   ineqnonlin                          8           Elapsed time is 292.878329 seconds.x_x0 =   14.8410   12.5973   18.3707    5.6539   12.1541   18.3603   -2.5993   15.0912   11.2407    6.4329   -1.8655   15.5438    7.6907   12.5108   12.5000</pre><img vspace="5" hspace="5" src="walkthrough_05.png"> <img vspace="5" hspace="5" src="walkthrough_06.png"> <h2>Solve the Problem Using a Different Start Point<a name="8"></a></h2>         <p>At this point, I&#8217;d like take a look at our problem from a different angle.  As you may know, gradient based solvers, such            as the one we used here, tend to fail if the objective function does not have smooth derivatives or the problem is ill-defined.             Let's change the start point of this problem, and see if <tt>fmincon</tt> can find a solution.         </p><pre class="codeinput">load <span class="string">wbPSOpt</span>tic<span class="keyword">try</span>    [x_x0c] = fmincon(@(x) wbObjFun(x,time,idealProfile),x0c,Aineq,bineq,<span class="keyword">...</span>       [],[],lb,ub,[],options);<span class="keyword">catch</span>    disp(<span class="string">'We caught the following error'</span>)    nowork = lasterror;    nowork.message<span class="keyword">end</span>toc</pre><pre class="codeoutput">                                max     Line search  Directional  First-order  Iter F-count        f(x)   constraint   steplength   derivative   optimality Procedure     0     16      15.4198   1.554e-015                                             1     33      393.237      -0.3751          0.5          940          109       2     50      275.985      -0.1875          0.5         -234         55.5       3     66      163.538   1.776e-015            1         -632         47.1   We caught the following errorans =Error using ==&gt; simError originates in Mechanical block DoubleWishbone/Double Wishbone/Double Wish/shock abs/Spherical1. A constraint has been violated. Check constraint solver type and tolerances in the Machine Environment block and Simulink solver and tolerances in Configuration Parameters. Check for kinematic singularities.Elapsed time is 78.577761 seconds.</pre><img vspace="5" hspace="5" src="walkthrough_07.png"> <img vspace="5" hspace="5" src="walkthrough_08.png"> <p>The error message is from our Simulink model and basically tells us we put in a bad definition for geometry that our model            could not handle.         </p>         <p>Just to be clear, the problem is not with the <tt>fmincon</tt> solver, but with my knowledge of the design space in which we are looking to explore. <tt>fmincon</tt> did what I asked it to do, it just found a point that my model could not handle.  To use <tt>fmincon</tt> we need to add constraints to our problem definition or redefine our objective function to prevent this situation from happening.         </p>         <p>I&#8217;m pointing this out because in practice you may often encounter problems where you just don&#8217;t have enough information to            completely define your problem or you may have an objective function that does not have well defined gradients, or even holes            in the design space. This often renders traditional gradient based solvers, such as the one used here, ineffective. This is            where algorithms in Genetic Algorithm and Direct Search Toolbox are useful.         </p>         <h2>Solve the Problem Using Genetic Algorithm and Direct Search Toolbox<a name="10"></a></h2>         <p>We can use pattern search to solve this problem without having to redefine our objective function, constraints, or the Simulink            model.  Let's define our problem as before and solve.  We loaded in the problem formulation in the structure <tt>wbPSOpt</tt> in the previous step.  Let's see if pattern search can find a solution.         </p>         <p><i>Note: You can set up your problem in the <tt>psearchtool</tt> GUI analogously to the <tt>optimtool</tt> GUI.  File --&gt; Import Options --&gt; wbPSOpt, set up the problem definition, and then click on <b>Start</b>.</i></p><pre class="codeinput"><span class="comment">% psearchtool % uncomment this line to run interactively</span><span class="comment">%</span><span class="comment">% Command line equivalent (comment out this section if using psearchtool)</span><span class="comment">% -----------------------</span><span class="comment">% Start with default options</span>options = psoptimset;<span class="comment">% Modify some parameters</span><span class="comment">% Note: You can use the defaults as well, this will speed up the solution</span>options = psoptimset(options,<span class="string">'TolFun'</span> ,0.06);options = psoptimset(options,<span class="string">'MeshContraction'</span> ,0.25);options = psoptimset(options,<span class="string">'MaxMeshSize'</span> ,1 );options = psoptimset(options,<span class="string">'PollingOrder'</span> ,<span class="string">'success'</span>);options = psoptimset(options,<span class="string">'SearchMethod'</span> ,@GPSPositiveBasisNp1);options = psoptimset(options,<span class="string">'Display'</span> ,<span class="string">'iter'</span>);options = psoptimset(options,<span class="string">'Cache'</span> ,<span class="string">'on'</span>);options = psoptimset(options,<span class="string">'CacheTol'</span> ,2.2204e-010);<span class="comment">% Request plots, add my custom plot to the mix</span>options = psoptimset(options,<span class="string">'OutputFcns'</span> ,{ { @updatePlot } });options = psoptimset(options,<span class="string">'PlotFcns'</span> ,{  @psplotbestf @psplotbestx });<span class="comment">%%Run PATTERNSEARCH</span>tic[x_x0c_ps] = patternsearch(@(x) wbObjFun(x,time,idealProfile),x0c,<span class="keyword">...</span>    Aineq,bineq,[],[],lb,ub,[],options);tocx_x0c_ps</pre><pre class="codeoutput">Iter     f-count          f(x)      MeshSize     Method    0        1        15.4198             1          1        4        4.73299             1     Successful Search    2        9         4.5874             1     Successful Search    3       10        4.38583             1     Successful Search    4       11        4.06205             1     Successful Search    5       12        3.66635             1     Successful Search    6       18        3.29301             1     Successful Search    7       49        3.29301          0.25     Refine Mesh    8       50        3.12307          0.25     Successful Search    9       61        2.94817          0.25     Successful Search   10       63        2.58323          0.25     Successful Search   11       64        2.38127          0.25     Successful Search   12       69        2.31824          0.25     Successful Search   13       70        2.29935          0.25     Successful Search   14       84        2.20802          0.25     Successful Search   15       86        1.93259          0.25     Successful Search   16       92        1.73249          0.25     Successful Search   17       93        1.56504          0.25     Successful Search   18       94        1.45678          0.25     Successful Search   19       95        1.43817          0.25     Successful Search   20      126        1.43817        0.0625     Refine Mesh   21      137        1.29085        0.0625     Successful Search   22      143        1.26478        0.0625     Successful Search   23      144        1.24756        0.0625     Successful Search   24      145        1.24026        0.0625     Successful Search   25      156        1.19536        0.0625     Successful Search   26      162        1.14688        0.0625     Successful Search   27      163        1.10743        0.0625     Successful Search   28      164        1.07901        0.0625     Successful Search   29      165         1.0635        0.0625     Successful Search   30      166        1.06245        0.0625     Successful SearchIter     f-count        f(x)       MeshSize      Method   31      177        1.02141        0.0625     Successful Search   32      183       0.966209        0.0625     Successful Search   33      184       0.924368        0.0625     Successful Search   34      185       0.899389        0.0625     Successful Search   35      186       0.894236        0.0625     Successful Search   36      203       0.893817        0.0625     Successful Search   37      210       0.880355        0.0625     Successful Search   38      237       0.880355       0.01563     Refine MeshOptimization terminated: change in the function value less than options.TolFun.Elapsed time is 255.561565 seconds.x_x0c_ps =   12.1238   12.5998   16.7597    9.1324   12.0842   17.0282   -1.6750   14.4139    9.0207    6.0950   -1.6124   14.8704    7.1561   11.2301   11.5000</pre><img vspace="5" hspace="5" src="walkthrough_09.png"> <img vspace="5" hspace="5" src="walkthrough_10.png"> <p>Pattern search found a solution.  We set the function tolerance for both <tt>fmincon</tt> and <tt>patternsearch</tt> to limit the total number of iterations required to satisfy our convergence criteria.  Decreasing this value would allow            the solution to converge more tightly on the 'Ideal' profile, but would require longer solution times.         </p>         <h2>Summary<a name="12"></a></h2>         <p>We used two approaches to optimize the design of a double wishbone suspension system.  The first approach illustrated traditional            gradient base techniques that work well for well-defined problems.  We then found a condition where the gradient based method            did not find a solution, since we do not have a completely defined problem and were unlucky in the choice of start point.             The second approach, pattern search, was used to illustrate non-gradient based methods that work well for problems that are            ill-defined or do not have well define gradients or objective functions.         </p>         <p class="footer"><br>            Published with MATLAB&reg; 7.3<br></p>      </div>      <!--##### SOURCE BEGIN #####%% Optimization of a Double Wishbone Suspension System
% This demo shows how to use MATLAB, Optimization Toolbox, and Genetic
% Algorithm and Direct Search Toolbox to optimize the design of a double
% wishbone suspension system.
%
% _Note: You will need to have the following products installed in order to
% run this demo: MATLAB, Simulink, Optimization Toolbox, Genetic Algorithm
% and Direct Search Toolbox, and SimMechanics. Optional: Virtual Reality
% Toolbox._
%
%% Introduction to the Problem
% We wish to optimize the response of a double wishbone suspension system.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -