Pyomo Tutorial: Index Sets David L. Woodruff Graduate School of Management University of California, Davis Davis, CA 95616-8609 USA [email protected]

1

Introduction

• Non-Users: In its simplest form, what does Pyomo look like?

• New Users: What’s the deal with index sets?

• Experienced users: How can I help new users with index sets?

2

Min Cost Flow

• Set: Nodes ≡ N • Set: Arcs ≡ A ⊆ N × N

• Var: Flow on arc (i, j): ≡ xi,j , (i, j) ∈ A

• Param: Flow Cost on arc (i, j): ≡ ci,j , (i, j) ∈ A • Param: Demand at node i: ≡ Di, i ∈ N • Param: Supply at node i: ≡ Si, i ∈ N P minimize ca x a a∈AP subject to: Sn + (i,n)∈A x(i,n) P −Dn − (n,j)∈A x(n,j) n ∈ N xa ≥ 0, a∈A

3

A=N ×N

# singlecomm.py from coopr.pyomo import * model = AbstractModel() model.Nodes = Set() model.Arcs = model.Nodes * model.Nodes

model.Flow = Var(model.Arcs, domain=NonNegativeReals) model.FlowCost = Param(model.Arcs, default = 0.0) model.Demand = Param(model.Nodes) model.Supply = Param(model.Nodes) def Obj_rule(model): return summation(model.FlowCost, model.Flow) model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(model, node): return model.Supply[node] \ + sum(model.Flow[i, node] for i in model.Nodes) \ - model.Demand[node] \ - sum(model.Flow[node, j] for j in model.Nodes) \ == 0 model.FlowBalance = Constraint(model.Nodes, \ rule=FlowBalance_rule)

4

The Same Only Different

# d2singlecomm.py - double indexes instead of an Arc # equivalent to singlecomm.py from coopr.pyomo import * model = AbstractModel() model.Nodes = Set() model.Flow = Var(model.Nodes, model.Nodes, \ domain=NonNegativeReals) model.FlowCost = Param(model.Nodes, model.Nodes) model.Demand = Param(model.Nodes) model.Supply = Param(model.Nodes) def Obj_rule(model): return summation(model.FlowCost, model.Flow) model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(model, node): return model.Supply[node] \ + sum(model.Flow[i, node] for i in model.Nodes) \ - model.Demand[node] \ - sum(model.Flow[node, j] for j in model.Nodes) \ == 0 model.FlowBalance = Constraint(model.Nodes, rule=Flow

5

Not So Dense?

You could use model.Arcs = Set(within=model.Nodes*model.Nodes) or mode.Arcs = Set(dimen=2) and then

def FlowBalance_rule(model, node): return model.Supply[node] \ + sum(model.Flow[i, node] for i in model.Nodes \ if (i,node) in model.Arcs) \ - model.Demand[node] \ - sum(model.Flow[node, j] for j in model.Nodes \ if (j,node) in model.Arcs) \ == 0 to match P Sn + (i,n)∈A x(i,n) P −Dn − (n,j)∈A x(n,j) n∈N

6

Let’s Do It My Way

I like

def FlowBalance_rule(model, nd): return model.Supply[nd] \ + sum(model.Flow[i, nd] for i in model.NodesIn[nd]) - model.Demand[nd] \ - sum(model.Flow[nd, j] for j in model.NodesOut[nd] == 0 to get it you need NodesIn and NodesOut, which you could just read from data, and maybe you should;

7

however,

you could get them from the arcs as follows:

def NodesIn_init(model, node): retval = [] for (i,j) in model.Arcs: if j == node: retval.append(i) return retval model.NodesIn = Set(model.Nodes, initialize=NodesIn_i

8

Oh, Do You Really Like Numbers?

model.NumNodes = Param(within=PositiveIntegers) model.Nodes = RangeSet(model.NumNodes)

9

From “Getting Started with Coopr”

You want for i in model.I, k in model.K, v in model.V[k] One way to get it:

def kv_init(model): return ((k,v) for k in model.K for v in model.V[k]) model.KV=Set(dimen=2, initialize=kv_init)

10

Illustration

from coopr.pyomo import * model = AbstractModel() model.I=Set() model.K=Set() model.V=Set(model.K)

def kv_init(model): return ((k,v) for k in model.K for v in model.V[k model.KV=Set(dimen=2, initialize=kv_init) model.a = Param(model.I, model.K) model.y = Var(model.I) model.x = Var(model.I, model.KV) #include a constraint #x[i,k,v] <= a[i,k]*y[i], # for i in model.I, k in model.K, v in model.V[k] def c1Rule(model,i,k,v): return model.x[i,k,v] <= model.a[i,k]*model.y[i] model.c1 = Constraint(model.I,model.KV,rule=c1Rule)

11

Summary

• You can have any number of indexes, each with any number of dimensions.

• If the index sets are sparse, it usually seems best to me to form higher dimensional sets to serve as the index sets, rather than using multiple indexes.

12

Questions or Comments?

13

Pyomo Tutorial: Index Sets -

Pyomo Tutorial: Index Sets. David L. Woodruff. Graduate School of Management .... Illustration from coopr.pyomo import * model = AbstractModel() model.I=Set().

106KB Sizes 14 Downloads 188 Views

Recommend Documents

Pyomo Tutorial: Index Sets -
Pyomo Tutorial: Index Sets. David L. Woodruff. Graduate School of Management .... Illustration from coopr.pyomo import * model = AbstractModel() model.I=Set().

VA FILEMAN V. 22 KEY AND INDEX TUTORIAL ... - Veterans Affairs
Sep 1, 2001 - Then choose a file number and a global root (internal global reference) for ...... Answer: A. "Control the direction in which FileMan traverses a ...

VA FILEMAN V. 22 KEY AND INDEX TUTORIAL ... - Veterans Affairs
Sep 1, 2001 - C. Neither of the above. Click here for answer. September 2001. VA FileMan V. 22 Key and Index Tutorial. 104. Revised: December 2006 ...

VA FILEMAN V. 22 KEY AND INDEX TUTORIAL ... - Veterans Affairs
Sep 1, 2001 - Use the New-Style Index in an Interactive IX^DIC Call ...... input variable to ^DIE to instruct VA FileMan to fire the record-level indexes and ...

A Brief Tutorial on Interval Type-2 Fuzzy Sets and Systems
May 6, 2013 - USA (phone: 213-595-3269; email: [email protected]; homepage: ... INTERVAL TYPE-2 FUZZY LOGIC SYSTEM (IT2 FLS). Fig. ..... Ip. Pp. Qp. Rp. Sp. X. %. Tp. Fig. 5. The 9-point representation of an IT2 FS. ..... [43] W. W. Tan and D. H. Kama

Interoperability with multiple instruction sets
Feb 1, 2002 - ABSTRACT. Data processing apparatus comprising: a processor core hav ing means for executing successive program instruction. Words of a ...

Interoperability with multiple instruction sets
Feb 1, 2002 - 712/209,. 712/210. See application ?le for complete search history. ..... the programmer speci?es the sorting order is to pass the address of a ...

Dense Admissible Sets
This search was implemented on a Cray T3D. Parallelizing was accomplished by breaking the sieve interval into equal pieces. It seemed possible that load.

Manheim Index - Automotive Digest
net increase of 157,000 jobs and a slight uptick in the unemployment rate to 7.9%, was greeted with ... used vehicle sales that occurred during the same period.

INDEX (UAE Tamil Sangam)
Nov 28, 2011 - FREE NRI/NRO Account with Karur Vysya Bank, India. Karur Vysya Bank's DGM/GM Group Head appreciated UAE Tamil Sangam Services & work, they supported Kalai Arangam 2011 ... Vysya Bank) and that account can be operated from anywhere in t

BamTools - Index of
Mar 22, 2011 - Page 3. Derek Barnett – 22 March 2011. -forceCompression. Force compression of BAM output. When tools are piped together (see details ...Missing:

MSCI Spain Index
Aug 31, 2017 - None of the Information or MSCI index or other product or service ... LAW, EACH MSCI PARTY HEREBY EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES, INCLUDING WARRANTIES ... Information Technology 5.47%.

Manheim Index - Automotive Digest
spike in personal income in December (driven by special dividends and early bonus payments ahead of tax hikes) and a significant rise in the stock market.

Index PDF - Unseen64
Centre for Computing History (43). Computer Spiele Museum (46). Felix Kupis (49) .... Are you a developer? (470). Authors & Editors | Unseen64 Collective (471).

INDEX (UAE Tamil Sangam)
Nov 28, 2011 - VOLUNTEERS & SUPPORT TEAM (Alphabetical order) ... Training Center, Computer Shop, Automobile-Tyre & Oil Change, Desert Safari,.

INDEX (UAE Tamil Sangam)
Nov 28, 2011 - With Emirates Identity Authority we published the Software & Application in our UAE Tamil ... UAE Tamil Sangam participated in Clean up the World Campaign 2010 ..... Please convey my best wishes and blessings to your members and their

FeynRules Tutorial
We will call mass eigenstates Φ1 and Φ2, and their masses M1 and M2, ... (3) where u and e are the SM up-quark and electron fields. Note that there is a Z2 symmetry ..... The kinetic terms for the fermions can be implemented in a similar way.

LaTeX Tutorial
To have formulas appear in their own paragraph, use matching $$'s to surround them. For example,. $$. \frac{x^n-1}{x-1} = \sum_{k=0}^{n-1}x^k. $$ becomes xn − 1 x − 1. = n−1. ∑ k=0 xk. Practice: Create your own document with both kinds of for

FeynRules Tutorial
The model we are considering depends on 9 new parameters, .... approach, and we start by opening a new notebook and load the FeynRules package (see the ...