Declare a 3-dimension variable or parameter in AMPL

7,583 views
Skip to first unread message

Terry

unread,
Mar 8, 2010, 9:03:58 AM3/8/10
to AMPL Modeling Language
Hi! Everyone,

I want to know, in AMPL, can it possibly declare a variable or
parameter with 3 dimension?
For example, I have a variable defined as:

Q(ij,t): the product quantity carried by the vehicle from customer i
to customer j in day t.

In the model, I only declare:
set Locations = 1..15 ;
.
.
param Q {Locations, Locations} >= 0;

so how to declare Q{i, j} with t together and how to specify the
data(3 dimension, not a matrix any more)??
Thanks a lot for your help!

Terry Zhong

Paul

unread,
Mar 8, 2010, 11:02:07 AM3/8/10
to AMPL Modeling Language

set Locations = 1..15 ;
set Time = 1..10; # or whatever your range of time indices is
param Q {Locations, Locations, Time} >= 0;

Terry

unread,
Mar 8, 2010, 11:20:37 AM3/8/10
to AMPL Modeling Language
Ok, thanks!
so the data set is like 'truple' set? Or could you specify a simple
data example?
thanks a lot!

Terry

Robert Fourer

unread,
Mar 8, 2010, 3:17:25 PM3/8/10
to am...@googlegroups.com, Terry
See the figures 4-1 and 4-2 of the AMPL book for an example of a model that
uses a 3-dimensional parameter -- named "cost" in this case -- and a data
file that specifies values for that parameter. I have copied them below,
and they're also at www.ampl.com/BOOK/EXAMPLES/EXAMPLES2/index_figs.html.

Bob Fourer
4...@ampl.com


### multi.mod ###

set ORIG; # origins
set DEST; # destinations
set PROD; # products

param supply {ORIG,PROD} >= 0; # amounts available at origins
param demand {DEST,PROD} >= 0; # amounts required at destinations

check {p in PROD}:
sum {i in ORIG} supply[i,p] = sum {j in DEST} demand[j,p];

param limit {ORIG,DEST} >= 0;

param cost {ORIG,DEST,PROD} >= 0; # shipment costs per unit
var Trans {ORIG,DEST,PROD} >= 0; # units to be shipped

minimize Total_Cost:
sum {i in ORIG, j in DEST, p in PROD}
cost[i,j,p] * Trans[i,j,p];

subject to Supply {i in ORIG, p in PROD}:
sum {j in DEST} Trans[i,j,p] = supply[i,p];

subject to Demand {j in DEST, p in PROD}:
sum {i in ORIG} Trans[i,j,p] = demand[j,p];

subject to Multi {i in ORIG, j in DEST}:
sum {p in PROD} Trans[i,j,p] <= limit[i,j];


### multi.dat ###

set ORIG := GARY CLEV PITT ;
set DEST := FRA DET LAN WIN STL FRE LAF ;
set PROD := bands coils plate ;

param supply (tr): GARY CLEV PITT :=
bands 400 700 800
coils 800 1600 1800
plate 200 300 300 ;

param demand (tr):
FRA DET LAN WIN STL FRE LAF :=
bands 300 300 100 75 650 225 250
coils 500 750 400 250 950 850 500
plate 100 100 0 50 200 100 250 ;

param limit default 625 ;

param cost :=

[*,*,bands]: FRA DET LAN WIN STL FRE LAF :=
GARY 30 10 8 10 11 71 6
CLEV 22 7 10 7 21 82 13
PITT 19 11 12 10 25 83 15

[*,*,coils]: FRA DET LAN WIN STL FRE LAF :=
GARY 39 14 11 14 16 82 8
CLEV 27 9 12 9 26 95 17
PITT 24 14 17 13 28 99 20

[*,*,plate]: FRA DET LAN WIN STL FRE LAF :=
GARY 41 15 12 16 17 86 8
CLEV 29 9 13 9 28 99 18
PITT 26 14 17 13 31 104 20 ;

Terry

unread,
Mar 9, 2010, 6:18:33 AM3/9/10
to AMPL Modeling Language
thank you very much!

Terry

behnous...@gmail.com

unread,
Oct 26, 2017, 12:16:33 PM10/26/17
to AMPL Modeling Language
HI everyone, 

is it possible someone help me to solve the following question?

The cost from the plants to the DCs (costs) as well as the cost from the DC to the regions (costr) are presented in a 3-dimensional parameter. That is, param costs{PLANTS, DCS, PRODUCTS) >= 0; The access to elements is similar to the 2-dimensional with costs[i,j,k].

param costs := [*,*,P1]: A B :=

C65 D47 M69

The code-snippet shows the cost for product 1 from the plants C, D, and M to the DC A and B.

Otherwise, the following constraints are given:

  • Supply per plant per product is limited according to “supply”

  • Minimum demand per region is given in “demand”

  • All incoming orders to the DC are fully forwarded to the regions

  • The DCs have a capacity

  • Even though there is a capacity for the supply per plant and product, there is also an

    overall max supply per plant (maxsupply)
    The overall goal is to minimise the cost of transport. Note that there are no fixcosts.

    Hints: Even though there might be other solutions, I am looking for the objective function, the variable and parameter definitions, and constraints (5 in my solution).

    Hint 2: Objective function is 6150, the transport quantities for B (DC) to NY (region) for product P3 is 25, C to A for P1 = 0, M to A for P3 150. 


Robert Fourer

unread,
Oct 27, 2017, 7:05:21 PM10/27/17
to am...@googlegroups.com
Normally for this situation you would define

param costs {PLANTS,DCS} >= 0;
param costr {DCS,PRODUCTS} >= 0;

where costs[i,j] represents cost from plant i to DC j, and costr[j,k] represents cost from DC j to product k. If you have a question about a way to represent all of these costs in a single 3-dimensional parameter "costs", then I suggest posting all of the data for costs and also an explanation of the meaning that you have in mind for "costs[i,j,k]".

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of behnous...@gmail.com
Sent: Thursday, October 26, 2017 8:47 AM
To: AMPL Modeling Language
Subject: [AMPL 14963] Re: Declare a 3-dimension variable or parameter in AMPL

is it possible someone help me to solve the following question?

The cost from the plants to the DCs (costs) as well as the cost from the DC to the regions (costr) are presented in a 3-dimensional parameter. That is, param costs{PLANTS, DCS, PRODUCTS) >= 0; The access to elements is similar to the 2-dimensional with costs[i,j,k].

param costs := [*,*,P1]: A B :=
C65 D47 M69

The code-snippet shows the cost for product 1 from the plants C, D, and M to the DC A and B.
Otherwise, the following constraints are given: ...


behnous...@gmail.com

unread,
Oct 28, 2017, 10:21:37 AM10/28/17
to AMPL Modeling Language

I just have the question and data ample which is:

The cost from the plants to the DCs (costs) as well as the cost from the DC to the regions (costr) are presented in a 3-dimensional parameter. That is, param costs{PLANTS, DCS, PRODUCTS) >= 0; The access to elements is similar to the 2-dimensional with costs[i,j,k].

param costs := [*,*,P1]: A B :=

C65 D47 M69

The code-snippet shows the cost for product 1 from the plants C, D, and M to the DC A and B.

Otherwise, the following constraints are given:

  • Supply per plant per product is limited according to “supply”

  • Minimum demand per region is given in “demand”

  • All incoming orders to the DC are fully forwarded to the regions

  • The DCs have a capacity

  • Even though there is a capacity for the supply per plant and product, there is also an

    overall max supply per plant (maxsupply)
    The overall goal is to minimise the cost of transport. Note that there are no fixcosts.

    Hints: Even though there might be other solutions, I am looking for the objective function, the variable and parameter definitions, and constraints (5 in my solution).

    Hint 2: Objective function is 6150, the transport quantities for B (DC) to NY (region) for product P3 is 25, C to A for P1 = 0, M to A for P3 150. 

    then according the data we have,I am trying mod file for it

     param maxsupply :=

    C 10000

    D 250

    M 10000;


     

     param costs :=

     [*,*,P1]:  A B :=

      C 6 5

      D 4 7 

      M 6 9

      

     [*,*,P2]:  A B :=

      C 6 5

      D 4 7 

      M 6 9

      

      [*,*,P3]:  A B :=

      C 6 5

      D 4 7 

      M 4 7


     param costr :=

     [*,*,P1]:  A B :=

      NY 8 9 

      VA 5 7

      PA 6 6

        

     [*,*,P2]:  A B :=

      NY 7 3 

      VA 8 8 

      PA 5 6

      

      [*,*,P3]:  A B :=

      NY 7 4 

      VA 4 5 

      PA 4 4

      ; 


    Robert Fourer

    unread,
    Oct 30, 2017, 9:20:09 AM10/30/17
    to am...@googlegroups.com
    Perhaps what you have in mind is

    param costs {PLANTS, DCS, PRODUCTS} >= 0;
    param costr {DCS, REGIONS, PRODUCTS} >= 0;

    But then your tables for costr are transposed, because they have regions on the left and DCs on the top. To tell AMPL that they are transposed, you need to add "(tr)" to each table like this:

    param costr :=
    [*,*,P1] (tr): A B :=
    NY 8 9
    VA 5 7
    PA 6 6

    [*,*,P2] (tr): A B :=
    NY 7 3
    VA 8 8
    PA 5 6

    [*,*,P3] (tr): A B :=
    C 6 5
    D 4 7
    M 4 7
    ;

    Bob Fourer
    am...@googlegroups.com

    =======

    From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of behnous...@gmail.com
    Sent: Friday, October 27, 2017 7:25 PM
    To: AMPL Modeling Language
    Subject: [AMPL 14974] Re: Declare a 3-dimension variable or parameter in AMPL

    I just have the question and data ample which is:
    The cost from the plants to the DCs (costs) as well as the cost from the DC to the regions (costr) are presented in a 3-dimensional parameter. That is, param costs{PLANTS, DCS, PRODUCTS) >= 0; The access to elements is similar to the 2-dimensional with costs[i,j,k].
    param costs := [*,*,P1]: A B :=
    C65 D47 M69
    The code-snippet shows the cost for product 1 from the plants C, D, and M to the DC A and B.

    ...

    ledez...@gmail.com

    unread,
    Dec 13, 2018, 10:21:23 PM12/13/18
    to AMPL Modeling Language
    excuse me . I know that this group publication was a long time ago

    But I'm writing my final project to graduate from college and I'm faced with a big problem. I do not know how to put the data in ampl. I hope you can help me.
    I am solving the Chinese postman with 3 dimiensiones:
    origin (i)
    destination (j)
    truck  (k)

    then write the mathematical model a small model to give an example and what I have written in ampl. I hope to explain myself well, my English is not the best

    param 

     Cost    ( Cij)
    produccion (D ij)
    penalizacion (direccion)  (Pij)
    Capacity truck (CAP k )

    var X ijk
    var  Y ijk Binary

    min     ∑ijk   C ij * X ijk +  ijk   Pij * X ijk -  ij∑k Y ijk

    restriccion 1   " flow restriction so that the truck does not stay in any arc " 

                                                 ∑j  X ijk +  j   X jik = 0

    restriccion 2  " One-way arcs have to be visited at least once " 

                                             ∑k  Xijk  >= 1     
                                                                                
    restriccion 3 "Two-way arcs have to be visited at least once"

                                          ∑k  Xijk   + ∑k  Xjik >= 1                                          

    restriccion 4 " Variables x have to be greater than variables and binary" 

                                Xijk  >=  Yijk       ꓯ i j k                                                             

    restriccion 5  "the production of garbage from the arches (Dijk) by the binary variable has to be lower than the capacity of the truck"

                                          ij  D ijk  *  Y ijk ≤  CAP k 



    restriccion 6"all trucks depart from node 1 "

        X1jk = 1  k E K 


    these are the data of an example I did in excel. it worked but the solver is not good that's why I need to program in ampl to add an extensive data base


    .mod

    set origen;
    set destino; 
    set truck;
    set ruta within {origen, destino, truck};

    param costs {ruta};
    param produccion {ruta};
    param penalizacion {ruta};

    var x 

    .dat 

    set origen := 1 2 3 4 5 
    set destino := 1 2 3 4 5 
    set truck := 1 2 
    set ruta :=
    1 2 1
    1 5 1
    4 5 1
    5 3 1
    1 4 1
    2 4 1
    2 5 1
    3 4 1
    1 2 2
    1 5 2
    4 5 2
    5 3 2
    1 4 2
    2 4 2
    2 5 2
    3 4 2 ;

    param costs:=
    1 2 1 10
    1 5 1 15
    4 5 1 15
    5 3 1 5
    1 4 1 40
    2 4 1 20
    2 5 1 8
    3 4 1 15
    1 2 2 10
    1 5 2 15
    4 5 2 15
    5 3 2 5
    1 4 2 40
    2 4 2 20
    2 5 2 8
    3 4 2 15 ; 

    param penalizacion :=
    1 2 1 1
    1 5 1 1
    4 5 1 1
    5 3 1 1
    1 4 1 2
    2 4 1 2
    2 5 1 2
    3 4 1 2
    1 2 2 1
    1 5 2 1
    4 5 2 1
    5 3 2 1
    1 4 2 2
    2 4 2 2
    2 5 2 2
    3 4 2 2 ;

    param produccion :=
    1 2 1 20
    1 5 1 30
    4 5 1 30
    5 3 1 10
    1 4 1 120
    2 4 1 40
    2 5 1 16
    3 4 1 30
    1 2 2 20
    1 5 2 30
    4 5 2 30
    5 3 2 10
    1 4 2 120
    2 4 2 40
    2 5 2 16
    3 4 2 30 ; 


    explained this, now my doubts, sorry I know that this is very long.

    I do not know how to define the variables if I have to put X (ijk) X (path) nose how to define my objective function to minimize or the restriction.

    I hope you can help me because I really have no idea how to do it.

    I thank you in advance

    AMPL Google Group

    unread,
    Dec 14, 2018, 6:55:03 PM12/14/18
    to Ampl Modeling Language
    Please find the attached script demonstrating the objective and constraint for your problem. I have written an objective and a constraint based on your description. You should take the script as a guideline and modify it based on your need. You need to add constraints in the script to model your problem. I would recommend you to read https://ampl.com/BOOK/CHAPTERS/18-network.pdf chapter as that will help you to understand the network flow problem in AMPL.

    --
    Dr. Paras Tiwari
    am...@googlegroups.com
    {#HS:731717494-31694#}
    --
    You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
    To post to this group, send email to am...@googlegroups.com.
    Visit this group at https://groups.google.com/group/ampl.
    For more options, visit https://groups.google.com/d/optout.



    demo.dat
    demo.mod

    ledez...@gmail.com

    unread,
    Dec 26, 2018, 6:42:33 PM12/26/18
    to AMPL Modeling Language
    Hi Dr Paras Tiwari:

    Thank you very much for your help, it was very useful for me.
    Now I have another problem, I was wondering if you would know something about it.

    given the equations I showed above. I solve it with the solver gurobi.
    but this solver give resulte and creates subtour, you know some restriction to eliminate subtour that is for the Chinese postman problem

    Thank you in advance for your help

    Osven Ledezma
    Reply all
    Reply to author
    Forward
    0 new messages