-
Notifications
You must be signed in to change notification settings - Fork 3
/
tutorial_optimization.tex
173 lines (121 loc) · 7.79 KB
/
tutorial_optimization.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
\documentclass[a4paper,pdftex]{article}
\usepackage{array}
\usepackage{placeins}
\usepackage{hopsantut}
\usepackage{csquotes}
\hypersetup{pdfauthor={Robert Braun}, pdftitle={Hopsan Tutorial - Running an Optimization}, pdfsubject={Hopsan Tutorial}}
\begin{document}
\maketitle{Running an Optimization}
\section*{Using the optimization dialog}
Numerical optimization is an important tool for automatically finding a desired parameter set for a model. Hopsan contains an optimization dialog from where optimizations can be controlled. This guide shows how to optimize control parameters for a simple hydraulic position servo using the Complex-RF algorithm. Optimization in Hopsan can either be performed from a dialog or by loading previously saved (or manually created) script files. This tutorial demonstrates both methods.
\begin{tutenumerate}
\tutitem{Open the model}
The \textit{Welcome} tab has a box with example models. Open the \textit{Position Servo} example model.
\begin{centering}
\includegraphics[width=0.5\textwidth]{gfx/optimization/openmodel.png}
\end{centering}
This model describes of a hydraulic position servo, where a PI-controller is used to control the position. This consists of a proportional gain ($K_{p}$) and an integrator gain ($K_{i}$). The values of these gains will decide the speed and stability of the servo. It is thus desirable to find as good controller parameters as possible.
\tutitem{Simulate}
Simulate the model by clicking the simulate icon.
\icon{0}{gfx/Hopsan-Simulate.png}{Simulate current project (Ctrl-Shift-S)}
Now double click on the \enquote{Scope} component on top of the model. Two curves will be plotted, the reference signal and the actual position. The reference signal is a step, and the position shows the corresponding step response. As you can see, it is quite oscillative. We want to adjust the control parameters to reduce its rise time, overshoot and settling time by using the \textit{optimization} feature.
\tutitem{Open the dialog}
Click the optimization icon to open the dialog.
\icon{0}{gfx/Hopsan-Optimize.png}{Open optimization dialog (Ctrl-Shift-Z)}
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt0.png}
\end{center}
The first page should look like above. You can either write an optimization script by hand, load a script file or start the script wizard.
Let us begin with the latter.
Click on the wizard icon.
\icon{0}{gfx/Hopsan-Wizard.png}{Script Wizard}
\tutitem{Optimization settings}
%
The first wizard page will open. From here you can choose the general optimization settings.
%
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt1.png}
\end{center}
%
\begin{itemize}
\item Set \textit{Algorithm} to \textit{Complex-RF}
\item Set \textit{Number of iterations} to 1000
\item Set \textit{Number of points} to 4
\end{itemize}
Leave all other settings unchanged for now.
\pagebreak
\tutitem{Choose optimization parameters}
Next page controls the parameters in the model that shall be optimized.
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt2.png}
\end{center}
Check the following parameters in the list:
\begin{itemize}
\item GainI $\rightarrow$ k\#Value
\item GainP $\rightarrow$ k\#Value
\end{itemize}
Then give them the following min and max values:
{\renewcommand{\arraystretch}{1.2}
\begin{tabularx}{\linewidth}{X X X}
\textbf{Parameter} & \textbf{Min} & \textbf{Max} \\
\specialrule{1.3pt}{0pt}{0pt}
GainI, k\#Value & 0 & 0.001 \\
GainP, k\#Value & 0 & 0.04 \\
\end{tabularx}
}
\tutitem{Choose objective functions}
The optimization algorithms use objective functions to calculate how good a certain point in the parameter space is. From the third page this can be chosen. In this model we want the actual position to be as close as possible to the reference signal. To do this, check the following variables in the list:
\begin{itemize}
\item{Position\_Sensor} $\rightarrow$ \textbf{out} $\rightarrow$ Value
\item{Step} $\rightarrow$ \textbf{out} $\rightarrow$ Value
\end{itemize}
Then choose \enquote{Minimize} and \enquote{Absolute Average Difference} in the boxes. Click \enquote{Add Function}.
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt3a.png}
\end{center}
Apart from minimizing the average difference, we also want to minimize the overshoot of the step response.
\begin{itemize}
\item Uncheck \textbf{Step} $\rightarrow$ \textbf{out} $\rightarrow$ Value
\end{itemize}
Now choose \enquote{Minimize} and \enquote{Overshoot}, and then click \enquote{Add Function}. In the box called \enquote{arg1}, change the value to 0.7.
Now we have added a second objective function. The total objective function will the sum of these. As they may not be equally important or equally scaled, it is possible to weight the individual functions. Change \enquote{Weight} for the first function to 10. Now the page should look like this:
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt3b.png}
\end{center}
\pagebreak
\tutitem{Start the optimization}
Next page shows the generated script code. Modifications can be made if necessary, but leave it as it is for now.
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt4.png}
\end{center}
Now change to the "run" tab. From here the execution of the optimization can be monitored:
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt5a.png}
\end{center}
Click on \enquote{Start Optimization}.
\icon{0}{gfx/Hopsan-Optimize.png}{Start Optimization}
During the optimization the objective function values and the values of the optimization parameters are shown for the different points. The rows are sorted from best to worst.
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt5b.png}
\end{center}
\tutitem{Evaluate the results}
Once the optimization has finished, we can evaluate the results. Parameters from a point can be written back to the original model by using the \enquote{Apply} buttons.
\begin{center}
\includegraphics[width=0.7\textwidth]{gfx/optimization/screenshot-opt5c.png}
\end{center}
Click the \enquote{Apply} button for the point with green text (the best point). Then go back to the original model and click the Simulate button again.
\icon{0}{gfx/Hopsan-Simulate.png}{Simulate current project (Ctrl-Shift-S)}
Now plot the step response by double clicking the scope component on top of the model. If a desirable result has not been found, it can be necessary to change objective functions or parameters and re-run the optimization.
\end{tutenumerate}
\section*{Optimizing from script files}
It is often necessary to re-use the settings from the optimization more than once. This can be done by saving the optimization scripts to a file, and loading them again later.
\begin{tutenumerate}
\tutitem{Follow steps 1-7 above}\vspace{-\baselineskip}
\tutitem{Saving a script file}
Click on Save script file in the dialog.
\tutitem{Loading a script file}
To load a previously saved script file, click on Load script file in the dialog. You will be taken directly to the code tab. Go to the "run" tab and start the optimization as before. Note that the settings on the first three pages are not updated by the script. Running the wizard again will re-generate the script and overwrite the loaded code.
\tutitem{Loading a script file from command terminal}
It is also possible to load a script file from the command terminal. Just write \enquote{exec} followed by the path to the file. Hopsan will automatically understand that this is an optimization script and start the dialog. You can use \enquote{pwd} to show current directory and \enquote{cd} to change directory if necessary. Example: \texttt{>{}> exec ..\textbackslash folder\textbackslash myscript.hcom}
\end{tutenumerate}
\end{document}