[AMPL 24439] AMPLPY dictionary

Hi,

I am using amplpy and having an issue when I try to have the prices of product j the same across all segment i.

To generate the prices, I have written:

rho_ = np.random.uniform(min_rev,
max_rev,
(segments, products))
rho = {
(i, j): (rho_[i][j])
for i in range(segments)
for j in range(products)

When I evaluate rho, I get the following tables :

rho [,] (tr)
: 0 1 :=
0 1.35228 1.56474
1 1.69702 2.47681
2 1.74028 2.30496
3 2.70224 1.471
4 1.781 2.93989
5 2.66161 2.58177

The column index denotes product j and the row index denotes customer segment i. The rho values (prices) for product j are different across customer segments.

How can I modify my modes so that the rho values (prices) are the same across all customer segments?

Would appreciate any guidance. Happy to provide more information as needed.

Thanks,
Phuc

You are generating a matrix of random values with a random value for each pair (segment, product). If you want all products to share the same random value across all segments you can generate just a vector of random values for the products as follows:

from amplpy import AMPL, Environment
import numpy as np
ampl = AMPL(Environment(“”, “x-ampl”))
ampl.eval(“”"
reset;
param n_segments;
param n_products;
set SEGMENTS := 0…n_segments-1;
set PRODUCTS := 0…n_products-1;
param rho{SEGMENTS, PRODUCTS};
“”")
segments = 5
products = 2
min_rev, max_rev = 10, 100
rho_ = np.random.uniform(min_rev, max_rev, (1, products))
rho = {
(i, j): (rho_[0][j])
for i in range(segments)
for j in range(products)
}
ampl.param[“n_segments”] = segments
ampl.param[“n_products”] = products
ampl.param[“rho”] = rho
ampl.eval(“display rho;”)

This produces the following output:

rho :=
0 0 55.511
0 1 92.8638
1 0 55.511
1 1 92.8638
2 0 55.511
2 1 92.8638
3 0 55.511
3 1 92.8638
4 0 55.511
4 1 92.8638
;

Also, in case the values will always be shared across all segments you can defined the parameter rho in function of another parameter with the values for each product as follows:

param rho_products{PRODUCTS};
param rho{s in SEGMENTS, p in PRODUCTS} := rho_products[p];

With amplpy you can then load the values as follows:

from amplpy import AMPL
import numpy as np
ampl = AMPL()
ampl.eval(“”"
reset;
param n_segments;
param n_products;
set SEGMENTS := 0…n_segments-1;
set PRODUCTS := 0…n_products-1;
param rho_products{PRODUCTS};
param rho{s in SEGMENTS, p in PRODUCTS} := rho_products[p];
“”")
segments, products = 5, 2
min_rev, max_rev = 10, 100
ampl.param[“n_segments”] = segments
ampl.param[“n_products”] = products
ampl.param[“rho_products”] = np.random.uniform(min_rev, max_rev, products)
ampl.eval(“display rho;”)

It produces the following output:

rho :=
0 0 17.9317
0 1 38.8254
1 0 17.9317
1 1 38.8254
2 0 17.9317
2 1 38.8254
3 0 17.9317
3 1 38.8254
4 0 17.9317
4 1 38.8254
;

1 Like