Initializing help system before first use

MIP model 2: imposing a minimum investment in each share

To formulate the second MIP model, we start again with the LP model from Chapters 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:

∀ s ∈ SHARES: fracs = 0 or 0.1 ≤ fracs ≤ 0.3

Implementation with Java

The following program implements the MIP model 2. The semi-continuous variables are defined by the column type SemiContinuous. By default, Xpress assumes a continuous limit of 1, so we need to set this value to 0.1 with the method withLimit.

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): 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 PartialInteger).

The following code snippet shows the implementation of the main function for our example using SemiContinuous variables, with the remaining code being the same as for the previous section:

public static void main(String[] args) {
    try (XpressProblem prob = new XpressProblem()) {
        // Output all messages.
        prob.callbacks.addMessageCallback(DefaultMessageListener::console);

        /**** VARIABLES ****/
        Variable[] frac = prob.addVariables(NSHARES)
                /* Fraction of capital used per share */
                .withName(i -> String.format("frac_%d", i))
                .withType(ColumnType.SemiContinuous)
                /* Upper bounds on the investment per share */
                .withUB(0.3)
                /* Investment limit */
                .withLimit(0.1).toArray();

        /**** CONSTRAINTS ****/
        /* Limit the percentage of high-risk values */
        prob.addConstraint(sum(NRISK, i -> frac[RISK[i]]).leq(1.0 / 3.0).setName("Risk"));

        /* Minimum amount of North-American values */
        prob.addConstraint(sum(NNA, i -> frac[NA[i]]).geq(0.5).setName("NA"));

        /* Spend all the capital */
        prob.addConstraint(sum(frac).eq(1.0).setName("Cap"));

        /* Objective: maximize total return */
        prob.setObjective(scalarProduct(frac, RET), ObjSense.MAXIMIZE);

        /* Solve */
        prob.optimize();

        /* Solution printing */
        printProblemStatus(prob);
        System.out.println("Total return: " + prob.attributes().getObjVal());
        double[] sol = prob.getSolution();
        range(0, NSHARES).forEach(i -> System.out
                .println(String.format("%s : %.2f%s", frac[i].getName(),
                         100.0 * frac[i].getValue(sol), "%")));
    }
}

When executing this program we obtain the following output (leaving out the part printed by the Optimizer):

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 securities 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.