mplPortfolio.cs

CSsharp Example:


using System;
using System.Collections.Generic;
using Maximal.OptiMax;

namespace Portfolio
{
   class MplPortfolio
   {
      private OptiMax _mpl;
      private Model _model;
      private Solver _solver;
      private string _resultString;
      private double _riskVariance;
      private string[] _investNames;
      private double[] _investAmounts;
      private bool _isSolving;

      public MplPortfolio()
      {
         _mpl = null;
         _model = null;
         _solver = null;
         _resultString = "";
         _riskVariance = 0.0;
         _investNames = null;
         _investAmounts = null;
         _isSolving = false;
      }

      private ResultType loadModel(Model model, double targetReturn, string[] monthNames,
                           string[] stockNames, double[] stockPrices)
      {
         const double minInvest = 0.0;
         const double maxInvest = 1.0;
         const int minStock = 5;
         const int maxStock = 10;

         try
         {
            IndexSet idxStock = 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 (Exception ex)
         {
            Console.WriteLine(ex.Message);
         }
         return model.LastResult;
      }


      public bool SolveModel(string solverName, double targetReturn, string[] monthNames,
                        string[] stockNames, double[] stockPrices)
      {
         _mpl = new OptiMax();
         _model = _mpl.Models.Add("Portfolio");
         _solver = _mpl.Solvers.Add(solverName);
         if (_solver == null)
         {
            _resultString = "Solver " + solverName + " was not found.";
            return false;
         }
         _mpl.Options["ModelType"].ValueStr = "Quadratic";
         _model.WorkingDirectory = _mpl.ModelDirectory;
         ResultType result = loadModel(_model, targetReturn, monthNames, stockNames, stockPrices);
         if (result != ResultType.Success)
         {
            _resultString = _model.Error.ToString();
            return false;
         }
         result = _model.Solve(_solver);
         if (result != ResultType.Success)
         {
            _resultString = "Solver " + _solver.Name + ": " + _model.ResultString + "\n" + _model.Solution.ResultString;
            return false;
         }
         _resultString = _model.Solution.ResultString;
         getSolutionValues(_model);
         return true;
      }

      public bool ResolveModelForNewTarget(double targetReturn)
      {
         if (_mpl == null)
         {
            _resultString = "Model has not been solved yet.";
            return false;
         }
         if (_isSolving)
         {
            return true;
         }
         _isSolving = true;
         Constraint conMeetTarget = _model.PlainConstraints["MeetTarget"];
         conMeetTarget.RHSValue = targetReturn;
         ResultType result = _model.Solve(_solver);
         if (result != ResultType.Success)
         {
            _resultString = "Solver " + _solver.Name + ": " + _model.ResultString + "\n" + _model.Solution.ResultString;
            return false;
         }
         _resultString = _model.Solution.ResultString;
         getSolutionValues(_model);
         _isSolving = false;
         return true;
      }




      private void getSolutionValues(Model model)
      {
         VariableVector investVect = model.VariableVectors["Invest"];
         investVect.ZeroTol = 0.001;
         List<string> solNames = new List<string>();
         List<double> solValues = new List<double>();
         foreach (Variable var in investVect.NonzeroVariables)
         {
            solNames.Add(investVect.Subscripts[1].ValueStr);
            solValues.Add(var.Activity);
         }
         _investAmounts = solValues.ToArray();
         _investNames = solNames.ToArray();
         _riskVariance = model.Solution.ObjectValue;
      }


      public string ResultString
      {
         get { return _resultString; }
      }

      public double RiskVariance
      {
         get { return _riskVariance; }
      }

      public string[] InvestNames
      {
         get { return _investNames; }
      }

      public double[] InvestAmounts
      {
         get { return _investAmounts; }
      }

      public string DataDir
      {
         get
         {
            if (_mpl == null)
            {
               return GetDataDir();
            }
            return _mpl.ModelDirectory;
         }
      }

      public static string GetDataDir()
      {
         OptiMax tempMpl = new OptiMax();
         string dataDir = tempMpl.ModelDirectory;
         tempMpl = null;
         return dataDir;
      }

   }
}

        

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