Skip to content

Commit

Permalink
Remove Cython classes for enumpure, enummixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
tturocy committed Sep 14, 2023
1 parent 0e580a9 commit adf795c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 77 deletions.
103 changes: 32 additions & 71 deletions src/pygambit/nash.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -33,80 +33,41 @@ def enumpure_strategy_solve(game: Game) -> typing.List[MixedStrategyProfileRatio
ret.append(p)
return ret

cdef class EnumPureAgentSolver:
cdef c_NashEnumPureAgentSolver *alg

def __cinit__(self, p_stopAfter=0, p_maxDepth=0):
self.alg = new c_NashEnumPureAgentSolver()
def __dealloc__(self):
del self.alg
def solve(self, Game game):
cdef c_List[c_MixedBehaviorProfileRational] solns
cdef MixedBehaviorProfileRational p
cdef shared_ptr[c_BehaviorSupportProfile] profile
profile = make_shared[c_BehaviorSupportProfile](game.game)
solns = self.alg.Solve(deref(profile))
ret = [ ]
for i in xrange(solns.Length()):
p = MixedBehaviorProfileRational()
p.profile = copyitem_list_mbpr(solns, i+1)
ret.append(p)
return ret


cdef class EnumMixedStrategySolverDouble:
cdef c_NashEnumMixedStrategySolverDouble *alg

def __cinit__(self):
self.alg = new c_NashEnumMixedStrategySolverDouble()
def __dealloc__(self):
del self.alg
def solve(self, Game game):
cdef c_List[c_MixedStrategyProfileDouble] solns
cdef MixedStrategyProfileDouble p
solns = self.alg.Solve(game.game)
ret = [ ]
for i in xrange(solns.Length()):
p = MixedStrategyProfileDouble()
p.profile = copyitem_list_mspd(solns, i+1)
ret.append(p)
return ret

cdef class EnumMixedStrategySolverRational:
cdef c_NashEnumMixedStrategySolverRational *alg
def enumpure_agent_solve(game: Game) -> typing.List[MixedBehaviorProfileRational]:
solns = c_NashEnumPureAgentSolver().Solve(c_BehaviorSupportProfile(game.game))
ret = []
for i in range(solns.Length()):
p = MixedBehaviorProfileRational()
p.profile = copyitem_list_mbpr(solns, i+1)
ret.append(p)
return ret

def __cinit__(self):
self.alg = new c_NashEnumMixedStrategySolverRational()
def __dealloc__(self):
del self.alg
def solve(self, Game game):
cdef c_List[c_MixedStrategyProfileRational] solns
cdef MixedStrategyProfileRational p
solns = self.alg.Solve(game.game)
ret = [ ]
for i in xrange(solns.Length()):
p = MixedStrategyProfileRational()
p.profile = copyitem_list_mspr(solns, i+1)
ret.append(p)
return ret
def enummixed_strategy_solve_double(game: Game) -> typing.List[MixedStrategyProfileDouble]:
solns = c_NashEnumMixedStrategySolverDouble().Solve(game.game)
ret = []
for i in range(solns.Length()):
p = MixedStrategyProfileDouble()
p.profile = copyitem_list_mspd(solns, i+1)
ret.append(p)
return ret

cdef class EnumMixedLrsStrategySolver:
cdef c_NashEnumMixedLrsStrategySolver *alg
def enummixed_strategy_solve_rational(game: Game) -> typing.List[MixedStrategyProfileRational]:
solns = c_NashEnumMixedStrategySolverRational().Solve(game.game)
ret = []
for i in range(solns.Length()):
p = MixedStrategyProfileRational()
p.profile = copyitem_list_mspr(solns, i+1)
ret.append(p)
return ret

def __cinit__(self):
self.alg = new c_NashEnumMixedLrsStrategySolver()
def __dealloc__(self):
del self.alg
def solve(self, Game game):
cdef c_List[c_MixedStrategyProfileRational] solns
cdef MixedStrategyProfileRational p
solns = self.alg.Solve(game.game)
ret = [ ]
for i in xrange(solns.Length()):
p = MixedStrategyProfileRational()
p.profile = copyitem_list_mspr(solns, i+1)
ret.append(p)
return ret
def enummixed_strategy_solve_lrs(game: Game) -> typing.List[MixedStrategyProfileRational]:
solns = c_NashEnumMixedLrsStrategySolver().Solve(game.game)
ret = []
for i in range(solns.Length()):
p = MixedStrategyProfileRational()
p.profile = copyitem_list_mspr(solns, i+1)
ret.append(p)
return ret


cdef class LCPBehaviorSolverDouble:
Expand Down
10 changes: 4 additions & 6 deletions src/pygambit/nash.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ def enumpure_solve(
if not game.is_tree or use_strategic:
return libgbt.enumpure_strategy_solve(game)
else:
alg = libgbt.EnumPureAgentSolver()
return alg.solve(game)
return libgbt.enumpure_agent_solve(game)


def enummixed_solve(
Expand Down Expand Up @@ -338,12 +337,11 @@ def enummixed_solve(
if external:
return ExternalEnumMixedSolver().solve(game, rational=rational)
if use_lrs:
alg = libgbt.EnumMixedLrsStrategySolver()
return libgbt.enummixed_strategy_solve_lrs(game)
elif rational:
alg = libgbt.EnumMixedStrategySolverRational()
return libgbt.enummixed_strategy_solve_rational(game)
else:
alg = libgbt.EnumMixedStrategySolverDouble()
return alg.solve(game)
return libgbt.enummixed_strategy_solve_double(game)


def lcp_solve(
Expand Down

0 comments on commit adf795c

Please sign in to comment.