Skip to content

Instantly share code, notes, and snippets.

@lifelike
Created August 14, 2013 19:14
Show Gist options
  • Select an option

  • Save lifelike/6234498 to your computer and use it in GitHub Desktop.

Select an option

Save lifelike/6234498 to your computer and use it in GitHub Desktop.
Solution to the constraint logic problem on http://intelligenceengine.blogspot.com/2013/08/decision-modeling-and-optimization-in.html using the python-constraint library (http://labix.org/python-constraint) instead of a spreadsheet.
from constraint import *
cl = ['Warlock', 'Wizard', 'Mage', 'Runecaster', 'Sorcelator']
sp = ['Fireball', 'Iceball', 'SpellSteal', 'Drain Life', 'Smashing Hand']
pk = ['Mana Regeneration', 'Endurance', 'Haste', 'Fire Mastery', 'Ice Mastery']
clsv = [c + 'Spell' for c in cl]
clpv = [c + 'Perk' for c in cl]
p = Problem()
p.addVariables(clsv, sp)
p.addVariables(clpv, pk)
p.addConstraint(AllDifferentConstraint())
for sv,pv in zip(clsv, clpv):
p.addConstraint(lambda sp, pk: (sp == 'Fireball') == (pk == 'Fire Mastery'),
[sv, pv])
p.addConstraint(lambda sp, pk: (sp == 'Iceball') == (pk == 'Ice Mastery'),
[sv, pv])
p.addConstraint(lambda sp, pk:
sp != 'SpellSteal' or pk != 'Mana Regeneration',
[sv, pv])
p.addConstraint(InSetConstraint(['Mana Regeneration']), ['WarlockPerk'])
p.addConstraint(NotInSetConstraint(['Iceball']), ['WizardSpell'])
p.addConstraint(InSetConstraint(['Drain Life']), ['RunecasterSpell'])
p.addConstraint(NotInSetConstraint(['Haste']), ['MagePerk'])
p.addConstraint(NotInSetConstraint(['Fireball', 'Iceball']), ['MageSpell'])
solution = p.getSolutions()[0]
for sv,pv in zip(clsv, clpv):
print "%25s: %-15s%25s: %s" %(sv, solution[sv], pv, solution[pv])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment