Initializing help system before first use

# MIQP

We now wish to express the fact that at most a given number MAXNUM of different assets may be selected into the portfolio, subject to all other constraints of the previous QP model. In Chapter Mixed Integer Programming we have already seen how this can be done by introducing an additional set of binary decision variables buys that are linked logically to the continuous variables:

∀ s ∈ SHARES: fracs ≤ buys

Through this relation, a variable buys will be at 1 if a fraction fracs greater than 0 is selected into the portfolio. If, however, buys equals 0, then fracs must also be 0.

To limit the number of different shares in the portfolio, we then define the following constraint:

∑s ∈ SHARES buys ≤ MAXNUM

## Implementation with Python

We may modify the previous QP model or simply append the following lines to the program of the previous section, just after the solution printing: the problem is then solved once as a QP and once as a MIQP in a single program run.

    # *******SECOND PROBLEM: LIMIT NUMBER OF ASSETS********
    buy = p.addVariables(NSHARES, vartype=xp.binary, name="buy")

    # CONSTRAINTS.
    # Minimum amount of North-American values.
    p.addConstraint(xp.Sum(frac[i] for i in NA) >= 0.5)

    # Limit the total number of assets.
    p.addConstraint(xp.Sum(buy) <= MAXNUM)

    # Linking the variables.
    p.addConstraint(frac[i] <= buy[i] for i in range(NSHARES))

    # Solve.
    p.optimize()

    # Print problem status.
    print(f"Problem status: \n\t Solve status: {p.attributes.solvestatus.name} \n\t Sol status: \
        {p.attributes.solstatus.name}")

    # Solution printing.
    print(f"With a target of {TARGET} minimum variance is {p.attributes.objval}")
    sol = p.getSolution(frac)
    for i in range(NSHARES):
        print(f"{frac[i].name} : {sol[i]*100:.2f} %")

When executing the MIQP model, we obtain the following solution output:

    Minimizing MIQP Folio using up to 20 threads and up to 31GB memory, with these control settings:
    OUTPUTLOG = 1
    NLPPOSTSOLVE = 1
    XSLP_DELETIONCONTROL = 0
    XSLP_OBJSENSE = 1
    Original problem has:
            15 rows           20 cols           58 elements        10 entities
            76 qobjelem
    Presolved problem has:
            14 rows           20 cols           54 elements        10 entities
            76 qobjelem
    LP relaxation tightened
    Presolve finished in 0 seconds
    Heap usage: 3773KB (peak 11MB, 103KB system)

    Coefficient range                    original                 solved
      Coefficients   [min,max] : [ 1.00e+00,  3.10e+01] / [ 6.25e-02,  1.00e+00]
      RHS and bounds [min,max] : [ 3.00e-01,  9.00e+00] / [ 5.00e-01,  4.80e+00]
      Objective      [min,max] : [      0.0,       0.0] / [      0.0,       0.0]
      Quadratic      [min,max] : [ 2.00e-01,  5.60e+01] / [ 7.81e-03,  6.88e-01]
    Autoscaling applied standard scaling

    Will try to keep branch and bound tree memory usage below 23.3GB
    Crash basis containing 10 structural columns created

       Its         Obj Value      S   Ninf  Nneg   Sum Dual Inf  Time
         0           .000000      D      3     0        .000000     0
         3           .000000      D      0     0        .000000     0
         3          1.544000      P      0     0        .000000     0

       Its         Obj Value      S   Nsft  Nneg       Dual Inf  Time
        11           .557393     QP      0     0        .000000     0
    QP solution found
    Optimal solution found
    Primal solved problem
      11 simplex iterations in 0.00 seconds at time 0

    Final objective                       : 5.573934108103896e-01
      Max primal violation      (abs/rel) : 4.337e-17 / 4.337e-17
      Max dual violation        (abs/rel) :       0.0 /       0.0
      Max complementarity viol. (abs/rel) : 1.967e-16 / 8.590e-17

    Starting root cutting & heuristics
    Deterministic mode with up to 4 additional threads

     Its Type    BestSoln    BestBound   Sols    Add    Del     Gap     GInf   Time
    a            4.094716      .557393      1                 86.39%       0      0
    b            1.839001      .557393      2                 69.69%       0      0
    q            1.568666      .557393      3                 64.47%       0      0
    k            1.419000      .557393      4                 60.72%       0      0
       1  K      1.419000      .557393      4      5      0   60.72%       7      0
       2  K      1.419000      .557393      4      3      2   60.72%       7      0
       3  K      1.419000      .557393      4     10      2   60.72%       7      0
       4  K      1.419000      .560790      4      7      7   60.48%       7      0
       5  K      1.419000      .570150      4     13      7   59.82%       8      0
       6  K      1.419000      .611241      4     16     10   56.92%       9      0
       7  K      1.419000      .623987      4     19     13   56.03%       9      0
    P            1.248762      .623987      5                 50.03%       0      0
       8  K      1.248762      .628253      5      8     17   49.69%       8      0
       9  K      1.248762      .628253      5      0      9   49.69%       9      0
    Heuristic search 'R' started
    Heuristic search 'R' stopped

    Cuts in the matrix         : 14
    Cut elements in the matrix : 116

    Starting tree search.
    Deterministic mode with up to 20 running threads and up to 64 tasks.
    Heap usage: 4428KB (peak 11MB, 108KB system)

        Node     BestSoln    BestBound   Sols Active  Depth     Gap     GInf   Time
           1     1.248762      .628257      5      2      1   49.69%       9      0
           2     1.248762      .628257      5      2      3   49.69%       5      0
           5     1.248762      .628257      5      2      4   49.69%       6      0
           8     1.248762      .628257      5      2      4   49.69%       5      0
           9     1.248762      .628257      5      1      3   49.69%       5      0
          10     1.248762      .628257      5      1      4   49.69%       4      0
          20     1.248762     1.045056      5      2      6   16.31%       4      0
     *** Search completed ***
    Numerical issues encountered:
       Singular bases   :      5 out of        79 (ratio: 0.0633)
    Uncrunching matrix
    Final MIP objective                   : 1.248761904761905e+00
    Final MIP bound                       : 1.248751904761905e+00
      Solution time / primaldual integral :      0.04s/ 51.870009%
      Work / work units per second        :      0.01 /      0.34
      Number of solutions found / nodes   :         5 /        23
      Max primal violation      (abs/rel) :       0.0 /       0.0
      Max integer violation     (abs    ) :       0.0
    Problem status:
    	 Solve status: COMPLETED
    	 Sol status:     OPTIMAL
    With a target of 9 minimum variance is 1.2487619047619054
    frac(0) : 30.00 %
    frac(1) : 20.00 %
    frac(2) : 0.00 %
    frac(3) : 0.00 %
    frac(4) : 23.81 %
    frac(5) : 26.19 %
    frac(6) : 0.00 %
    frac(7) : 0.00 %
    frac(8) : 0.00 %
    frac(9) : 0.00 %

The log of the Branch-and-Bound search tells us this time that 5 integer feasible solutions have been found (all by the MIP heuristics), and a total of 23 nodes have been enumerated to complete the search. With the additional constraint on the number of different assets the minimum variance is more than twice as large as in the QP problem.

Parent Topic Quadratic Programming

© 2001-2026 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.