Thread: [pymprog] Constraints and Iteration
An easy and flexible mathematical programming environment for Python.
Brought to you by:
lanyjie
From: Nico G. <nic...@gm...> - 2010-01-22 10:11:46
|
Hi everybody, I'm working on a way to do inverse linear optimization for 2 player zero sum games, but I'm encountering some problems concerning the constraints. When doing the inverse optimization, I know the probabilities with which each strategy is played in the nash equilibrium and want to calculate one of the many combinations of payoffs, so that my desired probabilities are a solution to this game. So, regarding this example: http://pymprog.sourceforge.net/tutorial.html#zero-sum-two-player-game, I want to do it basically the other way around. To make things easier, I'm only regarding symmetric two player zero sum games like Rock-Paper-Scissors (RPS), which enables me to make further assumptions: 1) Both players have the same set of strategies available 2) If both players utilize the same strategy, the payoff for both is zero 3) If strategy A versus strategy B leads to an outcome of X, then strategy B versus strategy A will lead to an outcome of -X (If I win with paper vs. rock and score one point, I will also lose one point if my opponent pulls the same trick and uses his paper vs. my rock). Now, there are several issues I have with the constraints. I have created the matrix like in example http://pymprog.sourceforge.net/advanced.html#re-optimize-the-assignment: 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: File "C:\Users\sano98\workspace\Balancer\src\Test.py", line 77, in calculate st((self.x[i,j]) == -(self.x[i,j])) File "C:\Python25\Lib\site-packages\pymprog.py", line 77, in st return prob.st(cons, name, inds) File "C:\Python25\Lib\site-packages\pymprog.py", line 267, in st cons = [t for t in cons] #copy TypeError: 'bool' object is not iterable Strangely, the constraint itself seems to work when called without iteration: self.constraint_01= st(self.x[0,1] == -(self.x[1,0])) self.constraint_02= st(self.x[0,2] == -(self.x[2,0])) self.constraint_03= st(self.x[2,1] == -(self.x[1,2])) But when it comes to place assumption 2) in the form of a constraint, it doesn't even work this way, since: self.constraint_04= st(self.x[0,0] == 0) also leads to File "C:\Users\sano98\workspace\Balancer\src\Test.py", line 98, in calculate self.constraint_04= st(self.x[0,0] == 0) File "C:\Python25\Lib\site-packages\pymprog.py", line 77, in st return prob.st(cons, name, inds) File "C:\Python25\Lib\site-packages\pymprog.py", line 267, in st cons = [t for t in cons] #copy TypeError: 'bool' object is not iterable Now, does anyone have an idea what the problem is? I can't seem to find any pattern why some of those things work and some don't. Thanks for the time and effort and hopefully for a solution or hint. ^_- Bye Nico |
From: Yingjie L. <la...@ya...> - 2010-03-08 06:16:28
|
> > 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 |
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 |