MIP model 2: imposing a minimum investment in each share
To formulate the second MIP model, we start again with the LP model from Chapter Building models and Inputting and solving a Linear Programming problem. The new constraint we wish to formulate is `if a share is bought, a minimum of 10% of the budget is spent on the share. Instead of simply constraining every variable fracs to take a value between 0 and 0.3, we now require it to either lie in the interval between 0.1 and 0.3 or take the value 0. This type of variable is known as a semi-continuous variable. In the new model, we replace the bounds on the variables fracs by the following constraint:
Implementation with Python
The following program implements the MIP model 2. The semi-continuous variables are defined by the variable type xp.semicontinuous. By default, Xpress assumes a continuous limit of 1, so we need to set this value to 0.1 with the argument threshold.
A similar type is available for integer variables that take either the value 0 or an integer value between a given limit and their upper bound (so-called semi-continuous integers): xp.semiinteger. A third composite type is a partial integer which takes integer values from its lower bound to a given limit value and is continuous beyond this value (marked by xp.semiinteger).
The following code snippet shows the implementation of our example using semicontinuous variables, with the remaining code being the same as for the previous section:
import xpress as xp # Problem data MAXNUM = 4 NSHARES = 10 RET = [5, 17, 26, 12, 8, 9, 7, 6, 31, 21] RISK = [1, 2, 3, 8, 9] NA = [0, 1, 2, 3] p = xp.problem(name="Folio") # VARIABLES. frac = p.addVariables(NSHARES, vartype=xp.semicontinuous, threshold=0.1, ub=0.3, name="frac") # CONSTRAINTS. # Limit the percentage of high-risk values. p.addConstraint(xp.Sum(frac[i] for i in RISK) <= 1/3) # Minimum amount of North-American values. p.addConstraint(xp.Sum(frac[i] for i in NA) >= 0.5) # Spend all the capital. p.addConstraint(xp.Sum(frac) == 1) # Objective: maximize total return. p.setObjective(xp.Sum(frac[i] * RET[i] for i in range(NSHARES)), sense=xp.maximize) # 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("Total return:", p.attributes.objval) sol = p.getSolution(frac) for i in range(NSHARES): print(f"{frac[i].name} : {sol[i]*100:.2f} %")
When executing this program we obtain the following output (leaving out the part printed by Xpress):
Total return: 14.033333333333331 frac(0) : 30.00 % frac(1) : 0.00 % frac(2) : 20.00 % frac(3) : 0.00 % frac(4) : 10.00 % frac(5) : 26.67 % frac(6) : 0.00 % frac(7) : 0.00 % frac(8) : 13.33 % frac(9) : 0.00 %
Now five stocks are chosen for the portfolio, each comprising at least 10% and at most 30% of the total investment. Due to the additional constraint, the optimal MIP solution value is again lower than the initial LP solution value.
© 2001-2025 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.