Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created February 7, 2011 14:06
Show Gist options
  • Save vedantk/814397 to your computer and use it in GitHub Desktop.
Save vedantk/814397 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
def eval_solution(self, soln):
self.apply_solution(soln)
confs = self.find_conflicts(next_state)
score = sum([weight_of(conflict) for conflict in confs])
self.revert_solution(soln)
return score
def find_solutions(self, conflict):
return conflict['issue'].fixer(conflict)
def find_conflicts(self, section):
return filter(lambda conf: conf, [test(section) for test in self.conf_tests])
def apply_solution(self, soln):
pass
def revert_solution(self, soln):
pass
def min_conflicts(self):
for k in xrange(1000):
issues = []
all_secs = Section.objects.all()
for section in all_secs:
conflicts = self.find_conflicts(section)
if len(conflicts):
issues.extend(conflicts)
if not len(issues):
return True
conflict = random.choice(issues)
solutions = self.find_solutions(conflict)
scores = [self.eval_solution(soln) for soln in solutions]
soln = solutions[scores.index(min(scores))]
self.apply_solution(soln)
return False
def no_planning_fixer(self, conflict):
# return a [solution]
pass
class Issue:
def __init__(self, weight, fixer):
self.weight = weight
self.fixer = fixer
class ISched:
__init__
self.NoPlanningBlock = Issue(1.00, self.no_planning_fixer)
def some_test(self, section):
return {
'issue': self.NoPlanningBlock,
'section': section,
# other data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment