Main Content

Integrate MATLAB Optimization Routines with Objective Functions

Overview

This example shows you how to create a .NET application that finds a local minimum of an objective function using the MATLAB® optimization function fminsearch and the MWObjectArray class.

In this example, you perform the following steps:

  • 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 either a C# application (OptimizeApp.cs) or a Visual Basic® application (OptimizeApp.vb).

  • Use the MWObjectArray class to create a reference to a .NET object using C# (BananaFunction.cs) or Visual Basic (BananaFunction.vb), and pass that object to the component.

  • Build and run the application using the Visual Studio® .NET development environment.

OptimizeComp Application

The OptimizeComp application 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. The two methods doOptim and displayObj encapsulate MATLAB functions.

Files

MATLAB FunctionsdoOptim.m
displayObj.m
MATLAB Function Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeComp
C# Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeCSApp\BananaFunction.cs
Visual Basic Code Locationmatlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExample\OptimizeVBApp\BananaFunction.vb
MWArray API Reference Locationmatlabroot\help\dotnetbuilder\MWArrayAPI

Procedure

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

    At the MATLAB command prompt, navigate to the new OptimizeExample\OptimizeComp subfolder in your work folder.

  2. Examine the MATLAB code that you want to access. This example uses doOptim.m and displayObj.m.

    function [x,fval] = doOptim(h, x0)
    mWrapper = @(x) h.evaluateFunction(x);
    
    directEval = h.evaluateFunction(x0)
    wrapperEval = mWrapper(x0)
    
    [x,fval] = fminsearch(mWrapper,x0)
    function className = displayObj(h)
    
    h
    className = class(h)
    whos('h')
    methods(h)

  3. Build the .NET component with the Library Compiler app or compiler.build.dotNETAssembly using the following information:

    FieldValue
    Library NameOptimizeComp
    Class NameOptimizeComp.OptimizeClass
    Files to CompiledoOptim.m
    displayObj.m

    For example, if you are using compiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotNETAssembly(["doOptim.m","displayObj.m"], ...
    'AssemblyName','OptimizeComp', ...
    'ClassName','OptimizeComp.OptimizeClass');

    For more details, see the instructions in Generate .NET Assembly and Build .NET Application.

  4. Decide whether you are using C# or Visual Basic to access the component.

    • C#

      If you are using C#, write source code for a class that implements an object function to optimize.

      The sample application for this example is in OptimizeExample\OptimizeCSApp\BananaFunction.cs

       BananaFunction.cs

    • Visual Basic

      If you are using Visual Basic, write source code for a class that implements an object function to optimize.

      The sample application for this example is in OptimizeExample\OptimizeVBApp\BananaFunction.vb.

       BananaFunction.vb

    The BananaFunction class implements the Rosenbrock banana function described in the fminsearch documentation.

  5. Open the .NET project file that corresponds to your application language using Visual Studio.

    • C#

      If you are using C#, the OptimizeCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking OptimizeCSApp.csproj in Windows® Explorer. You can also open it from the desktop by right-clicking OptimizeCSApp.csproj and selecting Open Outside MATLAB.

    • Visual Basic

      If you are using Visual Basic, 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 and selecting Open Outside MATLAB.

  6. Add a reference to your assembly file OptimizeComp.dll.

  7. Add a reference to the MWArray API.

    If MATLAB is installed on your systemmatlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll
    If MATLAB Runtime is installed on your system<MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dll

  8. Build and run the OptimizeApp application in Visual Studio .NET.

The program displays the following output:

Using initial points= -1.2000 1


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

h =

  MathWorks.Examples.Optimize.BananaFunction handle 
             with 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

See Also

| |

Related Examples

More About