mplEFrontier.vb

VBNet Example:


Imports Maximal.OptiMax


Public Class MplEFrontier

   Private _mpl As OptiMax
   Private _model As Model
   Private _solver As Solver
   Private _resultString As String
   Private _targetReturn As Double
   Private _riskVariance As Double
   Private _solCount As Integer


   Public Sub New()
      _mpl = Nothing
      _model = Nothing
      _solver = Nothing
      _resultString = ""
      _targetReturn = 0.0
      _riskVariance = 0.0
      _solCount = 0
   End Sub


   Private Function loadModelWithData(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.AddAlias("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 LoadModel(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("EFrontier")
      _mpl.Options("ModelStats").Value = 0
      _mpl.Options("StatusMessages").Value = 0
      _mpl.Options("ModelType").ValueStr = "Quadratic"
      _model.WorkingDirectory = _mpl.ModelDirectory
      Dim result As ResultType = loadModelWithData(_model, targetReturn, monthNames, stockNames, stockPrices)
      If Not result = ResultType.Success Then
         _resultString = _model.Error.ToString()
         Return False
      End If
      Return True
   End Function


   Public Function SolveFirstModel(ByVal solverName As String, ByVal origTarget As Double) As Boolean
      _solver = _mpl.Solvers.Add(solverName)
      If _solver.Name = "CPLEX" Then
         _solver.Options("BarrierIterLogInd").Value = 0
         _solver.Options("SimplexIterLogInd").Value = 0
      End If
      Return SolveModel(0, origTarget)
   End Function


   Public Function SolveModel(ByVal iter As Integer, ByVal targetReturn As Double) As Boolean
      Const CPLEX_OPTIMAL As Integer = 1
      Const CPLEX_MIPOPTIMAL As Integer = 101

      Dim conMeetTarget As Constraint = _model.PlainConstraints("MeetTarget")
      conMeetTarget.RHSValue = targetReturn
      Dim result As ResultType = _model.Solve(_solver)
      If Not result = ResultType.Success Then
         _resultString = _model.ResultString & vbCrLf & _model.Solution.ResultString
         Return False
      End If
      If (Not _model.Solution.ResultCode = CPLEX_OPTIMAL) And _
         (Not _model.Solution.ResultCode = CPLEX_MIPOPTIMAL) Then
         _resultString = iter.ToString() & ")  FINISHED: " & _model.Solution.ResultString
         Return False
      End If
      Dim macroVariance As Macro = _model.Macros("Variance")
      _riskVariance = macroVariance.Value
      _solCount = _solCount + 1
      _resultString = iter.ToString() & ") " & _
                  "return = " & targetReturn.ToString() & "%,  " & _
                  "risk = " & _riskVariance.ToString()
      Return True
   End Function


   Public ReadOnly Property ResultString() As String
      Get
         Return _resultString
      End Get
   End Property

   Public ReadOnly Property SolutionCount() As Integer
      Get
         Return _solCount
      End Get
   End Property

   Public ReadOnly Property RiskVariance() As Double
      Get
         Return _riskVariance
      End Get
   End Property

   Public ReadOnly Property TargetReturn() As Double
      Get
         Return _targetReturn
      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


        

Back To Top | Maximal Home Page | List of Samples | Previous Page | Next Page