Re: [pymprog] Constraints and Iteration
An easy and flexible mathematical programming environment for Python.
Brought to you by:
lanyjie
From: Nico G. <nic...@gm...> - 2010-04-08 13:38:28
|
Hi Yingjie, not, it's not too late at all - this project is a persistent one. ^_- Thank you very much - I see now that I must get more accustomed to the generator syntax. Generally, is there a way to initialize an "empty" pymprog variable? I stumbled upon this problem when I tried to change the constraint from the zero-sum-game example for arbitrarily large games. In: st(p[1]+p[2]+p[3] == 1) the number of ps is already fixed to three. So, my idea was: ------------------------------ p = var([1,2,3]) sum = var() for i in range (len(p)): sum = sum + p[i+1] st(sum == 1) -------------------------------- The problem is that sum = var() leads sum being to X1=0.000000. And with that, my final condition ends up being: X0[1]+ X0[2]+ X0[3]+ X1 To get around this, I used a rather clumsy trick: ------------------------------- p = var([1,2,3]) sum = p[1] for i in range (len(p)-1): sum = sum + p[i+2] st(sum == 1) -------------------------------- I can't remember why I didn't just initialize sum as an int with value zero, but there must have been a reason for this. -_-° Anyway: Is there a way to initialize an empty pymprog variable? If not, is there a smart way to do this with generators? Many Thanks Bye Nico -----Ursprüngliche Nachricht----- Von: Yingjie Lan [mailto:la...@ya...] Gesendet: Montag, 8. März 2010 06:50 An: pym...@li...; Nico Grupp Betreff: Re: [pymprog] Constraints and Iteration > > m = 3 # 3 > strategies > > M = > range(m) # payoffs of player A > > n = 3 # 3 > strategies > > N = range(n) # payoffs > of player B > > > > (…) > > > > > > class Test: > > > def > __init__(self): > > > beginModel('game') > > > self.x = > var(A, 'x') > > > > > > So far, so good. > But when I try to > implement point 3, it doesn’t work. The idea was to > ensure that: > > > > payoff of > self.x[i,j] = - payoff of self.x[j,i] > > > > and it’d best > be done with > iterations. But when trying something like this: > > > > > > for i in > M: > > > for j in N: > > > st((self.x[i,j]) == > -(self.x[i,j])) > > > > > > it leads > to: > Hi, Nico: It is probably too late, but I tested with the code below: #--------------------Cut----------------------- from pymprog import * m = 3 # 3 strategies M = range(m) # payoffs of player A n = 3 # 3 strategies N = range(n) # payoffs of player B A = iprod(M,N) class Test: def __init__(me): beginModel('game') me.x = var(A,'x') #st((me.x[i,j]) == -(me.x[j,i]) for i,j in A) for i in M: for j in N: print st((me.x[i,j]) == - (me.x[j,i])) t=Test() #--------------------Cut----------------------- And this is the output (note: you might want to check if you are using the latest version of pymprog): #--------------------Cut----------------------- yaellan:pymprog xiaoliz$ python testreg.py s.t. R0: 2 x[(0, 0)] == 0 s.t. R1: x[(0, 1)]+ x[(1, 0)] == 0 s.t. R2: x[(2, 0)]+ x[(0, 2)] == 0 s.t. R3: x[(0, 1)]+ x[(1, 0)] == 0 s.t. R4: 2 x[(1, 1)] == 0 s.t. R5: x[(1, 2)]+ x[(2, 1)] == 0 s.t. R6: x[(2, 0)]+ x[(0, 2)] == 0 s.t. R7: x[(1, 2)]+ x[(2, 1)] == 0 s.t. R8: 2 x[(2, 2)] == 0 #--------------------Cut----------------------- Cheers, Yingjie |