Main Content

# Surrogate Optimization in Simulink Design Optimization

This example shows how to use surrogate optimization in Simulink® Design Optimization™ to optimize the design of a hydraulic cylinder.

This example requires Parallel Computing Toolbox™ software.

### Surrogate Optimization

Solving optimization problems involves using different values of the design variables and evaluating the objective function multiple times, especially if the objective function is sufficiently nonsmooth such that a derivative-based solver is not suitable. In such cases, you might need to use a derivative-free solver such as `patternsearch`, but these solvers tend to require running the objective function many more times. Using such a solver can be time consuming if the objective function is computationally expensive to evaluate. One way to overcome this problem is surrogate optimization. This approach creates a surrogate of the expensive objective function. The surrogate can be evaluated quickly and gives results very similar to the original objective function. Surrogate optimization also tries many starting points, which helps find a global optimum, rather than converging on a solution that, while locally optimal, might not be the global optimum.

### Hydraulic Cylinder Model

This example shows how to use surrogate optimization to optimize the response of a hydraulic cylinder. Open the model.

`open_system('sdoHydraulicCylinder')`

The hydraulic cylinder model is based on the Simulink model `sldemo_hydcyl`. The model includes:

• `Pump` and `Cylinder Assembly` subsystems. For more information on the subsystems, see Single Hydraulic Cylinder Simulation.

• A step change applied to the cylinder control valve orifice area that causes the cylinder piston position to change.

### Specify Design Variables

Now, specify the design variables to tune: the cylinder cross-sectional area `Ac` and the piston spring constant `K`.

```DesignVars = sdo.getParameterFromModel('sdoHydraulicCylinder',{'Ac','K'}); DesignVars(1).Value = 1e-3; DesignVars(1).Minimum = 0.0003; DesignVars(1).Maximum = 0.0013; DesignVars(1).Scale = 0.001; DesignVars(2).Value = 50000; DesignVars(2).Minimum = 10000; DesignVars(2).Maximum = 100000; DesignVars(2).Scale = 1;```

### Specify Design Requirements

Next, specify design requirements to satisfy the following conditions during optimization:

• The pressure stays under 1,750,000 N/m.

• The piston position step response satisfies a rise time of 0.04 seconds and a settling time of 0.05 seconds.

```Requirements = struct; Requirements.MaxPressure = sdo.requirements.SignalBound(... 'BoundMagnitudes', [1750000 1750000], ... 'BoundTimes', [0 0.1]); Requirements.PistonResponse = sdo.requirements.StepResponseEnvelope(... 'FinalValue', 0.04, ... 'PercentSettling', 1, ... 'RiseTime', 0.04, ... 'SettlingTime', 0.05);```

### Signal Logging

Next, specify model signals to log during model simulation. These signals are needed to evaluate the design requirements, that is, to determine whether they are satisfied.

```Simulator = sdo.SimulationTest('sdoHydraulicCylinder'); PistonPosition_Info = Simulink.SimulationData.SignalLoggingInfo; PistonPosition_Info.BlockPath = 'sdoHydraulicCylinder/Cylinder Assembly'; PistonPosition_Info.OutputPortIndex = 2; PistonPosition_Info.LoggingInfo.LoggingName = 'PistonPosition'; PistonPosition_Info.LoggingInfo.NameMode = 1; Pressures_Info = Simulink.SimulationData.SignalLoggingInfo; Pressures_Info.BlockPath = 'sdoHydraulicCylinder/Cylinder Assembly'; Pressures_Info.LoggingInfo.LoggingName = 'Pressures'; Pressures_Info.LoggingInfo.NameMode = 1; Simulator.LoggingInfo.Signals = [... PistonPosition_Info; ... Pressures_Info];```

### Create Optimization Objective Function

Create a function that is called at each optimization iteration to evaluate the design requirements. Use an anonymous function with one argument that calls `sdoHydraulicCylinder_optFcn`.

`optimfcn = @(P) sdoHydraulicCylinder_optFcn(P,Simulator,Requirements);`

The `sdoHydraulicCylinder_optFcn` function uses the simulator and requirements objects to evaluate the design. Type `edit sdoHydraulicCylinder_optFcn` to examine the function in more detail.

### Optimization Using Derivative-Based Solver

Now, try solving this optimization problem using a derivative-based solver. Specify optimization options.

```Options = sdo.OptimizeOptions; Options.Method = 'fmincon'; Options.UseParallel = true; Options.OptimizedModel = Simulator;```

Run the optimization by calling `sdo.optimize` with the objective function handle, parameters to optimize, and options.

`[Optimized_DesignVars,Info] = sdo.optimize(optimfcn,DesignVars,Options);`
```Configuring parallel workers for optimization... Analyzing and transferring files to the workers ...done. Analyzing and transferring files to the workers ...done. Parallel workers configured for optimization. Optimization started 2023-May-04, 13:37:43 max First-order Iter F-count f(x) constraint Step-size optimality 0 5 0.001 0.3033 1 10 0.00123343 0.3726 0.233 100 2 20 0.00117522 0.3545 0.0582 100 3 28 0.00121802 0.3678 0.0428 100 4 39 0.00117879 0.3556 0.0392 100 5 48 0.00120789 0.3646 0.0291 100 6 60 0.00119077 0.3593 0.0171 100 7 72 0.00119977 0.3621 0.00901 100 8 80 0.00119977 0.3621 0.00089 100 Converged to an infeasible point. fmincon stopped because the size of the current step is less than the value of the step size tolerance but constraints are not satisfied to within the value of the constraint tolerance. Removing data from parallel workers... Data removed from parallel workers. ```

At the end of optimization iterations, the "`max constraint`" column is still positive, indicating that the derivative-based solver does not satisfy all of the requirements.

### Optimization Options Using Surrogate Solver

Since the derivative-based solver does not satisfy all the requirements, try `surrogateopt` as a derivative-free solver. Specify optimization options.

```Options = sdo.OptimizeOptions; Options.Method = 'surrogateopt'; Options.MethodOptions.MaxFunctionEvaluations = 100; Options.UseParallel = true; Options.OptimizedModel = Simulator;```

Run the optimization by calling `sdo.optimize` with the objective function handle, parameters to optimize, and options.

`[Optimized_DesignVars,Info] = sdo.optimize(optimfcn,DesignVars,Options);`
```Configuring parallel workers for optimization... Analyzing and transferring files to the workers ...done. Analyzing and transferring files to the workers ...done. Parallel workers configured for optimization. Optimization started 2023-May-04, 13:38:35 Current Current F-count f(x) max constraint f(x) max constraint Trial type 1 0.001 0.303264 0.001 0.303264 initial 2 0.001 0.303264 0.0003 0.411103 random 3 0.001 0.303264 0.0008 1.02568 random 4 0.001 0.303264 0.00055 0.578226 random 5 0.001 0.303264 0.00105 1.25679 random 6 0.001 0.303264 NaN Inf random 7 0.001 0.303264 0.001175 2.14476 random 8 0.001 0.303264 NaN Inf random 9 0.000925 0.252177 0.000925 0.252177 random 10 0.000925 0.252177 NaN Inf random 11 0.000925 0.252177 0.0011125 0.333109 random 12 0.000925 0.252177 NaN Inf random 13 0.000925 0.252177 0.0008625 0.724191 random 14 0.000925 0.252177 NaN Inf random 15 0.000925 0.252177 0.0009875 1.65452 random 16 0.000925 0.252177 0.0007375 4.26818 random 17 0.000925 0.252177 0.0012375 0.399354 random 18 0.000925 0.252177 0.00076875 0.985955 random 19 0.000925 0.252177 0.00126875 1.39122 random 20 0.000925 0.252177 0.00051875 0.487797 random 21 0.000925 0.252177 0.00101875 1.23392 random 22 0.000925 0.252177 NaN Inf random 23 0.00089375 0.226608 0.00089375 0.226608 random 24 0.00089375 0.226608 NaN Inf random 25 0.00089375 0.226608 0.00114375 3.56038 random 26 0.00089375 0.226608 NaN Inf random 27 0.00089375 0.226608 0.00095625 8.44124 random 28 0.00089375 0.226608 NaN Inf random 29 0.00089375 0.226608 0.00120625 0.325598 random 30 0.00058125 0.0293624 0.00058125 0.0293624 random 31 0.00058125 0.0293624 NaN Inf adaptive 32 0.00058125 0.0293624 NaN Inf adaptive 33 0.00058125 0.0293624 NaN Inf adaptive 34 0.00058125 0.0293624 NaN Inf adaptive 35 0.00058125 0.0293624 NaN Inf adaptive 36 0.00058125 0.0293624 NaN Inf adaptive 37 0.00058125 0.0293624 NaN Inf adaptive 38 0.00058125 0.0293624 0.000382646 0.259073 adaptive 39 0.00058125 0.0293624 0.000872844 0.18585 adaptive 40 0.00058125 0.0293624 NaN Inf adaptive 41 0.00058125 0.0293624 NaN Inf adaptive 42 0.00058125 0.0293624 NaN Inf adaptive 43 0.00058125 0.0293624 0.000346006 0.100726 adaptive 44 0.00058125 0.0293624 0.00043983 0.372926 adaptive 45 0.00058125 0.0293624 0.000510274 0.598923 adaptive 46 0.00058125 0.0293624 0.000510805 0.276542 adaptive 47 0.00058125 0.0293624 0.000616738 0.353985 adaptive 48 0.00058125 0.0293624 0.000651695 0.138744 adaptive 49 0.00058125 0.0293624 0.000652226 1.69733 adaptive 50 0.000582031 0.0286493 0.000582031 0.0286493 adaptive Current Current F-count f(x) max constraint f(x) max constraint Trial type 51 0.000582031 0.0286493 0.000581641 0.0290052 adaptive 52 0.000582031 0.0286493 0.000581445 0.0291836 adaptive 53 0.000582031 0.0286493 0.00058125 0.0292252 adaptive 54 0.000582031 0.0286493 0.000581348 0.029273 adaptive 55 0.000582031 0.0286493 0.00108125 0.821321 random 56 0.000582031 0.0286493 NaN Inf random 57 0.000582031 0.0286493 0.00083125 2.64838 random 58 0.000582031 0.0286493 NaN Inf random 59 0.000582031 0.0286493 0.00106562 0.768714 random 60 0.000582031 0.0286493 0.000315625 5.20466 random 61 0.000582031 0.0286493 0.000815625 0.725618 random 62 0.000582031 0.0286493 0.000440625 0.067243 random 63 0.000582031 0.0286493 0.000940625 0.901158 random 64 0.000582031 0.0286493 0.000690625 0.475563 random 65 0.000582031 0.0286493 0.00119062 1.14814 random 66 0.000582031 0.0286493 NaN Inf random 67 0.000582031 0.0286493 0.000878125 1.89266 random 68 0.000582031 0.0286493 0.000628125 7.48485 random 69 0.000582031 0.0286493 0.00112812 0.359996 random 70 0.000582031 0.0286493 NaN Inf random 71 0.000582031 0.0286493 0.00125312 0.367383 random 72 0.000582031 0.0286493 NaN Inf random 73 0.000582031 0.0286493 0.00100312 2.73758 random 74 0.000582031 0.0286493 NaN Inf random 75 0.000582031 0.0286493 0.00103437 0.258263 random 76 0.000582031 0.0286493 0.000784375 1.32496 random 77 0.000582031 0.0286493 0.00128437 3.05351 random 78 0.000582031 0.0286493 0.000659375 1.20157 random 79 0.000582031 0.0286493 0.00115937 1.52728 random 80 0.000582031 0.0286493 NaN Inf random 81 0.000582031 0.0286493 0.000909375 1.28986 random 82 0.000582031 0.0286493 0.000721875 0.11391 random 83 0.000582031 0.0286493 0.00122187 0.780649 random 84 0.000582031 0.0286493 NaN Inf random 85 0.000582031 0.0286493 0.0003 0.0907138 adaptive 86 0.000582031 0.0286493 NaN Inf adaptive 87 0.000582031 0.0286493 NaN Inf adaptive 88 0.000582031 0.0286493 NaN Inf adaptive 89 0.000357693 -0.000458298 0.000357693 -0.000458298 adaptive 90 0.000357693 -0.000458298 0.000445736 0.0595388 adaptive 91 0.000357693 -0.000458298 0.000472898 0.0296971 adaptive 92 0.000357693 -0.000458298 NaN Inf adaptive 93 0.000357693 -0.000458298 0.000467197 0.0100422 adaptive 94 0.000357693 -0.000458298 0.000349748 0.0373376 adaptive 95 0.000357693 -0.000458298 0.000355658 0.0328266 adaptive 96 0.000357693 -0.000458298 0.000332693 0.0625643 adaptive 97 0.000357693 -0.000458298 0.000357693 -0.000458298 best value The current solution is feasible. surrogateopt stopped because it exceeded the function evaluation limit set by the 'MethodOptions.MaxFunctionEvaluations' property in the sdo.OptimizeOptions object. If the solution needs to be improved, you could try increasing the function evaluation limit. Removing data from parallel workers... Data removed from parallel workers. ```

Using surrogateopt, all the design requirements are satisfied, as indicated by a negative value in the "`max constraint`" column.

Update the model with the optimized parameter values.

`sdo.setValueInModel('sdoHydraulicCylinder',Optimized_DesignVars);`

In this example, a solver using surrogates successful on an optimization problem where a derivative-based solver is unsuccessful. The `surrogateopt` solver is a global solver that tries many starting points. By using a surrogate of the model, `surrogateopt` needs to run the model only a moderate number of times.