FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home

Maximize the sum of logistic curves subject to linear and piecewise linear constraints

Description
Approximate the logistic curves using piecewise linear functions.

Further explanation of this example: 'Xpress Python Reference Manual'

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.

maxSumLogistic.py

'''
Maximize the sum of logistic curves subject to linear and piecewise linear constraints
Approximate the logistic curves using piecewise linear functions
(c) 2020-2024 Fair Isaac Corporation
'''
import numpy as np
import xpress as xp
import matplotlib.pyplot as plt

def logistic(x, K, r, c):
return K / (1 + np.exp(-r * (x - c)))

n_curves = 10
N = range(n_curves)
U = 10  # upper bound of the variables

# Create a problem and add these two vectors
p = xp.problem()

# Create two numpy vectors of variables

n_intervals = 100
# define the breakpoints of the piecewise linear terms
breakpoints = np.array([(U / n_intervals) * i for i in range(n_intervals + 1)])

# compute the function values at breakpoints
y_vals = [logistic(breakpoints, U, np.random.uniform(0.5, 3), U / 2) for _ in N]

# Enable to visualize curves
for i in N:
plt.plot(breakpoints, y_vals[i])

y_vals = np.array(y_vals).flatten().tolist()
x_vals = np.array([])
for i in N:
x_vals = np.concatenate((x_vals, breakpoints))
x_vals = x_vals.tolist()

# Set the starting indices for the flattened piecewise linear function definitions
start = [i * (n_intervals + 1) for i in N]