Objective function in Python

14 views
Skip to first unread message

Phuc Nguyen

unread,
Dec 15, 2022, 9:29:06 AM12/15/22
to AMPL Modeling Language
Hi,

How can I write my objective function in Python language using predefined sets and parameter values? The param values are stored in matrices. The python program returns no value for me.

Ampl code:
from amplpy import AMPL, Environment, DataFrame
import numpy as np
ampl = AMPL(Environment(r'D:\Hello Dream 2020\FSS 2021\OPM 682\5. Software\ampl_mswin64'))
ampl.eval('option version;')
ampl.setOption('solver', 'knitro')
ampl.eval(r'''
# ---- CAOP / bilinear mixed 0-1 formulation ---- #
#### Define sets ####
set M;                      # Set of products
set N;                      # Set of customer segments
set K;                      # Set of resources that may constrain the assortment

#### Define parameters ####
param gamma{M};             # Probability that the demand originates from segment i
param rho{M,N};             # Unit revenue of product j from segment i
param v{M,N};               # Preference of product j to segment i
param v0{M};                # Preference of the no-purchase option to segment i
param Beta{K,N};            # Amount of resource k used by product j
param Capacity_Max := 5;    # Maximum capacity of the assortment

#### Define variables ####
var x{N} binary;            # Decision identifier to include product j into the assortment (=1 for yes) and (=0 for no)
var y{i in M} >= 0;

#### Define objective function ####
maximize Revenue: sum{i in M, j in N} gamma[i] * rho[i,j] * v[i,j] * y[i] * x[j];

#### Constraints ####
subject to Capacitated_Capacity {k in K}: sum{j in N} Beta[k,j] * x[j] <= Capacity_Max;
subject to MMNL_model_nonlinear {i in M}: y[i] = 1 / (v0[i] + sum{j in N} v[i,j] * x[j]);

##### Define problem #####
problem CAOP_Bilinear_Mixed_0_1: x, y, Revenue, Capacitated_Capacity, MMNL_model_nonlinear;
''')

customer_segment = 2
M = list(range(1, customer_segment + 1))

product = 10
N = list(range(1, product + 1))

capacity = 1
K = [capacity]

gamma_ = np.divide(1, customer_segment)
gamma_= np.full(len(M),gamma_)
gamma = {
    (customer_segment_i): (gamma_[i])
    for i, customer_segment_i in enumerate(M)
}

rho_ = np.random.uniform(1, 3, (len(M), len(N)))
rho = {
    (customer_segment_i, product_j): (rho_[i][j])
    for i, customer_segment_i in enumerate(M)
    for j, product_j in enumerate(N)
}

v_ = np.random.uniform(0, 1, (len(M), len(N)))
v = {
    (customer_segment_i, product_j): (v_[i][j])
    for i, customer_segment_i in enumerate(M)
    for j, product_j in enumerate(N)
}

v0_ = [5, 5] 
v0 = {
    (customer_segment_i): (v0_[i])
    for i, customer_segment_i in enumerate(M)
}


Beta_ = np.random.uniform(0, 1, (len(K), len(N)))
Beta = {
    (capacity_k, product_j): (Beta_[k][j])
    for k, capacity_k in enumerate(K)
    for j, product_j in enumerate(N)
}

Capacity_Max = 5

ampl.set['M'] = M
ampl.set['N'] = N
ampl.set['K'] = K
ampl.param['gamma'] = gamma
ampl.param['rho'] = rho
ampl.param['v'] = v
ampl.param['v0'] = v0
ampl.param['Beta'] = Beta
ampl.eval('display M;')
ampl.eval('display N;')
ampl.eval('display K;')
ampl.eval('display gamma;')
ampl.eval('display rho;')
ampl.eval('display v;')
ampl.eval('display v0;')
ampl.eval('display Beta;')

Python code:
def f(CAOP_bilinear):
    dim = len(CAOP_bilinear)
    OF = 0
    x = 1
    for i in range(0, M):
        for j in range(0, N):
            t1 = 0
            t1 += gamma[i] * rho[i] * v[i] * y[i] * x[i]
            OF += t1
            return OF
    print(OF) 
Reply all
Reply to author
Forward
0 new messages