Objective function in Python

79 views
Skip to first unread message

Phuc Nguyen

unread,
Dec 15, 2022, 9:32:17 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) 

AMPL Google Group

unread,
Dec 16, 2022, 5:58:51 AM12/16/22
to AMPL Modeling Language
The value for the objective is zero before solving since all variables are zero at that point. You need to invoke ampl.solve() before you check the objective value:

ampl.solve()
ampl.eval("display Revenue;")

This will produce the following output:

Knitro 13.2.0: Locally optimal or satisfactory solution.
objective 0.9145105816; optimality gap 0
1 nodes; 1 subproblem solves

suffix feaserror OUT;
suffix opterror OUT;
suffix numfcevals OUT;
suffix numiters OUT;
suffix incumbent OUT;
suffix relaxbnd OUT;
Revenue = 0.914511


--
Filipe Brandão
am...@googlegroups.com
{#HS:2098669587-113260#}

Phuc Nguyen

unread,
Dec 19, 2022, 2:56:15 AM12/19/22
to am...@googlegroups.com
Hey Filipe,

I managed to get it running. Thanks for your help!

Have a nice day.

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/U5w24rnwAEM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-2098669587-6244736235-1671188328-1521994610%40helpscout.net.


--
Best regards,

Phuc Nguyen
 
Tel: +84 932 673 939 (Whatsapp available)
Reply all
Reply to author
Forward
0 new messages