Skip to content

Commit

Permalink
Added ability to specify constraint function in paramsearch
Browse files Browse the repository at this point in the history
git-svn-id: https://ilk.uvt.nl/svn/trunk/sources/pynlpl@13486 12f355fe-0486-481a-ad91-c297ab22b4e3
  • Loading branch information
proycon committed Oct 28, 2011
1 parent e9afed2 commit 09ae06a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def run(self, haltonerror=True):
class WPSParamSearch(object):
"""ParamSearch with support for Wrapped Progressive Sampling"""

def __init__(self, experimentclass, inputdata, size, parameterscope, poolsize=1, sizefunc=None, prunefunc=None, delete=True): #parameterscope: {'parameter':[values]}
def __init__(self, experimentclass, inputdata, size, parameterscope, poolsize=1, sizefunc=None, prunefunc=None, constraintfunc = None, delete=True): #parameterscope: {'parameter':[values]}
self.ExperimentClass = experimentclass
self.inputdata = inputdata
self.poolsize = poolsize #0 or 1: sequential execution (uses experiment.run() ), >1: parallel execution using ExperimentPool (uses experiment.start() )
Expand All @@ -399,10 +399,15 @@ def __init__(self, experimentclass, inputdata, size, parameterscope, poolsize=1,
self.prunefunc = prunefunc
else:
self.prunefunc = lambda i: 0.5


if constraintfunc != None:
self.constraintfunc = constraintfunc
else:
self.constraintfunc = lambda x: True

#compute all parameter combinations:
verboseparameterscope = [ self._combine(x,y) for x,y in parameterscope.items() ]
self.parametercombinations = [ (x,0) for x in itertools_product(*verboseparameterscope) ] #generator
self.parametercombinations = [ (x,0) for x in itertools_product(*verboseparameterscope) if self.constraintfunc(dict(x)) ] #generator

def _combine(self,name, values): #TODO: can't we do this inline in a list comprehension?
l = []
Expand Down Expand Up @@ -469,9 +474,9 @@ def __iter__(self):

class ParamSearch(WPSParamSearch):
"""A simpler version of ParamSearch without Wrapped Progressive Sampling"""
def __init__(self, experimentclass, inputdata, parameterscope, poolsize=1, delete=True): #parameterscope: {'parameter':[values]}
def __init__(self, experimentclass, inputdata, parameterscope, poolsize=1, constraintfunc = None, delete=True): #parameterscope: {'parameter':[values]}
prunefunc = lambda x: 0
super(ParamSearch, self).__init__(experimentclass, inputdata, -1, parameterscope, poolsize, None,prunefunc, delete)
super(ParamSearch, self).__init__(experimentclass, inputdata, -1, parameterscope, poolsize, None,prunefunc, constraintfunc, delete)

def __iter__(self):
for parametercombination, score in sorted(self.test(), key=lambda v: v[1]):
Expand Down

0 comments on commit 09ae06a

Please sign in to comment.