Availability of tuple (data structure) in AMPL

34 views
Skip to first unread message

Udoy Paul

unread,
May 17, 2018, 8:57:12 PM5/17/18
to AMPL Modeling Language
Hi,
Greetings.
Until the last couple of days, I was more involved in using OPL (CPLEX). It offers data structures like "tuple". I am new to AMPL. I am wondering if there is any data structure like tuple available in AMPL.

Example code in OPL: (It would be great if you please let me know if there exist any similar way to code in AMPL)

int T=...;
range hour=1..T;

 tuple unitparameter{
 key int unitnumber;
 int buslocation;
 float Pmin;
 float Pmax;
 float cost;
 }

  tuple branch {
  key int branchnumber;
  int fromnode;
  int tonode;
  float maxflow;
  };

   {unitparameter} units=...;
   {branch} branches=...;
   {int} nodeG=...;
   {int} nodeNG=...;

float localdemandG[nodeG][hour] = ...;
float localdemandNG[nodeNG][hour] = ...;

float alphaG[branches][nodeG] = ...;
float alphaNG[branches][nodeNG] = ...;

dvar float flow[branches][hour];
dvar float+ p[units][hour] in 0..3000;
dvar boolean x[units][hour];

minimize sum (t in hour)(sum (i in units)(p[i][t])*i.cost);

subject to{
//capacity
forall(i in units,t in hour){
i.Pmax*x[i][t]>=p[i][t];
i.Pmin*x[i][t]<=p[i][t];
};
}

Thank you so much for your time.
Best,
Udoy Paul

AMPL Google Group

unread,
May 18, 2018, 12:42:12 PM5/18/18
to Ampl Modeling Language
You can use compound sets. You can read about it at https://ampl.com/BOOK/CHAPTERS/09-sets2.pdf (pp 96).

--
Paras Tiwari
am...@googlegroups.com
{#HS:583181609-8003#}
--
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.



AMPL Google Group

unread,
May 18, 2018, 1:41:36 PM5/18/18
to Ampl Modeling Language
AMPL does not have any construct like the OPL's tuples. However there is often a simple way to say the same thing in AMPL. As an example, for the OPL objective and constraints that you show, an AMPL definition could be as follows:

param T > 0;
set HOURS := 1 .. T;

set UNITS;
param cost {UNITS};
param Pmin {UNITS};
param Pmax {UNITS};

var p {UNITS, HOURS} >= 0, <= 3000;
var x {UNITS, HOURS} binary;

minimize TotalCost: sum {i in UNITS, t in HOURS} cost[i] * p[i,t];

subject to CapacityMin {i in UNITS, t in HOURS}:
Pmin[i] * x[i,t] <= p[i,t];

subject to CapacityMax {i in UNITS, t in HOURS}:
Pmax[i] * x[i,t] >= p[i,t];

Other uses of tuples in OPL may require sets of ordered pairs, triples, etc. in AMPL; for example a set of network arcs would be defined as a set of pairs of nodes. These are what AMPL calls "sets of tuples" and they are described in the reference given by Paras.

--
Robert Fourer
am...@googlegroups.com
{#HS:583181609-8003#}
On Fri, May 18, 2018 at 4:41 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You can use compound sets. You can read about it at https://ampl.com/BOOK/CHAPTERS/09-sets2.pdf (pp 96).

--
Paras Tiwari
am...@googlegroups.com


On Fri, May 18, 2018 at 12:57 AM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:

Udoy Paul

unread,
May 18, 2018, 2:03:25 PM5/18/18
to am...@googlegroups.com
Dear Dr. Fourer,
It was great to hear from you. I have started following your book. 
In my previous code, for simplicity, I did not include two more constraints that I have to deal with. With the level of knowledge I have right now, it seems there must be some trick to deal with the constraints. 

the complete code in OPL is :

//Demand
forall (t in hour){
sum (nodenumG in nodeG)(sum (i in units: i.buslocation==nodenumG)(p[i][t])-localdemandG[nodenumG][t])+
sum (nodenumNG in nodeNG)(-localdemandNG[nodenumNG][t])==0;
};

//Powerflow
forall( l in branches, t in hour){

flow[l][t]== sum (nodenumG in nodeG)(alphaG[l][nodenumG]*(sum (i in units: i.buslocation==nodenumG)(p[i][t])-localdemandG[nodenumG][t]))+
sum (nodenumNG in nodeNG)(alphaNG[l][nodenumNG]*(-localdemandNG[nodenumNG][t]));
}

}//end of subject to

I would like to thank Dr. Tiwari as well for his help so far. Thank you again for your time. 

Best Regards, 
Udoy Paul



To unsubscribe from this group and stop receiving emails from it, send an email to ampl+unsubscribe@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.



--
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+unsubscribe@googlegroups.com.

AMPL Google Group

unread,
May 19, 2018, 11:53:25 AM5/19/18
to Ampl Modeling Language
If you have an algebraic statement of your problem then it might be easier to go from that to AMPL than to translate the OPL representation to AMPL. Working from the algebraic statements (10) and (11), a directly equivalent AMPL formulation would be like this:

param I integer > 0;
param J {i in 1..I} integer > 0;
param L integer > 0;
param T integer > 0;

param demand {1..I, 1..T};
param flow {1..L, 1..T};
param shift {1..I, 1..L};

var pk {i in 1..I, 1..J[i], 1..T} >= 0;

subject to SystemDemand {t in 1..T}:
   sum {i in 1..I} (sum {j in 1..J[i]} pk[i,j,t] - demand[i,t]) = 0;

subject to PowerFlow {l in 1..L, t in 1..T}:
   flow[l,t] = sum {i in 1..I} shift[i,l] * 
      (sum {j in 1..J[i]} pk[i,j,t] - demand[i,t]);

If you want to have a name for a set of integers then you can define, for example, set HOURS = 1..T; as in my previous example.

--
Robert Fourer
am...@googlegroups.com
{#HS:583181609-8003#}
On Fri, May 18, 2018 at 6:04 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Dear Dr. Fourer,
It was great to hear from you. I have started following your book.
In my previous code, for simplicity, I did not include two more constraints that I have to deal with. With the level of knowledge I have right now, it seems there must be some trick to deal with the constraints.
ii_jhc9m47s0_1637464e99c8cbea
minimize sum (t in hour)(sum (i in units)(p[t])*i.cost);


subject to{
//capacity
forall(i in units,t in hour){
i.Pmax*x[t]>=p[t];
i.Pmin*x[t]<=p[t];

};

//Demand
forall (t in hour){
sum (nodenumG in nodeG)(sum (i in units: i.buslocation==nodenumG)(p[t])-localdemandG[nodenumG][t])+

sum (nodenumNG in nodeNG)(-localdemandNG[nodenumNG][t])==0;
};

//Powerflow
forall( l in branches, t in hour){

flow[l][t]== sum (nodenumG in nodeG)(alphaG[l][nodenumG]*(sum (i in units: i.buslocation==nodenumG)(p[t])-localdemandG[nodenumG][t]))+

sum (nodenumNG in nodeNG)(alphaNG[l][nodenumNG]*(-localdemandNG[nodenumNG][t]));
}

}//end of subject to

I would like to thank Dr. Tiwari as well for his help so far. Thank you again for your time.

Best Regards,
Udoy Paul
On Fri, May 18, 2018 at 5:41 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
AMPL does not have any construct like the OPL's tuples. However there is often a simple way to say the same thing in AMPL. As an example, for the OPL objective and constraints that you show, an AMPL definition could be as follows:

param T > 0;
set HOURS := 1 .. T;

set UNITS;
param cost {UNITS};
param Pmin {UNITS};
param Pmax {UNITS};

var p {UNITS, HOURS} >= 0, <= 3000;
var x {UNITS, HOURS} binary;

minimize TotalCost: sum {i in UNITS, t in HOURS} cost[i] * p[i,t];

subject to CapacityMin {i in UNITS, t in HOURS}:
Pmin[i] * x[i,t] <= p[i,t];

subject to CapacityMax {i in UNITS, t in HOURS}:
Pmax[i] * x[i,t] >= p[i,t];

Other uses of tuples in OPL may require sets of ordered pairs, triples, etc. in AMPL; for example a set of network arcs would be defined as a set of pairs of nodes. These are what AMPL calls "sets of tuples" and they are described in the reference given by Paras.

--
Robert Fourer
am...@googlegroups.com


To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages