def _hsStatePeriodicUpdate(self, exhaustedSwarmId=None):
if self._hsState is None:<EOL><INDENT>self._hsState =  HsState(self)<EOL><DEDENT>self._hsState.readStateFromDB()<EOL>completedSwarms = set()<EOL>if exhaustedSwarmId is not None:<EOL><INDENT>self.logger.info("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (exhaustedSwarmId))<EOL>(particles, _, _, _, _) = self._resultsDB.getParticleInfos(<EOL>swarmId=exhaustedSwarmId, matured=False)<EOL>if len(particles) > <NUM_LIT:0>:<EOL><INDENT>exhaustedSwarmStatus = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>exhaustedSwarmStatus = '<STR_LIT>'<EOL><DEDENT><DEDENT>if self._killUselessSwarms:<EOL><INDENT>self._hsState.killUselessSwarms()<EOL><DEDENT>completingSwarms = self._hsState.getCompletingSwarms()<EOL>for swarmId in completingSwarms:<EOL><INDENT>(particles, _, _, _, _) = self._resultsDB.getParticleInfos(<EOL>swarmId=swarmId, matured=False)<EOL>if len(particles) == <NUM_LIT:0>:<EOL><INDENT>completedSwarms.add(swarmId)<EOL><DEDENT><DEDENT>completedSwarmGens = self._resultsDB.getMaturedSwarmGenerations()<EOL>priorCompletedSwarms = self._hsState.getCompletedSwarms()<EOL>for (swarmId, genIdx, errScore) in completedSwarmGens:<EOL><INDENT>if swarmId in priorCompletedSwarms:<EOL><INDENT>continue<EOL><DEDENT>completedList = self._swarmTerminator.recordDataPoint(<EOL>swarmId=swarmId, generation=genIdx, errScore=errScore)<EOL>statusMsg = "<STR_LIT>""<STR_LIT>" % (genIdx, swarmId, errScore)<EOL>if len(completedList) > <NUM_LIT:0>:<EOL><INDENT>statusMsg = "<STR_LIT>" % (statusMsg, completedList)<EOL><DEDENT>self.logger.info(statusMsg)<EOL>self._cjDAO.jobSetFields (jobID=self._jobID,<EOL>fields=dict(engStatus=statusMsg),<EOL>useConnectionID=False,<EOL>ignoreUnchanged=True)<EOL>if '<STR_LIT>' in os.environ:<EOL><INDENT>while True:<EOL><INDENT>resultsStr = self._cjDAO.jobGetFields(self._jobID, ['<STR_LIT>'])[<NUM_LIT:0>]<EOL>if resultsStr is None:<EOL><INDENT>results = {}<EOL><DEDENT>else:<EOL><INDENT>results = json.loads(resultsStr)<EOL><DEDENT>if not '<STR_LIT>' in results:<EOL><INDENT>results['<STR_LIT>'] = {}<EOL><DEDENT>for swarm in completedList:<EOL><INDENT>if swarm not in results['<STR_LIT>']:<EOL><INDENT>results['<STR_LIT>'][swarm] = (genIdx,<EOL>self._swarmTerminator.swarmScores[swarm])<EOL><DEDENT><DEDENT>newResultsStr = json.dumps(results)<EOL>if newResultsStr == resultsStr:<EOL><INDENT>break<EOL><DEDENT>updated = self._cjDAO.jobSetFieldIfEqual(jobID=self._jobID,<EOL>fieldName='<STR_LIT>',<EOL>curValue=resultsStr,<EOL>newValue = json.dumps(results))<EOL>if updated:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>if len(completedList) > <NUM_LIT:0>:<EOL><INDENT>for name in completedList:<EOL><INDENT>self.logger.info("<STR_LIT>"<EOL>"<STR_LIT:%s>" % (name, genIdx, errScore))<EOL><DEDENT>completedSwarms = completedSwarms.union(completedList)<EOL><DEDENT><DEDENT>if len(completedSwarms)==<NUM_LIT:0> and (exhaustedSwarmId is None):<EOL><INDENT>return<EOL><DEDENT>while True:<EOL><INDENT>if exhaustedSwarmId is not None:<EOL><INDENT>self._hsState.setSwarmState(exhaustedSwarmId, exhaustedSwarmStatus)<EOL><DEDENT>for swarmId in completedSwarms:<EOL><INDENT>self._hsState.setSwarmState(swarmId, '<STR_LIT>')<EOL><DEDENT>if not self._hsState.isDirty():<EOL><INDENT>return<EOL><DEDENT>success = self._hsState.writeStateToDB()<EOL>if success:<EOL><INDENT>jobResultsStr = self._cjDAO.jobGetFields(self._jobID, ['<STR_LIT>'])[<NUM_LIT:0>]<EOL>if jobResultsStr is not None:<EOL><INDENT>jobResults = json.loads(jobResultsStr)<EOL>bestModelId = jobResults.get('<STR_LIT>', None)<EOL><DEDENT>else:<EOL><INDENT>bestModelId = None<EOL><DEDENT>for swarmId in list(completedSwarms):<EOL><INDENT>(_, modelIds, _, _, _) = self._resultsDB.getParticleInfos(<EOL>swarmId=swarmId, completed=False)<EOL>if bestModelId in modelIds:<EOL><INDENT>modelIds.remove(bestModelId)<EOL><DEDENT>if len(modelIds) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>self.logger.info("<STR_LIT>"<EOL>"<STR_LIT>" % (swarmId,<EOL>str(modelIds)))<EOL>for modelId in modelIds:<EOL><INDENT>self._cjDAO.modelSetFields(modelId,<EOL>dict(engStop=ClientJobsDAO.STOP_REASON_KILLED),<EOL>ignoreUnchanged = True)<EOL><DEDENT><DEDENT>return<EOL><DEDENT>self._hsState.readStateFromDB()<EOL>self.logger.debug("<STR_LIT>"<EOL>"<STR_LIT>" % (pprint.pformat(self._hsState._state, indent=<NUM_LIT:4>)))<EOL><DEDENT>
Periodically, check to see if we should remove a certain field combination
from evaluation (because it is doing so poorly) or move on to the next
sprint (add in more fields).

This method is called from _getCandidateParticleAndSwarm(), which is called
right before we try and create a new model to run.

Parameters:
-----------------------------------------------------------------------
removeSwarmId:     If not None, force a change to the current set of active
                  swarms by removing this swarm. This is used in situations
                  where we can't find any new unique models to create in
                  this swarm. In these situations, we update the hypersearch
                  state regardless of the timestamp of the last time another
                  worker updated it.