Initializing help system before first use

Basic LP tasks: problem statement and solving; solution analysis


Type: Production planning
Rating: 1 (simple)
Description: Small, introductory problems to modeling with Python.
File(s): chess.py, chess2.py


chess.py
'''*******************************************************
  * Python Example Problems                             *
  *                                                     *
  * file chess.py                                       *
  * Example for the use of the Python language          *
  * (Small LP-problem)                                  *
  *                                                     *
  * (c) 2018-2024 Fair Isaac Corporation                *
  *******************************************************'''

import xpress as xp

p = xp.problem()

small = p.addVariable()
large = p.addVariable()

# Now we have the constraints

p.addConstraint(3*small + 2*large <= 400)  # limit on available machine time
p.addConstraint(small + 3*large <= 200)  # limit on available wood

# Define the objective function
p.setObjective(5*small + 20*large, sense=xp.maximize)

p.optimize()

print('')
print("Here are the LP results")
print("Objective value is ", p.attributes.objval)
print("Make ", p.getSolution(small), " small sets, and ",
      p.getSolution(large), " large sets")

p.chgcoltype([small, large], ['I', 'I'])

p.optimize()

print('')
print("Here are the IP results")
print("Objective value is ", p.attributes.objval)
print("Make ", p.getSolution(small), " small sets, and ",
      p.getSolution(large), " large sets")

chess2.py
'''*******************************************************
  * Python Example Problems                             *
  *                                                     *
  * file chess2.py                                      *
  * Example for the use of the Python language          *
  * (Small LP-problem)                                  *
  *                                                     *
  * (c) 2018-2024 Fair Isaac Corporation                *
  *******************************************************'''

import xpress as xp

DescrV = {}
DescrC = {}

p = xp.problem()

xs = p.addVariable()
xl = p.addVariable()

mc_time = 3*xs + 2*xl <= 400  # Limit on available machine time
wood = xs + 3*xl <= 200  # Limit on available wood

# Define the variable and constraint descriptions. Since the arrays
# and the indexing sets are dynamic they grow with each new variable
# description added:
DescrV = {xs: " Number of small chess sets",
          xl: " Number of large chess sets"}

DescrC = {mc_time: " Limit on available machine time",
          wood: " Limit on available wood"}

p.addConstraint(mc_time, wood)

# Define the objective function
p.setObjective(5*xs + 20*xl, sense=xp.maximize)

p.optimize()

rhs = []
p.getrhs(rhs, 0, p.attributes.rows - 1)

# Print out the solution
print("Solution:\n Objective: ", p.attributes.objval)
print(DescrV[xs], ":", p.getSolution(xs), ",",
      DescrV[xl], ":", p.getSolution(xl))

print(" Constraint activity:")
print(DescrC[mc_time], ": ", rhs[p.getIndex(mc_time)] - p.getSlacks(mc_time), "\n",
      DescrC[wood],    ": ", rhs[p.getIndex(wood)]    - p.getSlacks(wood), sep='')

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