mplEFrontier.cs

CSsharp Example:


using System;
using Maximal.OptiMax;

namespace Efrontier
{
   class MplEFrontier
   {
      private OptiMax _mpl;
      private Model _model;
      private Solver _solver;
      private string _resultString;
      private double _targetReturn;
      private double _riskVariance;
      private int _solCount;

      public MplEFrontier()
      {
         _mpl = null;
         _model = null;
         _solver = null;
         _resultString = "";
         _targetReturn = 0.0;
         _riskVariance = 0.0;
         _solCount = 0;
      }


      private ResultType loadModelWithData(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.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 (Exception ex)
         {
            Console.WriteLine(ex.Message);
         }
         return model.LastResult;
      }


      public bool LoadModel(double targetReturn, string[] monthNames,
                        string[] stockNames, double[] stockPrices)
      {
         _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;
         ResultType result = loadModelWithData(_model, targetReturn, monthNames, stockNames, stockPrices);
         if (result != ResultType.Success) {
            _resultString = _model.Error.ToString();
            return false;
         }
         return true;
      }


      public bool SolveFirstModel(string solverName, double origTarget)
      {
         _solver = _mpl.Solvers.Add(solverName);
         if (_solver.Name == "CPLEX")
         {
            _solver.Options["BarrierIterLogInd"].Value = 0;
            _solver.Options["SimplexIterLogInd"].Value = 0;
         }
         return SolveModel(0, origTarget);
      }


      public bool SolveModel(int iter, double targetReturn)
      {
         const int CPLEX_OPTIMAL = 1;
         const int CPLEX_MIPOPTIMAL = 101;

         Constraint conMeetTarget = _model.PlainConstraints["MeetTarget"];
         conMeetTarget.RHSValue = targetReturn;
         ResultType result = _model.Solve(_solver);
         if (result != ResultType.Success) {
            _resultString = _model.ResultString + "\n" + _model.Solution.ResultString;
            return false;
         }
         if ((_model.Solution.ResultCode != CPLEX_OPTIMAL) &&
            (_model.Solution.ResultCode != CPLEX_MIPOPTIMAL)){
            _resultString = iter.ToString() + ")  FINISHED: " + _model.Solution.ResultString + " "
                          + _model.Solution.ResultCode.ToString();
            return false;
         }
         Macro macroVariance = _model.Macros["Variance"];
         _riskVariance = macroVariance.Value;
         _solCount++;
         _resultString = iter.ToString() + ") " +
                     "return = " + targetReturn.ToString() + "%,  " +
                     "risk = " + _riskVariance.ToString();
         return true;
      }


      public string ResultString
      {
         get { return _resultString; }
      }

      public int SolutionCount
      {
         get { return _solCount; }
      }

      public double RiskVariance
      {
         get { return _riskVariance; }
      }

      public double TargetReturn
      {
         get { return _targetReturn; }
      }

      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