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
MWObjectArrayclass 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 Functions | doOptim.m
displayObj.m
|
| MATLAB Function Location | |
| C# Code Location | |
| Visual Basic Code Location | |
| MWArray API Reference Location | |
Procedure
Copy the following folder that ships with MATLAB to your work folder:
matlabroot\toolbox\dotnetbuilder\Examples\VSVersion\NET\OptimizeExampleAt the MATLAB command prompt, navigate to the new
OptimizeExample\OptimizeCompsubfolder in your work folder.Examine the MATLAB code that you want to access. This example uses
doOptim.manddisplayObj.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)
Build the .NET component with the .NET Assembly Compiler app or
compiler.build.dotNETAssemblyusing the following information:Field Value Library Name OptimizeCompClass Name OptimizeComp.OptimizeClassFiles to Compile doOptim.m
displayObj.mFor 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.
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.csVisual 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.
The
BananaFunctionclass implements the Rosenbrock banana function described in thefminsearchdocumentation.Open the .NET project file that corresponds to your application language using Visual Studio.
C#
If you are using C#, the
OptimizeCSAppfolder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingOptimizeCSApp.csprojin 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
OptimizeVBAppfolder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clickingOptimizeVBApp.vbprojin Windows Explorer. You can also open it from the desktop by right-clicking OptimizeVBApp.vbproj and selecting Open Outside MATLAB.
Add a reference to your assembly file
OptimizeComp.dll.Add a reference to the
MWArrayAPI.If MATLAB is installed on your system matlabroot\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllIf MATLAB Runtime is installed on your system <MATLAB_RUNTIME_INSTALL_DIR>\toolbox\dotnetbuilder\bin\win64\<framework_version>\MWArray.dllBuild and run the
OptimizeAppapplication 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
