« Previous « Start » Next »
D Editing Init Files directly
TOMLAB is based on the principle of creating a problem structure
that defines the problem and includes all relevant information
needed for the solution of the user problem. Two formats are
defined, the TOMLAB format (TQ format) and the Init File format
(IF format). The TQ format gives the user a fast way to setup a
problem structure and solve the problem from the Matlab command
line using any suitable TOMLAB solver.
The definition of an advanced general graphical user interface (GUI) and
a similar menu system demanded a more complicated format.
The solution is the IF format, where groups of problems are collected into
sets, each set having an initialization file.
Besides defining the problem, a list of all problems in the set is also
generated by the initialization file.
In the following sections detailed descriptions are given on how
to edit the Init Files for different types of problems, linear
programming, quadratic programming, unconstrained optimization,
box-bounded global optimization, global mixed-integer nonlinear
programming and constrained optimization.
D.1 Editing New Problems in
Linear Programming Init Files
The step wise description below shows how to edit new problems
into an existing Init File for LP problems.
The example shows how to add one
new problem in
lp_prob.m , the default file for LP problems
in TOMLAB .
As test example choose the same test example as in
Section
5.1,
(
13), here called
lptest1 .
-
Copy
File: tomlab/testprob/lp_prob
to e.g. lpnew_prob.m
in a working directory.
- Edit lpnew_prob.m .
Add the problem name, lptest1 , to the menu choice:
...
,'Winston Ex. 4.12 B4. Max || ||. Rewritten'...
,'lptest1'...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
- There are twelve problems defined in
lp_prob.m , making
thirteen
the new problem number.
Define the constraint matrix A, the upper bounds
for the constraints b_U, the cost vector c as below.
If the constraints
would be of equality type then define the lower bounds for the
constraints b_L equal to b_U.
...
elseif P == 13
Name = 'lptest1';
c = [-7 -5]';
A = [ 1 2
4 1 ];
b_U = [ 6 12 ]';
x_L = [ 0 0 ]';
x_min = [ 0 0 ]';
x_max = [10 10 ]';
else
disp('lp_prob: Illegal problem number')
pause
Name=[];
end
...
- Note that because the file name is changed, the new name must be
substituted. There are three places where the name should be changed,
the function definition in the beginning of the file,
the disp statement above and the place shown in the following text
...
if ask==-1 & ~isempty(Prob)
if isstruct(Prob)
if ~isempty(Prob.P)
if P==Prob.P & strcmp(Prob.probFile,'lpnew_prob'), return; end
end
end
end
...
- Save the file properly.
- Run
AddProblemFile('lpnew_prob','lp_prob with extra user problems','lp');
- To solve this problem the following statements may be used.
Prob = probInit('lpnew_prob', 13); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('lpSimplex', Prob); % Call lpSimplex using driver routine
It is also possible to
define the optional parameters
B,
f_
min and
x_0 as
described in the problem definition description in
lp_prob.m .
If
B
and
x_0
are not given, as in this case, a Phase I linear program is solved to
an initial feasible solution point.
Now
lpnew_prob.m is one of the
TOMLAB Init Files and all problems in
lpnew_prob.m
are accessible from the GUI, the menu systems, and the driver routines.
The edited file is found in
File: tomlab/usersguide/lpnew_prob
.
D.2 Editing New Problems in
Quadratic Programming Init Files
The step wise description below shows how to edit new problems
into an existing QP Init File. The example shows how to add one
new problem in
qp_prob.m , the default file for QP problems
in TOMLAB .
The test example is the same as in Section
5.2,
problem (
15), named
QP EXAMPLE.
-
Copy
File: tomlab/testprob/qp_prob
to e.g. qpnew_prob.m
in a working directory.
- Edit qpnew_prob.m .
Add the problem name, QP Example , to the menu choice:
...
,'Bazaara IQP 9.29b pg 405. F singular'...
,'Bunch and Kaufman Indefinite QP'...
,'QP EXAMPLE'...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
- There are fourteen problems defined in
qp_prob.m , making
fifteen
the new problem number.
Add the following in qpnew_prob.m after the last already existing
problem:
...
elseif P==15
Name='QP EXAMPLE';
F = [ 8 2 % Hessian
2 8 ];
c = [ 3 -4 ]';
A = [ 1 1 % Constraint matrix
1 -1 ];
b_L = [-inf 0 ]'; % Lower bounds on the constraints
b_U = [ 5 0 ]'; % Upper bounds on the constraints
x_L = [ 0 0 ]'; % Lower bounds on the variables
x_U = [ inf inf ]'; % Upper bounds on the variables
x_0 = [ 0 1 ]'; % Starting point
x_min=[-1 -1 ]; % Plot region parameters
x_max=[ 6 6 ]; % Plot region parameters
else
disp('qp_prob: Illegal problem number')
pause
Name=[];
end
...
- Note that because the file name is changed, the new name must be
substituted. There are three places where the name should be changed,
the function definition in the beginning of the file,
the disp statement above and the place shown in the following text
...
if ask==-1 & ~isempty(Prob)
if isstruct(Prob)
if ~isempty(Prob.P)
if P==Prob.P & strcmp(Prob.probFile,'qpnew_prob'), return; end
end
end
end
...
- Save the file properly.
- Run
AddProblemFile('qpnew_prob','qp_prob with extra user problems','qp');
- To solve this problem the following statements may be used.
Prob = probInit('qpnew_prob', 15); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('qpSolve', Prob); % Call qpSolve using driver routine
Now
qpnew_prob.m is one of the
TOMLAB Init Files in the GUI data base and all problems in
qpnew_prob.m
are accessible from the GUI, the menu systems, and the driver routines.
The edited file is found in
File: tomlab/usersguide/qpnew_prob
.
D.3 Editing New Problems in
Unconstrained Optimization Init Files
The step wise description below shows how to edit new problems
into an existing unconstrained optimization (UC) Init File.
The example shows how to add one
new problem in
uc_prob.m , the default file for UC problems
in TOMLAB . As test example the
problem (
17) in Section
6
is used,
The m-file code for the objective function for this
problem is given in
File: tomlab/usersguide/rbb_f.m
.
-
Copy
File: tomlab/testprob/uc_prob
to e.g. ucnew_prob.m
in a working directory.
Also copy
File: tomlab/testprob/uc_f
,
File: tomlab/testprob/uc_g
and
File: tomlab/testprob/uc_H
to the working directory,
and change the names to
ucnew_f.m ,
ucnew_g.m and
ucnew_H.m .
- Add the problem name to the menu choice in ucnew_prob.m :
...
,'Fletcher Q.2.6'...
,'Fletcher Q.3.3'...
,'RB BANANA'...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
- There are seventeen problems defined in
ucnew_prob.m , making
eighteen
the new problem number.
Add the following in ucnew_prob.m after the last already existing
problem (the optional parameters are not necessary to define):
Note that the routine checkuP is used to check if the
user parameter in the
Prob structure sent to this routine is defined for the correct
problem.
The routine askparam then takes care of setting the proper
user parameter dependent on what value the flag
ask is set to.
...
elseif P == 18
Name ='RB BANANA';
x_0 = [-1.2 1]'; % Starting values for the optimization.
x_L = [-10;-10]; % Lower bounds for x.
x_U = [2;2]; % Upper bounds for x.
x_opt = [1 1]; % Known optimal point (optional).
f_opt = 0; % Known optimal function value (optional).
f_min = 0; % Lower bound on function (optional).
x_max = [ 1.3 1.3]; % Plot region parameters.
x_min = [-1.1 -0.2]; % Plot region parameters.
% The following lines show how to use the advanced user parameter
% definition facility in TOMLAB.
uP = checkuP(Name,Prob); % Check if given uP is for this problem
% Ask the following question if flag set to ask questions
uP(1) = askparam(ask,'Give the nonlinear factor in Rosenbrocks banana: ',...
0,[],100,uP);
% CHANGE: elseif P == 18
% CHANGE: Add an elseif entry and the other variable definitions needed
...
- Note that because the file name is changed, the new name must be
substituted. There are three places where the name should be changed,
the function definition in the beginning of the file,
a disp statement and most important the place shown in the following text
...
if ask==-1 & ~isempty(Prob)
if isstruct(Prob)
if ~isempty(Prob.P)
if P==Prob.P & strcmp(Prob.probFile,'ucnew_prob'), return; end
end
end
end
...
- The names of the m-files are also changed and must be changed on two rows
...
% Define the m-files that compute the function value, the gradient vector
% and the Hessian matrix
Prob=tomFiles(Prob,'ucnew_f','ucnew_g','ucnew_H');
...
- The function value, gradient vector and Hessian routine must now be
edited. The function names should also be renamed, but Matlab
does not care what the name is of the routine, only what the file name
is, so it is optional to change the name of each function.
Make the following addition in ucnew_f.m :
...
elseif P == 17 % Fletcher Q.3.3
f = 0.5*(x(1)^2+x(2)^2)*exp(x(1)^2-x(2)^2);
elseif P == 18 % RB BANANA
f = Prob.uP(1)*(x(2)-x(1)^2)^2 + (1-x(1))^2;
end
...
- Make the following addition in ucnew_g.m :
...
elseif P == 17 % Fletcher Q.3.3
%f = 0.5*(x(1)^2+x(2)^2)*exp(x(1)^2-x(2)^2);
e = exp(x(1)^2-x(2)^2);
g = e*[x(1)*(1+x(1)^2+x(2)^2); x(2)*(1-x(1)^2-x(2)^2)];
elseif P == 18 % RB BANANA
g = Prob.uP(1)*[-4*x(1)*(x(2)-x(1)^2)-2*(1-x(1)); 2*(x(2)-x(1)^2) ];
end
...
- Make the following addition in ucnew_H.m :
...
elseif P == 17 % Fletcher Q.3.3
%f = 0.5*(x(1)^2+x(2)^2)*exp(x(1)^2-x(2)^2);
%g = e*[x(1)*(1+x(1)^2+x(2)^2); x(2)*(1-x(1)^2-x(2)^2)];
e = exp(x(1)^2-x(2)^2);
H = [1+5*x(1)^2+2*x(1)^2*x(2)^2+x(2)^2+2*x(1)^4, ...
-2*x(1)*x(2)*(x(1)^2+x(2)^2);
0 , 1-x(1)^2+2*x(1)^2*x(2)^2-5*x(2)^2+2*x(2)^4 ];
H(2,1)=H(1,2);
H = e*H;
elseif P == 18 % RB BANANA
H = Prob.uP(1)*[ 12*x(1)^2-4*x(2)+2 , -4*x(1);
-4*x(1) , 2 ];
end
...
- Save all the files properly.
- Run
AddProblemFile('ucnew_prob','uc_prob with extra user problems','uc');
- To solve this problem the following statements may be used.
Prob = probInit('ucnew_prob', 18); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('ucSolve', Prob); % Call ucSolve using driver routine
Now
ucnew_prob.m is one of the
TOMLAB Init Files in the GUI data base and all problems in
ucnew_prob.m
are accessible from the GUI, the menu system, and the driver routines.
The edited Init File is found in
File: tomlab/usersguide/ucnew_prob
.
In the same directory the function, gradient and Hessian routines are
found.
D.4 Editing New Problems in
Box-bounded Global Optimization Init Files
Box-bounded global optimization problems are defined in the same way as
unconstrained optimization problems.
Since no derivative information is used,
only the Init File definition file and the routine to compute
the objective function value have to be modified.
The step wise description below shows how to edit a new problem
into an existing Init File,
in this case the predefined
glb_prob ,
and the objective function routine
glb_f .
As test example use the
Rosenbrock's banana problem
The standard value is α=100. To define (
23) as
a box-bounded global optimization problem follow the step wise
instructions below (for all instructions it is assumed that the
files are edited in a text editor). Note that in this example the
lower variable bounds are changed to
xL=(−2,−2)
T. The reason
for that is to speed up the global search for the reader who wants
to run this example. It is always important to make the
box-bounded region as small as possible.
- Copy the files glb_prob.m and glb_f.m to a working
directory.
- Add the problem name to the menu choice in glb_prob.m :
...
,'HGO 468:2'...
,'Spiral'...
,'RB BANANA'...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
- Add the following in glb_prob.m after the last already existing
problem (the optional parameters are not necessary to define):
...
elseif P == 33
Name='RB BANANA';
x_L = [-2;-2]; % Lower bounds for x.
x_U = [ 2; 2]; % Upper bounds for x.
x_opt = [1 1]; % Known optimal point (optional).
f_opt = 0; % Known optimal function value (optional).
n_global = 1; % Number of global minima (optional).
n_local = 1; % Number of local minima (optional).
K = []; % Lipschitz constant, not used.
x_max = [ 2 2]; % Plot region parameters.
x_min = [-2 -2]; % Plot region parameters.
...
- Make the following addition in glb_f.m :
...
elseif P == 33 % RB BANANA
f = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
end
...
- Save all the files properly.
- To solve this problem the following statements may be used.
Prob = probInit('glb_prob', 33); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('glbSolve', Prob); % Call glbSolve using driver routine
Note that the working directory in Matlab must be the directory
where the new files have been edited, otherwise the predefined files in
TOMLAB with the same names will be used.
D.5 Editing New Problems in
Global Mixed-Integer Nonlinear Programming Init Files
To illustrate how to define a global mixed-integer nonlinear programming
problem in the Init File format, add constraints
to the test problem
Rosenbrock's banana,
|
|
|
s/t |
−2 |
≤ |
x1 |
≤ |
2, x1 integer |
−2 |
≤ |
x2 |
≤ |
2 |
|
|
x1−x2 |
≤ |
1 |
|
|
−x12−x2 |
≤ |
1 |
|
|
(24) |
The standard value is α=100
Note the additional constraint that
x1 must be integer.
The third constraint is linear
and is therefore defined separately
from the nonlinear fourth constraint.
To define (
24)
as a global mixed-integer nonlinear programming
problem follow the step wise
instructions below
(for all instructions it is assumed that the files are edited
in a text editor).
-
Copy the files glc_prob.m , glc_f.m and glc_c.m
to the working directory.
- Modify the files glc_prob.m and glc_f.m in the same
way as described for the box-bounded case in Section D.4.
- Extend the problem definition in glc_prob.m with the constraint
parameters:
...
elseif P == 25
Name='RB BANANA';
x_L = [-2;-2]; % Lower bounds for x.
x_U = [ 2; 2]; % Upper bounds for x.
x_opt = [1 1]; % Known optimal point (optional).
f_opt = 0; % Known optimal function value (optional).
A = [1 -1]; % Linear constraints matrix.
b_L = -inf; % Lower bounds on linear constraints.
b_U = 1; % Upper bounds on linear constraints.
c_L = -inf; % Lower bounds on nonlinear constraints.
c_U = 1; % Upper bounds on nonlinear constraints.
IntVars = 1; % Indices for integer constrained variables.
n_global = 1; % Number of global minima (optional).
n_local = 1; % Number of local minima (optional).
K = []; % Lipschitz constant, not used.
x_max = [ 2 2]; % Plot region parameters.
x_min = [-2 -2]; % Plot region parameters.
end
...
- Make the following addition in glc_c.m :
...
elseif P == 25 % RB BANANA
cx = -x(1)^2 - x(2);
end
...
- Save all the files properly.
- To solve this problem the following statements may be used.
Prob = probInit('glc_prob', 25); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('glcSolve', Prob); % Call glcSolve using driver routine
Note that the working directory in Matlab must be the directory
where the new files have been edited, otherwise the predefined files in
TOMLAB with the same names will be used.
D.6 Editing New Problems in
Constrained Optimization Init Files
To illustrate how to define a constrained problem in the
Init File format, add two constraints
to the
Rosenbrock's banana problem,
|
|
|
s/t |
−10 |
≤ |
x1 |
≤ |
2 |
−10 |
≤ |
x2 |
≤ |
2 |
|
|
x1−x2 |
≤ |
1 |
|
|
−x12−x2 |
≤ |
1 |
|
|
.
(25) |
The standard value is α=100.
The first two constraints are simple bounds
on the variables.
The third constraint is linear
and treated separately from the fourth nonlinear inequality constraint.
The problem will be defined by following the step wise instructions below
(for all instructions it is assumed that the files are edited
in a text editor):
-
Copy the files con_prob.m , con_f.m , con_g.m ,
con_H.m , con_c.m , con_dc.m and con_d2c.m
to the working directory.
- Modify the files con_prob.m , con_f.m , con_g.m
and con_H.m in the same way as described for the
unconstrained case in Section D.3.
- Extend the problem definition in con_prob.m with the constraint
parameters:
...
elseif P == 15
Name='RB BANANA';
x_0 = [-1.2 1]'; % Starting values for the optimization.
x_L = [-10;-10]; % Lower bounds for x.
x_U = [2;2]; % Upper bounds for x.
x_opt = [1 1]; % Known optimal point (optional).
f_opt = 0; % Known optimal function value (optional).
f_min = 0; % Lower bound on function (optional).
x_max = [ 1.3 1.3]; % Plot region parameters.
x_min = [-1.1 -0.2]; % Plot region parameters.
A = [1 -1]; % Linear constraints matrix.
b_L = -inf; % Lower bounds on linear constraints.
b_U = 1; % Upper bounds on linear constraints.
c_L = -inf; % Lower bounds on nonlinear constraints.
c_U = 1; % Upper bounds on nonlinear constraints.
end
...
- Make the following addition in con_c.m :
...
elseif P == 15 % RB BANANA
cx = -x(1)^2 - x(2);
end
...
- Make the following addition in con_dc.m :
...
elseif P == 15 % RB BANANA
if init==0
dc = [-2*x(1) -1];
else
dc = ones(1,2);
end
end
...
- Make the following addition in con_d2c.m :
...
elseif P == 15 % RB BANANA
if init==0
d2c = [-2 0;0 0]*lam;
else
d2c = [1 0; 0 0];
end
end
...
- Save all the files properly.
- To solve this problem the following statements may be used.
Prob = probInit('con_prob', 15); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('conSolve', Prob); % Call conSolve using driver routine
D.7 Creating a New
Constrained Optimization Init File
Assume a collection of e.g. constrained
problems should be defined in new problem definition files. Also
assume the problems have been defined in
con_prob ,
con_f ,
con_g ,
con_H ,
con_c and
con_dc
as described in Section
D.6.
Of course it is possible to
remove the already existing problems and define the first new problem as
number one. The extra modifications needed are:
-
Rename the files to for example connew_prob , connew_f ,
connew_g , connew_H ,
connew_c and
connew_dc .
- Make the following modification in the beginning of connew_prob :
...
if ask==-1 & ~isempty(Prob)
if isstruct(Prob)
if ~isempty(Prob.P)
if P==Prob.P & strcmp(Prob.probFile,'connew_prob'), return; end
end
end
end
...
- Make the following modifications at the end of connew_prob :
...
Prob=tomFiles(Prob,'connew_f','connew_g','connew_H','connew_c','connew_dc');
...
- Save all the files properly.
- Run
AddProblemFile('connew_prob','New Constrained Test Problems','con');
The text New Constrained ... is of course possible to change
as the user likes.
Now connew_prob.m is one of the
TOMLAB Init Files in the GUI data base and all problems in
connew_prob.m
are accessible from the GUI, the menu system, and the driver routines.
This file is also the default file for constrained problems.
D.8 Editing New Problems in
Nonlinear Least Squares Init Files
To define (
17) as a nonlinear least squares problem follow the
step wise instructions below
(for all instructions it is assumed that the files
are edited in a text editor).
- Copy the files ls_prob.m , ls_r.m and ls_J.m
to the working directory.
- Add the problem name to the menu choice in ls_prob.m :
...
...
,'Plasmid Stability n=3 (subst.)'...
,'Plasmid Stability n=3 (probability)'...
,'RB BANANA'...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
...
- Add the following in ls_prob.m after the last already existing
problem (the optional parameters are not necessary to define):
...
...
elseif P==10
Name='RB BANANA';
y=[0;0]; % r(x) = residual = model psi(t,x) - data y(t)
x_0=[-1.2 1]'; % Starting values for the optimization.
x_L=[-10;-10]; % Lower bounds for x.
x_U=[2;2]; % Upper bounds for x.
x_opt=[1 1]; % Known optimal point (optional).
f_opt=0; % Known optimal function value (optional).
f_min=0; % Lower bound on function (optional).
x_max=[ 1.3 1.3]; % Plot region parameters.
x_min=[-1.1 -0.2]; % Plot region parameters.
else
disp('ls_prob: Illegal problem number')
pause
Name=[];
end
...
...
- Make the following addition in ls_r.m :
...
...
yMod=r;
elseif P==10
% RB BANANA
r = [10*(x(2)-x(1)^2);1-x(1)];
end
if Prob.LS.yUse & m==length(r), r=r-y; end
...
...
- Make the following addition in ls_J.m :
...
...
elseif P==10
% RB BANANA
J = [-20*x(1) 10
-1 0 ];
end
...
...
- Save all the files properly.
- To solve this problem the following statements may be used.
Prob = probInit('ls_prob', 10); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('clsSolve', Prob); % Call clsSolve using driver routine
Note that the working directory in Matlab must be the directory
where the new files have been edited, otherwise the predefined files in
TOMLAB with the same names will be used.
D.9 Editing New Problems in
Exponential Sum Fitting Init Files
The pre-defined exponential sum fitting problems are defined in
one problem definition
file,
exp_prob.m .
Assume a fit of a sum of exponential terms
should be made
to the data series
t = 10−3 |
|
30 |
50 |
70 |
90 |
110 |
130 |
150 |
170 |
190 |
210 |
230 |
250 |
270 |
290 |
310 |
330 |
350 |
370 |
|
|
|
,
Y(t) = 10−4 |
|
18299 |
15428 |
13347 |
11466 |
10077 |
8729 |
7382 |
6708 |
5932 |
5352 |
4734 |
4271 |
3744 |
3485 |
3111 |
2950 |
2686 |
2476 |
|
|
|
,
(26) |
here named
SW.
To define (
26) as a exponential sum fitting problem follow the
step wise
instructions below
(for all instructions it is assumed that the files
are edited in a text editor).
-
Copy the file exp_prob.m to the working directory.
- Add the problem name to the menu choice in exp_prob.m :
...
,'CDFsim5'...
,'CDFdata5\~ '...
,'SW '...
); % MAKE COPIES OF THE PREVIOUS ROW AND CHANGE TO NEW NAMES
if isempty(P)
return;
end
...
- Add the following in exp_prob.m after the last already existing
problem:
...
elseif P==52
Name='SW';
t=[30:20:370]'; % Time in ms
y=[18299 15428 13347 11466 10077 8729 7382 6708 5932 5352 4734 4271 ...
3744 3485 3111 2950 2686 2476]';
t=t/1000; % Scale to seconds. Gives lambda*1000, of order 1
y=y/10000; % Scale function values. Avoid large alpha
else
disp('exp_prob: Illegal problem number')
...
- Save the file properly.
- To solve this problem the following statements may be used.
Prob = probInit('exp_prob', 52); % Get Prob structure.
... % Define changes in Prob structure
Result = tomRun('clsSolve', Prob); % Call clsSolve using driver routine
Note that the working directory in Matlab must be the directory
where the new files have been edited, otherwise the predefined files in
TOMLAB with the same names will be used.
There are five different types of exponential models available in TOMLAB .
The type of exponential model is determined by the parameter
Prob.ExpFit.eType , which is set by defining the parameter
eType
in the problem definition file:
...
elseif P==52
Name='SW';
t=[30:20:370]'; % Time in ms
y=[18299 15428 13347 11466 10077 8729 7382 6708 5932 5352 4734 4271 ...
3744 3485 3111 2950 2686 2476]';
t=t/1000; % Scale to seconds. Gives lambda*1000, of order 1
y=y/10000; % Scale function values. Avoid large alpha
eType = 1;
else
disp('exp_prob: Illegal problem number')
...
The above definition of
eType is not necessary and was made just
in illustrative purpose since 1 is the default value of
eType .
See Section
8.4, for a description of the exponential models available.
D.10 Creating a New Nonlinear Least Squares
Init File
Assume a collection of e.g. nonlinear least squares
problems should be defined in new Init Files. Also
assume that the problems are defined
in
ls_prob ,
ls_r
and
ls_J as described in Section
D.8
It is of course possible to
remove the already existing problems and define the first new problem as
number one. The extra modifications needed are:
-
Rename the files to for example lsnew_prob , lsnew_r and
lsnew_J .
- Make the following modification in the beginning of lsnew_prob :
...
...
if ask==-1 & ~isempty(Prob)
if isstruct(Prob)
if ~isempty(Prob.P)
if P==Prob.P & strcmp(Prob.probFile,'lsnew_prob'), return; end
end
end
end
...
...
- Make the following modifications at the end of lsnew_prob :
...
...
Prob=tomFiles(Prob,'ls_f','ls_g','ls_H',[],[],[],'lsnew_r','lsnew_J');
...
...
- Save all the files properly.
- Run
AddProblemFile('lsnew_prob','New Nonlinear Least Squares Test Problems','con');
The text New Nonlinear ... is of course possible to change
as the user likes.
Now lsnew_prob.m is one of the
TOMLAB Init Files in the GUI data base and all problems in
lsnew_prob.m
are accessible from the GUI, the menu systems, and the driver routines.
This file is also the default file for nonlinear least squares problems.
The following
example
illustrate how the
tomRun driver routine could be used in an efficient way.
A simple
for loop solves all the least squares problems
defined in the files
lsnew_prob ,
lsnew_r and
lsnew_J ,
see Section
D.10.
Several parameters are
explicitly set for illustrative purpose,
one could otherwise rely on the default values.
The function
drv_test below runs
tomRun for all problems defined in
lsnew_prob , and then displays the number of iterations performed.
Instead of just printing the number of iterations, the
results could be saved for later use in e.g. statistical analysis.
function drv_test();
probFile = 'lsnew_prob'; % Solve problems defined in lsnew_prob.m
probNames = feval(probFile); % Get a list of all available problems.
ask = 0; % Do not ask questions in problem definition.
PriLev = 0; % No printing output.
for P = 1:size(probNames,1)
probNumber = P;
Prob = probInit(probFile, P, ask, []);
Prob.optParam.eps_x = 1E-7; % Termination tolerance for X
Prob.optParam.eps_f = 1E-9; % Termination tolerance on F
Prob.optParam.cTol = 1E-5; % Constraint violation
Prob.optParam.MaxIter = 200; % Maximum number of iterations
Prob.optParam.eps_g = 1E-5; % Termination tolerance on gradient
Prob.optParam.eps_absf = 1E-35; % Absolute convergence tol. in function f.
Prob.optParam.LineSearch.sigma = 0.5; % Line search accuracy sigma
fprintf('\n Problem number %d:',P);
fprintf(' %s',Prob.Name);
Result = tomRun('clsSolve', Prob, PriLev, ask);
fprintf('\n Number of iterations: %d',Result.Iter);
end
Another example is on how to solve
the exponential sum fitting problem (
26)
probFile = 'exp_prob'; % Problem definition file.
P = 44; % Problem number.
Prob = probInit(probFile, P); % Setup Prob structure.
Result = tomRun([], Prob, 2); % Default solver is clsSolve
Also see the Section
6.3 on how to make a direct call to an optimization routine.
D.11 Using the Driver Routines
Solve the problem
RB BANANA (
17)
defined as an unconstrained problem. Default values will
be used for all parameters not explicitly changed. The following calls
solve the problem:
probFile = 'uc_prob'; % Problem definition file.
P = 18; % Problem number, after editing the new problem
Result = tomRun('ucSolve', probFile, P);
To display the result of the run call the print routine
PrintResult with the
Result structure,
PrintResult(Result);
which gives the following printing output:
=== * * * ================================================== * * *
Problem 18: RB BANANA f_k 0.000000000000000001
User given f(x_*) 0.000000000000000000
f(x_0) 24.199999999999996000
Solver: ucSolve. EXIT=0. INFORM=2.
Safeguarded BFGS
FuncEv 48 GradEv 40
TOMLAB Global Variable Counters give:
FuncEv 48 GradEv 41 Iter 36
Starting vector x:
x_0: -1.200000 1.000000
Optimal vector x:
x_k: 1.000000 1.000000
Diff x-x0:
2.200000e+000 -2.312176e-009
Gradient g_k:
g_k: -4.162202e-009 9.227064e-010
TOMLAB found no active constraints.
=== * * * ================================================== * * *
To solve the problem using the routine
fminu
call the driver routine
tomRun
with the solver name as string argument:
probFile = 'uc_prob'; % Problem definition file.
P = 18; % Problem number.
Prob = probInit(probFile, P); % Setup Prob structure.
Result = tomRun('fminu', Prob); % Call fminu using driver routine
The second example has a more "testing and developing" characteristic.
The purpose is
to illustrate how the driver routines could be used in an efficient way.
By use of a simple
for loop solve all the constrained
problems
defined in the files
connew_prob ,
connew_f ,
connew_g ,
connew_H ,
connew_c and
connew_dc , see
Section
D.10.
Several parameter values are set
explicitly for
illustrative purposes.
The function
drv_test below runs
tomRun for all problems defined in
connew_prob , and then displays the number of iterations performed.
Instead of just printing the number of iterations,
the results could be stored in a structure array
for later use in e.g. statistical analysis.
function drv_test();
probFile = 'connew_prob'; % Solve problems defined in connew_prob.m
probNames = feval(probFile); % Get a list of all available problems.
ask = 0; % Do not ask questions in problem definition.
PriLev = 0; % No printing output.
for P = 1:size(probNames,1)
probNumber = P;
Prob = probInit(probFile, P, ask, []);
Prob.optParam.eps_x = 1E-7; % Termination tolerance for X
Prob.optParam.eps_f = 1E-9; % Termination tolerance on F
Prob.optParam.cTol = 1E-5; % Constraint violation
Prob.optParam.MaxIter = 200; % Maximum number of iterations
Prob.optParam.eps_g = 1E-5; % Termination tolerance on gradient
Prob.optParam.LineSearch.sigma = 0.5; % Line search accuracy sigma
fprintf('\n Problem number %d:',P);
fprintf(' %s',Prob.Name);
Result = tomRun(Solver, Prob, PriLev, ask);
fprintf('\n Number of iterations: %d',Result.Iter);
end
« Previous « Start » Next »