Difference between revisions of "Using a model"

From SUMOwiki
Jump to navigationJump to search
 
(17 intermediate revisions by 2 users not shown)
Line 12: Line 12:
 
** >> <code>model = modelFile.model;</code>
 
** >> <code>model = modelFile.model;</code>
  
Now the model is available as the variable 'model' in the Matlab workspace.  See the [[Using a model]] page.
+
Now the model is available as the variable 'model' in the Matlab workspace.
  
 
== Model portability ==
 
== Model portability ==
Line 31: Line 31:
  
 
In this case you can use the ''getExpression'' and ''exportToMFile'' (available from v6.0) methods. See below.
 
In this case you can use the ''getExpression'' and ''exportToMFile'' (available from v6.0) methods. See below.
 +
 +
== Model space vs Simulator space ==
 +
 +
It is important to note the difference between '''Model space''' and '''Simulator space'''.  When a data point is in model space, it means its inputs all lie in the range [-1 1].  When the point is in simulator space its inputs lie in the range specified by the [[Simulator configuration]] file.
 +
 +
Internally the toolbox only works in model space. The toolbox will take care of translating points from simulator space into model space and back (this happens in the SampleManager object).  You will note that many methods have a ''XXXinModelSpace'' variant.  This just means that the method does exactly the same, except it expects points to be in model space.  You should normally not have to care about model space unless you are writing your own extensions to the toolbox.  In that case see [[Add Model Type]].
  
 
== Available methods ==
 
== Available methods ==
  
Once the model is loaded you can invoke a number methods on it.  For the most complete, up to date list see the list of functions in the @Model and @XModel subdirectories.
+
Once the model is loaded you can invoke a number methods on it.  We list the main ones below. For a full list of available methods just use the matlab 'methods' command:
 +
 
 +
<source lang="matlab">
 +
>>methods(model)
 +
</source>
 +
 
 +
If you want to understand the structure of the model, i.e., how the model object is built up you can do two things:
 +
 
 +
# open the class file for that model, e.g., for an ANNModel object, open the src/matlab/models/@ANNModel/ANNModel.m file
 +
# use the struct command to convert the model object to a structure.  For example:
 +
 
 +
<source lang="matlab">
 +
>>str = struct(model)
 +
</source>
 +
 
 +
Also, some model types provide methods to access the internal model representation.  For example, if m is an object of type ANNModel, then executing m.getNetwork() will return the nested Matlab neural network object (from the Matlab Neural Network Toolbox).
  
 
=== guiPlotModel ===
 
=== guiPlotModel ===
  
The easiest way to explore a model is to use the graphical model browser (available from v6.0). [[Model Visualization GUI|See here for more information]]
+
The easiest way to explore a model is to use the graphical model browser. [[Model Visualization GUI|See here for more information]]
  
 
=== plotModel ===
 
=== plotModel ===
<code><pre>
+
<source lang="matlab">
  >>[figureHandle options] = plotModel(model,[outputNumber],[options])
+
  >>[figureHandle] = plotModel(model,[outputNumber],[options])
</pre></code>
+
</source>
  
<code>plotModel</code> will generate an indicative plot of the model surface. To do so, it evaluates the model on a reasonably large grid of points.
+
<code>plotModel</code> will generate an indicative plot of the model surface. To do so, it evaluates the model on a reasonably dense grid of points.
  
 
<code>plotModel</code> optional parameters:
 
<code>plotModel</code> optional parameters:
 
* <code>outputNumber</code>: optional parameter, an integer specifying which output to plot
 
* <code>outputNumber</code>: optional parameter, an integer specifying which output to plot
* <code>options</code>: optional parameter, a struct containing a number of options you can set.  To see the list of available options, simply call <code>plotModel</code> with 2 output arguments and only one input argument.
+
* <code>options</code>: optional parameter, a struct containing a number of options you can set.  To get the default options simply call <code>Model.getPlotDefaults()</code>.
  
  
Line 60: Line 81:
 
* '''Higher dimensional problems''': All variables after the fifth are fixed at 0, and plotting proceeds as if the model was five dimensional.
 
* '''Higher dimensional problems''': All variables after the fifth are fixed at 0, and plotting proceeds as if the model was five dimensional.
  
 
+
The toolbox handles complex valued outputs as their modulus (= absolute value = magnitude) for plotting purposes. These plots are just visual aids for monitoring the modeling process. Phase data can be extracted from the model files.
The toolbox handles '''COMPLEX OUTPUTS''' as their modulus (= absolute value = magnitude) for plotting purposes. These plots are just visual aids for monitoring the modeling process. Phase data can be extracted from the model files.
 
 
 
=== plotModelErrors ===
 
 
 
Available since v6.0. Normally you would want to use this function through [[Model Visualization GUI| model browser gui instead]].
 
 
 
Shows the errors of your model on the training data and (optionally) validation data.
 
 
 
<code><pre>
 
>> [fighandle options] = plotModelErrors(model, [output index], [validation data], [options]);
 
</pre></code>
 
 
 
If passed the format of the validation data should match the model signature exactly (one column for every input and output, with 2 columns for complex data).  Options are obtained in the same way as with plotModel.
 
  
 
=== evaluate ===
 
=== evaluate ===
<code><pre>
+
<source lang="matlab">
 
  >> values = evaluate(model, samples);
 
  >> values = evaluate(model, samples);
</pre></code>
+
</source>
 +
 
 +
This evaluates the model on the given samples. The samples should be provided in simulator space. Simulator space is defined by the range in the [[Simulator configuration]]. If no range (minimum and maximum) was specified, the domain is assumed to be [-1,1].
  
This evaluates the model on the given samples. The samples should be provided in simulator space. Simulator space is defined by the range in the [[Data_format|simulator configuration]]. If no range (minimum and maximum) is defined, the domain is assumed to be [-1,1].
+
See also [[Using_a_model#Model_object_interfacing_and_optimization]]
  
=== evaluateInModelspace ===
+
=== evaluateDerivative ===
<code><pre>
+
<source lang="matlab">
  >> values = evaluateInModelspace(model, samples);
+
  >> values = evaluateDerivative(model, samples, [outputIndex]);
</pre></code>
+
</source>
  
This evaluates the model on the given samples. The samples should lie in the [-1,1] range (model space).
+
This approximates the partial derivatives of the model at each given sample. Note that the base class implementation is a very simple approximation.  Models can override this function to provide more accurate derivatives (e.g., Kriging does this already).  However, in its current form it is already useful.
  
 
=== getSamples ===
 
=== getSamples ===
<code><pre>
+
<source lang="matlab">
 
  >> samples = getSamples(model);
 
  >> samples = getSamples(model);
</pre></code>
+
</source>
  
 
Returns the samples that were used to fit the model. The samples are returned in simulator space.
 
Returns the samples that were used to fit the model. The samples are returned in simulator space.
  
 
=== getValues ===
 
=== getValues ===
<code><pre>
+
<source lang="matlab">
 
  >> values = getValues(model);
 
  >> values = getValues(model);
</pre></code>
+
</source>
  
 
Returns the values that correspond to the samples from getSamples().
 
Returns the values that correspond to the samples from getSamples().
  
 
=== getDescription ===
 
=== getDescription ===
<code><pre>
+
<source lang="matlab">
 
  >> desc = getDescription(model);
 
  >> desc = getDescription(model);
</pre></code>
+
</source>
  
 
Returns a string with a user friendly description of the model.
 
Returns a string with a user friendly description of the model.
  
 
=== getExpression ===
 
=== getExpression ===
<code><pre>
+
<source lang="matlab">
 
  >> desc = getExpression(model,[outputNumber]);
 
  >> desc = getExpression(model,[outputNumber]);
</pre></code>
+
</source>
 
 
Returns the symbolic mathematical expression of this model (e.g., 3*x1^2 - 2*x2 +5).  Note that not all model types implement this (yet).
 
  
=== freeParams ===
+
Returns the symbolic mathematical expression of this model (e.g., 3*x1^2 - 2*x2 +5)Note that not all model types implement this.
<code><pre>
 
>> n = freeParams(model);
 
</pre></code>
 
 
 
Returns the number of free parameters in the model.  By default this returns the number of datapoints the model was built with but this is overridden by some model types.  For example, an ANN model returns the number of weights in the network while a rational model returns the number of coefficients.
 
  
=== fields ===
+
=== construct ===
<code><pre>
+
<source lang="matlab">
  >> fields(model);
+
  >> model = construct(model,samples);
</pre></code>
+
</source>
  
Returns all fields the object has.
+
This will build/train/fit.. the model on the given set of data points and return the updated model.
  
 +
=== complexity ===
 +
<source lang="matlab">
 +
>> n = complexity(model);
 +
</source>
  
Other methods differ per model typeInspect the corresponding model directory and constructor to see what methods and options are available.
+
Returns the number of free parameters in the model.  By default this returns the number of datapoints the model was built with but this is overridden by some model types.  For example, an ANN model returns the number of weights in the network while a rational model returns the number of coefficients.
  
== Model Optimization ==
+
== Model object interfacing and optimization ==
  
To optimize the model, Matlab requires a function handle to the objective function (= the model object)You can construct a function handle from the model object as follows (example for the 3D case):
+
You may want to use the model as part of a larger Matlab program, or you may simply want to optimizer the model.  To easily do this you can create a function handle to the model object.  You can do this as follows (example for the 3D case):
  
 
<code><pre>
 
<code><pre>
Line 148: Line 155:
 
   feval( handle, 0, 1, -1 );
 
   feval( handle, 0, 1, -1 );
 
</pre></code>
 
</pre></code>
 +
 +
== Miscellaneous post-processing scripts ==
 +
 +
The SUMO Toolbox also includes some miscellaneous scripts you can use to analyze the results of your run. You can find them in <code>...\SUMO-Toolbox\src\scripts\matlab\</code> folder.

Latest revision as of 17:28, 10 February 2012

This page explains what you can do with a SUMO generated model.

Loading a model from disk

As the SUMO Toolbox builds models, each current best model is stored as a Matlab mat file in the output directory (e.g.: output/Academic_2D_Twice_rep01_run00_2008.05.20_10-27-18/models_out/model_0002.mat).

In order to load this model from disk and actually use it, do the following:

  • Start Matlab, make sure the SUMO Toolbox is in your path and navigate to the directory where the model file is stored
  • Load the model from disk as follows:
    • >> modelFile = load('model_0002.mat');
    • >> model = modelFile.model;

Now the model is available as the variable 'model' in the Matlab workspace.

Model portability

How do you exchange and/or export SUMO models.

The other person has the SUMO Toolbox installed

The model 'mat' files can be shared with other people. In order for somebody else to use your saved model the following conditions need to be satisfied:

  • The person has the SUMO Toolbox in his Matlab path
  • The person should be using a similar Matlab version (including toolboxes) as was used to create the model file (preferably equal)
  • The person should be using a similar SUMO Toolbox version as was used to create the model file (preferably equal)

We do not guarantee portability if the the above versions differ.

The other person does NOT have the SUMO Toolbox installed

In this case you can use the getExpression and exportToMFile (available from v6.0) methods. See below.

Model space vs Simulator space

It is important to note the difference between Model space and Simulator space. When a data point is in model space, it means its inputs all lie in the range [-1 1]. When the point is in simulator space its inputs lie in the range specified by the Simulator configuration file.

Internally the toolbox only works in model space. The toolbox will take care of translating points from simulator space into model space and back (this happens in the SampleManager object). You will note that many methods have a XXXinModelSpace variant. This just means that the method does exactly the same, except it expects points to be in model space. You should normally not have to care about model space unless you are writing your own extensions to the toolbox. In that case see Add Model Type.

Available methods

Once the model is loaded you can invoke a number methods on it. We list the main ones below. For a full list of available methods just use the matlab 'methods' command:

 >>methods(model)

If you want to understand the structure of the model, i.e., how the model object is built up you can do two things:

  1. open the class file for that model, e.g., for an ANNModel object, open the src/matlab/models/@ANNModel/ANNModel.m file
  2. use the struct command to convert the model object to a structure. For example:
 >>str = struct(model)

Also, some model types provide methods to access the internal model representation. For example, if m is an object of type ANNModel, then executing m.getNetwork() will return the nested Matlab neural network object (from the Matlab Neural Network Toolbox).

guiPlotModel

The easiest way to explore a model is to use the graphical model browser. See here for more information

plotModel

 >>[figureHandle] = plotModel(model,[outputNumber],[options])

plotModel will generate an indicative plot of the model surface. To do so, it evaluates the model on a reasonably dense grid of points.

plotModel optional parameters:

  • outputNumber: optional parameter, an integer specifying which output to plot
  • options: optional parameter, a struct containing a number of options you can set. To get the default options simply call Model.getPlotDefaults().


To determine which kind of plot is generated, one makes a distinction based on the dimension of the input space:

  • One dimensional models are always plotted in a simple XY line chart. Samples are shown as dots.
  • Two dimensional models are plotted as a Matlab *mesh* plot, i.e. a colored surface. The colors are just an indication of height and don't have any further meaning. The samples are plotted as dots, and should (hopefully) approach the surface.
  • Three dimensional problems are plotted used a custom built Slice Plot.
  • Four dimensional problems are plotted using 3 Slice Plots. The leftmost plot fixes the variable of the fourth variable at -1, the middle plot at 0 and the rightmost plot at 1 (thus reducing the function to a three dimensional function, making a slice plot possible
  • Five dimensional problems are plotted using 9 Slice Plots. The fourth and fifth variables are fixed at values of -1, 0 and 1. Indicators below the plots show where the variables were fixed.
  • Higher dimensional problems: All variables after the fifth are fixed at 0, and plotting proceeds as if the model was five dimensional.

The toolbox handles complex valued outputs as their modulus (= absolute value = magnitude) for plotting purposes. These plots are just visual aids for monitoring the modeling process. Phase data can be extracted from the model files.

evaluate

 >> values = evaluate(model, samples);

This evaluates the model on the given samples. The samples should be provided in simulator space. Simulator space is defined by the range in the Simulator configuration. If no range (minimum and maximum) was specified, the domain is assumed to be [-1,1].

See also Using_a_model#Model_object_interfacing_and_optimization

evaluateDerivative

 >> values = evaluateDerivative(model, samples, [outputIndex]);

This approximates the partial derivatives of the model at each given sample. Note that the base class implementation is a very simple approximation. Models can override this function to provide more accurate derivatives (e.g., Kriging does this already). However, in its current form it is already useful.

getSamples

 >> samples = getSamples(model);

Returns the samples that were used to fit the model. The samples are returned in simulator space.

getValues

 >> values = getValues(model);

Returns the values that correspond to the samples from getSamples().

getDescription

 >> desc = getDescription(model);

Returns a string with a user friendly description of the model.

getExpression

 >> desc = getExpression(model,[outputNumber]);

Returns the symbolic mathematical expression of this model (e.g., 3*x1^2 - 2*x2 +5). Note that not all model types implement this.

construct

 >> model = construct(model,samples);

This will build/train/fit.. the model on the given set of data points and return the updated model.

complexity

 >> n = complexity(model);

Returns the number of free parameters in the model. By default this returns the number of datapoints the model was built with but this is overridden by some model types. For example, an ANN model returns the number of weights in the network while a rational model returns the number of coefficients.

Model object interfacing and optimization

You may want to use the model as part of a larger Matlab program, or you may simply want to optimizer the model. To easily do this you can create a function handle to the model object. You can do this as follows (example for the 3D case):

  handle = @(x,y,z) evaluate( model, [x,y,z] );

Afterwards, you can pass that handle to your optimization procedure, or use it through feval:

  fmincon( handle, ... );
  feval( handle, 0, 1, -1 );

Miscellaneous post-processing scripts

The SUMO Toolbox also includes some miscellaneous scripts you can use to analyze the results of your run. You can find them in ...\SUMO-Toolbox\src\scripts\matlab\ folder.