Initializing help system before first use

Create the Python Project

In this section we shall create a Python file based on the standard template that solves the example business problem.
We shall edit the python file named application.py to contain the optimization problem. To begin the process, complete the following steps:
  1. Open Xpress Workbench.
  2. The landing page is displayed. Select CREATE INSIGHT (PYTHON) PROJECT.
  3. Select a folder in which to store the project.
    A new template project is created containing client_rescources, model_resources, and python_source folders, as well as an application.xml file.
  4. Click File > New > Python File. Save the new file in the python_source folder as application.py to overwrite the existing file.
  5. We shall start by importing all the libraries that will be used in this tutorial.
    import xpressinsight as xi
    import xpress as xp
    import pandas as pd
    import sys
  6. Below that block, enter the following:
    @xi.AppConfig(name="Portfolio Optimization", version=xi.AppVersion(1, 0, 0))
    class InsightApp(xi.AppBase):
        # Input entities
        MaxHighRisk: xi.Scalar(default=0.33, alias="Maximum investment into high-risk values")
        MaxPerShare: xi.Scalar(default=0.25, alias="Maximum investment per share")
        MinNorthAmerica: xi.Scalar(default=0.45, alias="Minimum investment into North-American values")
        ShareIds: xi.Index(dtype=xi.string, alias="Shares")
    This section declares the parameters that will be used as constraints when performing optimization. The specified default values can be changed for every model execution without any need to edit the model source.
  7. Below the constraints block, enter the following declarations:
    # Input and result entities indexed over ShareIds
    Shares: xi.DataFrame(index="ShareIds", columns=[
        xi.Column("Return", dtype=xi.real, format="$ 0.00", alias="Expected Return on Investment"),
        xi.Column("HighRisk", dtype=xi.boolean, alias="High-risk value"),
        xi.Column("NorthAmerica", dtype=xi.boolean, alias="Issued in North America"),
        xi.Column("fraction", dtype=xi.real, format="0.0%", alias="Fraction used", manage=xi.Manage.RESULT)
    ])    
    This section of code declares the entities that will be used to store the different data categories.
  8. Below the inputs block, enter the following code snippet:
    # Result entities
    TotalReturn: xi.Scalar(dtype=xi.real, alias="Total expected return on investment", manage=xi.Manage.RESULT)
    SummaryIds: xi.Index(dtype=xi.string, alias="Summary", manage=xi.Manage.RESULT)
    SummaryValues: xi.Series(index="SummaryIds", dtype=xi.real, manage=xi.Manage.RESULT)
    This section declares and names the data series that will be used to hold the result.
  9. The final declaration specifies the source data file.
    # Constant class attribute
        DATAFILE = "shares.csv"
  10. Below the result line, add the two required mode functions-These must be included in any Python Insight file.
    First declare the LOAD mode function.
    @xi.ExecModeLoad(descr="Load input data and initialize all input entities.")
        def load(self):
            print("Loading data.")
            self.Shares = pd.read_csv(InsightApp.DATAFILE, index_col=['ShareIds'], squeeze=True)
            self.ShareIds = self.Shares.index
            print("Loading finished.")
  11. The largest block of code is the RUN mode.
    @xi.ExecModeRun(descr="Solve problem and initialize all result entities.")
    def run(self):
        print('Starting optimization.')
    
        # Create Xpress problem and variables
        p = xp.problem("portfolio")
        self.Shares['fractionVar'] = pd.Series(xp.vars(self.ShareIds, vartype=xp.continuous, name='fractionVar'))
        p.addVariable(self.Shares.fractionVar)
    
        # Objective: expected total return
        objective = xp.Sum(self.Shares.Return * self.Shares.fractionVar)
        p.setObjective(objective, sense=xp.maximize)
    
        # Limit the percentage of high-risk values
        limit_high_risk = \
            xp.Sum(self.Shares.loc[self.Shares.HighRisk, 'fractionVar']) \
            <= self.MaxHighRisk
        p.addConstraint(limit_high_risk)
    
        # Minimum amount of North-American values
        limit_north_america = \
            xp.Sum(self.Shares.loc[self.Shares.NorthAmerica, 'fractionVar']) \
            >= self.MinNorthAmerica
        p.addConstraint(limit_north_america)
    
        # Spend all the capital
        p.addConstraint(xp.Sum(self.Shares.fractionVar) == 1)
    
        # Upper bounds on the investment per share
        for share_id, share in self.Shares.iterrows():
            p.addConstraint(share.fractionVar <= self.MaxPerShare)
    
        # Solve optimization problem
        p.solve()
    
        # Save results and key indicator values for GUI display
        self.Shares["fraction"] = pd.Series(p.getSolution(), index=self.ShareIds)
        self.TotalReturn = p.getObjVal()
    
        self.SummaryValues = pd.Series({
            "Expected total return": self.TotalReturn,
            "Total high risk shares": limit_high_risk.ub - p.getSlack(limit_high_risk),
            "Total North-American": limit_north_america.lb - p.getSlack(limit_north_america),
            "Largest position": self.Shares.fraction.max()
        })
        self.SummaryIds = self.SummaryValues.index
        print('\n', self.SummaryValues, '\n', sep='')
        print('Optimization finished.')
  12. Finally, at the end of the file, add the main class declaration that specifies which instructions Python should initially run.
    if __name__ == "__main__":
        app = xi.create_app(InsightApp)
        sys.exit(app.call_exec_modes(["LOAD", "RUN"]))
If you attempt to publish to Xpress Insight at this point, it will fail, because the compiler will recognize there is a missing file. We must add in the data file that is required by the Python script.
  1. Create a new file. In Xpress Workbench, click File > New > Blank File, or use the keyboard shortcut Ctrl-N. Name the new file shares.csv. Save the file to the model_resources folder.
  2. Double click shares.csv to open it in Xpress Workbench. Copy and paste the following text into the file, then save and close it.
    ShareIds,Return,HighRisk,NorthAmerica
    Bank,6.0,False,False
    Brewery,8.0,False,False
    Cars,7.0,False,False
    Electronics,21.0,True,False
    Hardware,17.0,True,True
    Highways,9.0,False,False
    Software,31.0,True,False
    Telecom,12.0,True,True
    Theater,26.0,True,True
    Treasury,5.0,False,True
  3. Now you can publish the application to Xpress Insight.

© 2001-2020 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.