VBNet Example:
Imports Maximal.OptiMax Public Class MplPortfolio Private _mpl As OptiMax Private _model As Model Private _solver As Solver Private _resultString As String Private _riskVariance As Double Private _investNames() As String Private _investAmounts() As Double Private _isSolving As Boolean Public Sub New() _mpl = Nothing _model = Nothing _solver = Nothing _resultString = "" _riskVariance = 0.0 ReDim _investNames(0) ReDim _investAmounts(0) _isSolving = False End Sub Private Function loadModel(ByVal model As Model, ByVal targetReturn As Double, ByVal monthNames() As String, _ ByVal stockNames() As String, ByVal stockPrices() As Double) As ResultType Const minInvest As Double = 0.0 Const maxInvest As Double = 1.0 Const minStock As Integer = 5 Const maxStock As Integer = 10 Try Dim idxStock As IndexSet = model.IndexSets.Add("stock", stockNames) model.IndexSets.Add("stock2", idxStock) model.IndexSets.Add("month", monthNames) model.IndexSets.Add("period[month]", "month - (First(month))") model.DataConstants.Add("MinInvest", minInvest) model.DataConstants.Add("MaxInvest", maxInvest) model.DataConstants.Add("MinStocks", minStock) model.DataConstants.Add("MaxStocks", maxStock) model.DataConstants.Add("TargetReturn", targetReturn) model.DataVectors.Add("Price[stock,month]", stockPrices) model.ReadFilePart("Portfolio.mpl", "DATA_RETURN_MARKER") Catch ex As Exception Console.WriteLine(ex.Message) End Try Return model.LastResult End Function Public Function SolveModel(ByVal solverName As String, ByVal targetReturn As Double, ByVal monthNames() As String, _ ByVal stockNames() As String, ByVal stockPrices() As Double) As Boolean _mpl = New OptiMax() _model = _mpl.Models.Add("Portfolio") _solver = _mpl.Solvers.Add(solverName) If _solver Is Nothing Then _resultString = "Solver " & solverName & " was not found." Return False End If _mpl.Options("ModelType").ValueStr = "Quadratic" _model.WorkingDirectory = _mpl.ModelDirectory Dim result As ResultType = loadModel(_model, targetReturn, monthNames, stockNames, stockPrices) If Not result = ResultType.Success Then _resultString = _model.Error.ToString() Return False End If result = _model.Solve(_solver) If Not result = ResultType.Success Then _resultString = "Solver " & _solver.Name & ": " & _model.ResultString & vbCrLf & _model.Solution.ResultString Return False End If _resultString = _model.Solution.ResultString getSolutionValues(_model) Return True End Function Public Function ResolveModelForNewTarget(ByVal targetReturn As Double) As Boolean If _mpl Is Nothing Then _resultString = "Model has not been solved yet." Return False End If If _isSolving Then Return True End If _isSolving = True Dim conMeetTarget As Constraint = _model.PlainConstraints("MeetTarget") conMeetTarget.RHSValue = targetReturn Dim result As ResultType = _model.Solve(_solver) If Not result = ResultType.Success Then _resultString = "Solver " & _solver.Name & ": " & _model.ResultString & vbCrLf & _model.Solution.ResultString _isSolving = False Return False End If _resultString = _model.Solution.ResultString getSolutionValues(_model) _isSolving = False Return True End Function Private Sub getSolutionValues(ByVal model As Model) Dim investVect As VariableVector = model.VariableVectors("Invest") investVect.ZeroTol = 0.001 Dim solNames As List(Of String) = New List(Of String) Dim solValues As List(Of Double) = New List(Of Double) For Each var As Variable In investVect.NonzeroVariables solNames.Add(investVect.Subscripts(1).ValueStr) solValues.Add(var.Activity) Next var _investAmounts = solValues.ToArray() _investNames = solNames.ToArray() _riskVariance = model.Solution.ObjectValue End Sub Public ReadOnly Property ResultString() As String Get Return _resultString End Get End Property Public ReadOnly Property RiskVariance() As Double Get Return _riskVariance End Get End Property Public ReadOnly Property InvestNames() As String() Get Return _investNames End Get End Property Public ReadOnly Property InvestAmounts() As Double() Get Return _investAmounts End Get End Property Public ReadOnly Property DataDir() As String Get If _mpl Is Nothing Then Return GetDataDir() Else Return _mpl.ModelDirectory End If End Get End Property Public Shared Function GetDataDir() As String Dim tempMpl As OptiMax = New OptiMax() Dim dataDir As String = tempMpl.ModelDirectory tempMpl = Nothing Return dataDir End Function End Class