//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–44– [44–53/10]
21.11.2003 3:04PM
Chapter 5
ActiveX components and numerical optimization
In this section we provide two illustrative examples of using numerical optimization components on a Web page. The ‘Ray tracing example’performs a complicated numerical optimization involving the minimization of an integral, while the ‘Port- folio allocation example’solves the classic Markowitz portfolio selection problem.
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–45– [44–53/10]
21.11.2003 3:04PM
control, INTG, is nested within the objective function ofthe optimization control, OPTIM. Two user-defined functions are therefore required: a user-defined objective function and a user-defined integrand.
Excerpts from the VBScript used for this demonstration are now given, the complete source code is supplied on the CD ROM which accompanies the book.
The properties and methods of each control will not be discussed in detail, since these can be worked out from the context in which they occur in the VBScript code.
The VBScript controlling the selection of the light colour is as follows:
Sub CommandButtonBlue_Click() red_color¼0
green_color¼0 blue_color¼255 frequency¼7.0 End Sub
Here the frequency and RGB plot colour is set to that corresponding to blue light when the command button labelled ‘Blue’is clicked. Computations are performed when the ‘Calculate’button is clicked and the subroutine CommandButton1_
Click()is run. VBScript excerpts from CommandButton1_Click() are given in Code excerpt 5.1 below.
Sub CommandButton1_Click()
Dim bl(100) ’ holds the upper constraints Dim bu(100) ’ holds the lower constraints Dim loc_x(100)
Dim g(100) Dim a(100)
Dim n, nclin, ncnlin, tda, num_vars Dim y_old, y_new, x_old, x_new
Dim i, j, k, dx, xtemp, canvas_height, canvas_width, x_start, y_start tda¼3
nclin¼1
n¼3 ’ number of variables ncnlin¼0
num_vars¼n
num_pts¼51 ’ number of data points to plot
’ on the first call initialise the plotting area if (first_call¼1) then
GRAPH.BrushColor 230,240,255 GRAPH.PenColor 0,0,0 GRAPH.PenWidth¼3
GRAPH.text ‘‘Rays that minimise the optical path integral’’,x_start,y_startþ20 GRAPH.Rectangle x_start,20,x_startþ337,y_start
GRAPH.circle 468,y_start,10 first_call¼0
end if i¼0
For i¼0 To 3 ’ loop over the vertical start position of the ray y_shift¼i ’ set the vertical position
’ set the initial estimates of the coefficients of the cubic loc_x(0)¼0.000001
loc_x(1)¼0.000001 loc_x(2)¼0.000001
’ set the bounds and constraints For k¼0 To num_vars1
bl(k)¼ 10.0 bu(k)¼10.0 Next
bl(num_vars)¼ y_shift bu(num_vars)¼ y_shift tda¼3
ActiveX components and numerical optimization 45
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–46– [44–53/10]
21.11.2003 3:04PM
a(0)¼2.5 * 2.5 * 2.5 a(1)¼2.5 * 2.5 a(2)¼2.5
atmospheric_factor¼TextBox1.Value ’ set the atmospheric decay factor if (atmospheric_factor <> ‘‘’’) then ’ check that the decay factor has been set
’ perform the numerical optimization
OPTIM.optimize n, nclin, ncnlin, a(0), tda, g(0), loc_x(0), bl(0), bu(0) OPTIM.getvars loc_x(0), n ’ load the optimal cubic coefficients into loc_x
’ work out the optimal path of the ray
do_plot() ’ now plot the optimal path end if
Next End Sub
Code excerpt 5.1 The VBScript code for the subroutineCommandButton1_Click
It can be seen that numerical optimization is performed by the optimize method of the ActiveX componentOPTIM. The user-defined objective function to be minimized is contained in the routineOPTIM_Objfunction(). The complete source code for this routine is given below
Sub OPTIM_Objfunction()
’ The function to optimize.
’ Note : The numerical quadrature ActiveX component INTG is called to evaluate the path integral Dim x(100)
Dim obj_val, num_vars, a, b Dim result, numintervals, val, i
num_vars¼3
obj_val¼OPTIM.Objval OPTIM.getvars x(0), num_vars numintervals¼num_pts a¼0.0
b¼2.5
For i¼0 To num_vars1 params(i)¼x(i) Next
INTG.integrate a, b, numintervals ’ evaluate the path integral val¼INTG.answer ’ assign the path integral to val obj_val¼val
OPTIM.Objval¼obj_val ’ make OPTIM_Objfunction return the value of the path integral OPTIM.setvars x(0), num_vars
End Sub
Code excerpt 5.2 Illustrating the use of VBScript to define the objective function that the ActiveX numerical optimization component,OPTIM, will minimize
The optical path length (to be minimized) is calculated by using the integrate method of the numerical quadrature component INTG. The path integral between a¼0 and b¼2:5 is calculated using 51 intervals. The user-defined integrand is specified in the following routine:
Sub INTG_Integrand()
’ This routine is used by INTG to evaluate the path integral
’ It specifies the spatial and frequency dependence of the refractive index Dim y, x, grad
Dim rindex, opt_path, temp, factor, n_o
x¼INTG.getx ’set the frequency dependency of the refractive index n_o¼10.0 * (1.00.12 * frequency)
y¼params(0) * x * x * xþparams(1) * x * xþparams(2) * xþy_shift grad¼params(0) * x * x * 3.0þparams(1) * x * 2.0þparams(2) temp¼y * yþ(1.25x) * (1.25x)
factor¼Sqr(temp)
’ the spatial dependence of the refractive index
46 Using Numerical Software Components within Microsoft Windows
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–47– [44–53/10]
21.11.2003 3:04PM
rindex¼n_o * Exp(-factor * atmospheric_factor) rindex¼rindexþ1.0
temp¼1.0þgrad * grad opt_path¼rindex * Sqr(temp) value¼opt_path
INTG.getfunval¼value End Sub
Code excerpt 5.3 Illustrating the use of VBScript to define the integrand that the ActiveX numerical quadrature component,INTGwill integrate
The optimal rays are plotted using the subroutinedo_plot().
Sub do_plot()
’ use the ActiveX control GRAPH to plot the optimal ray GRAPH.PenColor red_color,green_color,blue_color GRAPH.PenWidth¼2
GRAPH.Getdata num_pts, x_pos(0), y_pos(0) End Sub
Code excerpt 5.4 The VBScript that plots the results by calling the ActiveX componentGRAPH
Figure 5.1 shows the Web page before any computations are performed. Illus- trative results of the ray tracing are presented in Figures 5.2 and 5.3, and indicates that the model behaves as expected. The results of the ray tracing (not presented here in full) show that the model behaves as expected. For a given decay factor, red light is deviated more than blue. Also at sufficiently high decay factors and
Figure 5.1 The Web page before any calculations have been performed
ActiveX components and numerical optimization 47
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–48– [44–53/10]
21.11.2003 3:04PM
Figure 5.2 Plot of optimal red rays, the decay factor is 1
Figure 5.3 Plot of optimal red rays, the decay factor is 15
48 Using Numerical Software Components within Microsoft Windows
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH005.3D–49– [44–53/10]
21.11.2003 3:04PM
radial distances the refractive index is effectively unity. This leads to more uniformity in the medium and less curvature in the light ray. It should be mentioned that it would be easy, within a Web browser, to edit the user-defined integrand (within the source of the Web page) so as to model different spatial distributions of the refractive index, or even change the optimization and plot settings to model completely new problems.