Main Content

Integrating MATLAB Optimization Routines with Objective Functions in Visual Basic

Optimization Example

Purpose

This example shows how to:

  • Use the MATLAB® Compiler SDK™ product to create an assembly (OptimizeComp). This assembly applies MATLAB optimization routines to objective functions implemented as .NET objects.

  • Access the component in a .NET application (OptimizeApp.vb). Then, use the MWObjectArray class to create a reference to a .NET object (BananaFunction.vb), and pass that object to the component.

    Note

    For information about these data conversion classes, see the MATLAB MWArray Class Library Reference, available in the matlabroot\help\dotnetbuilder\MWArrayAPI folder, where matlabroot represents your MATLAB installation folder

  • Build and run the application.

OptimizeComp Component

The component (OptimizeComp) finds a local minimum of an objective function and returns the minimal location and value. The component uses the MATLAB optimization function fminsearch. This example optimizes the Rosenbrock banana function used in the fminsearch documentation.

The class OptimizeComp.OptimizeClass performs an unconstrained nonlinear optimization on an objective function implemented as a .NET object. A method of this class, doOptim, accepts an initial value (NET object) that implements the objective function, and returns the location and value of a local minimum.

The second method, displayObj, is a debugging tool that lists the characteristics of a .NET object. These two methods, doOptim and displayObj, encapsulate MATLAB functions. The MATLAB code for these two methods resides in doOptim.m and displayObj.m. You can find this code in matlabroot\toolbox\dotnetbuilder\Examples\VSversion\NET\OptimizeExample\OptimizeVBApp.

Procedure

  1. If you have not already done so, copy the files for this example as follows:

    1. Copy the following folder that ships with MATLAB to your work folder: matlabroot\toolbox\dotnetbuilder\Examples\VSversion\NET\OptimizeExample

    2. At the MATLAB command prompt, cd to the new OptimizeExample subfolder in your work folder.

  2. If you have not already done so, set the environment variables that are required on a development machine.

  3. Write the MATLAB code that you want to access. This example uses doOptim.m and displayObj.m, which already resides in your work folder. The path is matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeComp.

    For reference, the code of doOptim.m is displayed here:

    function [x,fval] = doOptim(h, x0)
    mWrapper = @(x) h.evaluateFunction(x);
    
    directEval = h.evaluateFunction(x0)
    wrapperEval = mWrapper(x0)
    
    [x,fval] = fminsearch(mWrapper,x0)
    For reference, the code of displayObj.m is displayed here:
    function className = displayObj(h)
    
    h
    className = class(h)
    whos('h')
    methods(h)

  4. From the MATLAB apps gallery, open the Library Compiler app.

  5. As you compile the .NET application using the Library Compiler, use the following information:

    Project NameOptimizeComp
    Class NameOptimizeComp.OptimizeClass
    File to compile

    doOptim.m displayObj.m

  6. Write source code for a class (BananaFunction) that implements an object function to optimize. The sample application for this example is in matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeVBApp. The program listing for BananaFunction.vb displays the following code:

    Imports System
    
    Namespace MathWorks.Examples.Optimize
    
        Class BananaFunction
    
    #Region "Methods"
            Public Sub BananaFunction()
            End Sub
    
            Public Function evaluateFunction(ByVal x As Double()) As Double
    
                Dim term1 As Double = 100 * Math.Pow((x(1) - Math.Pow(x(0), 
                                                                 2.0)), 2.0)
                Dim term2 As Double = Math.Pow((1 - x(0)), 2.0)
                Return term1 + term2
            End Function
    #End Region
    
        End Class
    End Namespace
    
    
    
    The class implements the Rosenbrock banana function described in the fminsearch documentation.

  7. Customize the application using Visual Studio® .NET using the OptimizeVBApp folder, which contains a Visual Studio .NET project file for this example.

    1. The OptimizeVBApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking OptimizeVBApp.vbproj in Windows® Explorer. You can also open it from the desktop by right-clicking OptimizeVBApp.vbproj > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version\mwarray.dll.

    3. If necessary, add (or fix the location of) a reference to the OptimizeComp component which you built in a previous step. (The component, OptimizeComp.dll, is in the \OptimizeExample\OptimizeComp\x86\V2.0\Debug\distrib subfolder of your work area.)

When run successfully, the program displays the following output:

Using initial points= -1.2000 1


*****************************************************
**            Properties of .NET Object            **
*****************************************************

h =

  MathWorks.Examples.Optimize.BananaFunction handle w
      ith no properties.
  Package: MathWorks.Examples.Optimize




className =

MathWorks.Examples.Optimize.BananaFunction


  Name  Size   Bytes  Class          Attributes

  h     1x1    60   MathWorks.Examples.Optimize.BananaFunction



Methods for class MathWorks.Examples.Optimize.BananaFunction:

BananaFunction    addlistener       findprop          lt
Equals            delete            ge                ne
GetHashCode       eq                gt                notify
GetType           evaluateFunction  isvalid
ToString          findobj           le


**************** Finished displayObj ****************


*****************************************************
** Performing unconstrained nonlinear optimization **
*****************************************************

directEval =

   24.2000



wrapperEval =

   24.2000



x =

    1.0000    1.0000



fval =

  8.1777e-010


***************** Finished doOptim ******************


Location of minimum: 1.0000    1.0000
Function value at minimum: 8.1777e-010