# [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?

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