Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Thu June 17, 2010 01:26 AM

    Originally posted by: VikramM


    Hi,

    I am currently using cplexqp() to solve a markowitz style portfolio optimization problem i.e. given a covariance matrix of returns and a vector of forecast returns i want to solve out for the asset weights that maximize a risk return tradeoff. I just want to add a cardinal constraint on the number assets I can hold. I guess I am looking for some example code to formulate the cardinality constraint so I can use cplexmiqp()

    Thanks.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Thu June 17, 2010 01:55 AM

    Originally posted by: John Cui


    Sorry, I don't catch your question.
    If you are looking for examples of our toolbox functions, then you can go to
    cplex\examples\src\matlab
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 12:28 AM

    Originally posted by: VikramM


    Let me clarify further. I do understand how to use the cplexqp() function, but I dont know how to setup a cardinal constraint and was looking for an example that does this. I could not find an example that uses cplexqp with such a constraint.

    Here is a stylized example:

    %Standard Markowitz portfolio
    n = 10;
    S = randn(n);S = S*S'/1000; % Covariance
    mu = rand(1,n)/100; % Expected return

    Aeq = ones(size(mu));
    beq = 1;
    lb = 0 * Aeq;
    ub = Aeq;

    w = cplexqp(2*S, -mu', ], [, Aeq, beq, lb', ub')

    I just want to add a constraint that ensures that I have a certain minimum and maximum number of non zero positions.

    i.e. minPos <= sum(W>0) <= maxPos

    where minPos & maxPos are integers.

    Any idea as to how I can setup such a constraint and use with cplexmiqp?
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 01:23 AM

    Originally posted by: John Cui


    Seems you need to use Cplex class better than cplexqp.
    Here is an example:
    %       Maximize
       %        obj: x1 + 2 x2 + 3 x3
       %               - 0.5 ( 33x1*x1 + 22*x2*x2 + 11*x3*x3
       %                    -  12*x1*x2 - 23*x2*x3 )
       %       Subject To
       %        c1: - x1 + x2 + x3 <= 20
       %        c2: x1 - 3 x2 + x3 <= 30
       %        q1: [ x1^2 + x2^2 + x3^3 ] <= 1.0
       %       Bounds
       %        0 <= x1 <= 40
       %        0 <= x2
       %        0 <= x3
       %       End
     
       cplex = Cplex('qcpex1');
       cplex.Model.sense = 'maximize';
       cplex.addCols([1 2 3]', [], [0; 0; 0], [40; inf; inf]);
       cplex.Model.Q   = [-33     6   0; ...
                            6   -22  11.5; ...
                            0  11.5 -11];
       cplex.addRows(-inf, [-1  1 1], 20);
       cplex.addRows(-inf, [ 1 -3 1], 30);
       cplex.addQCs([0 0 0]', [1 0 0;0 1 0;0 0 1], 'L', 1.0);
       cplex.solve();
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 01:34 AM

    Originally posted by: VikramM


    > John Cui wrote:
    > Seems you need to use Cplex class better than cplexqp.
    > Here is an example:
    >
    >    %       Maximize
    >    %        obj: x1 + 2 x2 + 3 x3
    >    %               - 0.5 ( 33x1*x1 + 22*x2*x2 + 11*x3*x3
    >    %                    -  12*x1*x2 - 23*x2*x3 )
    >    %       Subject To
    >    %        c1: - x1 + x2 + x3 <= 20
    >    %        c2: x1 - 3 x2 + x3 <= 30
    >    %        q1: [ x1^2 + x2^2 + x3^3 ] <= 1.0
    >    %       Bounds
    >    %        0 <= x1 <= 40
    >    %        0 <= x2
    >    %        0 <= x3
    >    %       End
    > 
    >    cplex = Cplex('qcpex1');
    >    cplex.Model.sense = 'maximize';
    >    cplex.addCols([1 2 3]', [], [0; 0; 0], [40; inf; inf]);
    >    cplex.Model.Q   = [-33     6   0; ...
    >                         6   -22  11.5; ...
    >                         0  11.5 -11];
    >    cplex.addRows(-inf, [-1  1 1], 20);
    >    cplex.addRows(-inf, [ 1 -3 1], 30);
    >    cplex.addQCs([0 0 0]', [1 0 0;0 1 0;0 0 1], 'L', 1.0);
    >    cplex.solve();
    >
    

    Thanks John.

    Are you saying that I need to use the Cplex class API to solve the problem because the toolbox function cplexmiqp cannot be used to solve problems with such constraints?
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 01:47 AM

    Originally posted by: John Cui


    In the toolbox functions, we don't give a way to add lhs to a constraint,
    so you have to reformulate you model to only have rhs in each constraint.

    But Cplex class can do most things like Concert/Callable Functions of CPLEX.
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 01:56 AM

    Originally posted by: VikramM


    Thanks for your response again. I'd prefer to use the toolbox over the API so could I get it to work if I had only a RHS on the constraint i.e. Sum(w>0) <= maxPos?

    If so how can I specify this using the Aineq or Aeq variables?
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri June 18, 2010 09:11 AM

    Originally posted by: John Cui


    sure, you can define Sum(>0) as Aineq, and maxPos as bineq in toolbox function.
    such as we can use below code to build a model:
    %   Maximize  x1 + 2 x2 + 3 x3 + x4
    %             - 0.5 ( 33x1*x1 + 22*x2*x2 + 11*x3*x3 - 12*x1*x2 - 23*x2*x3 )
    %   Subject to
    %      - x1 +   x2 + x3 + 10 x4 <= 20
    %        x1 - 3 x2 + x3         <= 30
    %               x2      - 3.5x4  = 0
    %   Bounds
    %        0 <= x1 <= 40
    %        0 <= x2
    %        0 <= x3
    %        2 <= x4 <= 3
    %   Integers
    %       x4
    

    H = [33   6     0    0;
          6  22    11.5  0;
          0  11.5  11    0;
          0   0     0    0];
       f     = [-1 -2 -3 -1]';
       
       Aineq = [-1  1  1 10;
          1 -3  1  0];
       bineq = [20  30]';
       
       Aeq   = [0  1  0 -3.5];
       beq   =  0;
       
       lb    = [ 0;   0;   0; 2];
       ub    = [40; inf; inf; 3];
       ctype = 'CCCI';
       
       options = cplexoptimset;
       options.Diagnostics = 'on';
       
       [x, fval, exitflag, output] = cplexmiqp (H, f, Aineq, bineq, Aeq, beq,...
          [], [], [], lb, ub, ctype, [], options);
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Fri April 24, 2015 08:18 PM

    Originally posted by: Bellilein


    Hi,

    I'm trying to do the same. I don't know how to get the cardinality constrained efficient frontier (CCEF) like in the attached picture, which is plotted by the same data. I would really appreciate it if you could help me with that! Thanks in advance!

    numx=4;

     

     

    sigma=[1 0.118368 0.143822 0.252213 0 0 0 0;

           0.118368 1 0.164589 0.099763 0 0 0 0;

           0.143822 0.164589 1 0.083122 0 0 0 0;

           0.252213 0.099763 0.083122 1 0 0 0 0;

           ];

     

     

     

    K = 2;

     

    mu=[0.004798

        0.000659

        0.003174

        0.001377];

     

     

    l=0.1*ones(1,numx);

     

    u=ones(1,numx);

     

    rhomin=0.0025;

     rhomax=0.0045;

     

     anzPlots=100;

     

     rhovec=0:(anzPlots-1);

     rhovec=rhomin*ones(1,anzPlots)+rhovec*(rhomax-rhomin)/(anzPlots-1);

     rvvec=zeros(1,anzPlots);

     

     i=1;

     exitflag=1;

     

     while i<anzPlots && exitflag==1

         

         rho=rhovec(i);

     

    Aineq=[ -eye(numx),diag(l);

            eye(numx),-diag(u)

            ];

    bineq=[0;0;0;0;0;0;0;0

            ];

     

    Aeq=[mu',zeros(1,numx);

        ones(1,numx),zeros(1,numx);

        zeros(1,numx),ones(1,numx);

        ];

    beq=[rho; 

        1;

        K;

        ];

     

    lb=[0; 0; 0; 0; 0; 0; 0; 0];

    ub=[1;1;1;1;1;1;1;1];

    ctype= 'CCCCBBBB'

     

     

    [x, fval, exitflag, output] = cplexmiqp (2*sigma,zeros(1,2*numx),Aineq,bineq,Aeq,beq,[],[],[],lb,ub,ctype);

       fprintf ('\nSolution status = %s \n', output.cplexstatusstring);

       fprintf ('Solution value = %f \n', fval);

       disp ('Values Wallieus =');

       disp (x');

       disp (rho);

       

       

    rvvec(i)=sqrt(fval);

     i=i+1;

     end

     

     

     plot(rvvec(1:i-1),rhovec(1:i-1))

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Implementing cardinality constraint in Cplex 12 toolbox for matlab

    Posted Mon May 18, 2015 01:28 AM

    Can you please elaborate on what exactly your problem is? If I understand your code correctly then you sample rho and then plot rho versus the expected return. Is the problem creating the plot or do you want to plot something else? Or are you seeing unexpected results?


    #CPLEXOptimizers
    #DecisionOptimization