def prepareForRun(self): # if no numtabu* params given, use infinite state tabulist if (self._NUM_TABU_ACTIONS is None and self._NUM_TABU_STATES is None and self._NUM_TABU_TRANSITIONS is None): self.log("Using default: 'numtabustates:infinite'") self._NUM_TABU_STATES = INFINITY self._suggesters = [] # the funcs that suggest an action # order: action, state, transition if self._NUM_TABU_ACTIONS is not None: self._tabulist_action = TabuList(self._NUM_TABU_ACTIONS) self._suggesters.append(self._newAction) if self._NUM_TABU_STATES is not None: self._tabulist_state = TabuList(self._NUM_TABU_STATES) self._suggesters.append(self._newStateAction) if self._NUM_TABU_TRANSITIONS is not None: self._tabulist_transition = TabuList(self._NUM_TABU_TRANSITIONS) self._suggesters.append(self._newTransitionAction)
class TabuListUser: # The tabulist. # There's only 1 tabulist per process (it's a class variable). _THE_TABULIST = TabuList() # Locked when somebody's using the tabulist. _TABULIST_LOCK = Lock() # Number of connected TabuListUsers. _CONNECTED = 0 _CONN_LOCK = Lock() _PARAMS = None def __init__(self, params=None): TabuListUser._CONN_LOCK.acquire() TabuListUser._CONNECTED += 1 self._connNum = TabuListUser._CONNECTED if params is not None: TabuListUser._PARAMS = params TabuListUser._CONN_LOCK.release() def getParameters(self): return TabuListUser._PARAMS def connNum(self): return self._connNum def len(self): TabuListUser._TABULIST_LOCK.acquire() le = len(TabuListUser._THE_TABULIST) TabuListUser._TABULIST_LOCK.release() return le def add(self, item): TabuListUser._TABULIST_LOCK.acquire() TabuListUser._THE_TABULIST.add(item) TabuListUser._TABULIST_LOCK.release() def addMany(self, items): TabuListUser._TABULIST_LOCK.acquire() for item in items: TabuListUser._THE_TABULIST.add(item) TabuListUser._TABULIST_LOCK.release() def tabunessOf(self, items): """ Eg. If the 3 first items are tabu and the last one is not, returns: (True,True,True,False) """ TabuListUser._TABULIST_LOCK.acquire() to = tuple([i in TabuListUser._THE_TABULIST for i in items]) TabuListUser._TABULIST_LOCK.release() return to
def setParameter(self, name, value): name = name.lower() if name in ("size", "tabusize"): self._tabuList = TabuList(_parseSize(value)) elif name == "weight": if value in ("decreasing", "decr"): self.weight = self._decreasingWeight else: self.weight = lambda: 1 elif name == "name": self._name = value else: print __doc__ raise ValueError("Invalid param '%s' for %s" % (name, self.__class__.__name__))
def __init__(self, paramStr): # default params: self.setParameter("weight", "decreasing") self._tabuList = TabuList() # user-given params: self.setParameterStr(paramStr)