Skip to content

Instantly share code, notes, and snippets.

@algrant
Created January 18, 2012 23:29
Show Gist options
  • Save algrant/1636511 to your computer and use it in GitHub Desktop.
Save algrant/1636511 to your computer and use it in GitHub Desktop.
Glitch - Element/Compound Optimiser
RED = 0
GREEN = 1
BLUE = 2
SHINY = 3
SELLING = 0
SUGGESTED = 1
class Compound():
def __init__(self, elements, price, name):
self.elements = elements #[R,G,B,S]
self.price = [float(i) for i in price] #[SUGGESTED, SELLING]
self.name = name
class Powder():
def __init__(self,compounds,price,name):
self.compounds = compounds
self.price = [float(i) for i in price] #[SUGGESTED, SELLING]
self.name = name
self.elements = [0,0,0,0] #R,G,B,S
self.updateElements()
def updateElements(self):
for c,amount in self.compounds.iteritems():
for i in range(4):
self.elements[i] += amount*comp_dic[c].elements[i]
mabon = Compound([0, 7, 7, 7], [12, 15], 'mabon')
grendalinunin = Compound([17, 7, 4, 2], [9, 11], 'grendalinunin')
potoxin = Compound([7, 11, 4, 0], [8, 9], 'potoxin')
spriggase = Compound([11, 7, 5, 0], [7, 9], 'spriggase')
cosmox = Compound([12, 0, 4, 3], [7, 8], 'cosmox')
alphose = Compound([3, 5, 7, 0], [6, 8], 'alphose')
abbasidose = Compound([4, 2, 6, 0], [5, 6], 'abbasidose')
humbabol = Compound([0, 0, 1, 5], [5, 5], 'humbabol')
zillene = Compound([0, 5, 3, 1], [4, 5], 'zillene')
friendly_acid = Compound([10, 0, 5, 0], [4, 5], 'friendly_acid')
rubemycin = Compound([5, 3, 2, 1], [4, 4], 'rubemycin')
lemene = Compound([4, 3, 2, 1], [4, 4], 'lemene')
ixite = Compound([2, 3, 2, 1], [3, 4], 'ixite')
groddlene = Compound([6, 3, 0, 0],[2, 2], 'groddlene')
diabolic_acid = Compound([1, 1, 1, 0],[1, 1], 'diabolic_acid')
tiite = Compound([2, 0, 1, 0], [1, 1], 'tiite')
sparkly = Compound([5, 1, 4, 6], [6, 8], 'sparkly')
compounds = [mabon,grendalinunin,potoxin,spriggase,cosmox,alphose,abbasidose,humbabol,zillene,friendly_acid,rubemycin,lemene,ixite,groddlene,diabolic_acid,tiite,sparkly]
comp_dic = {}
for c in compounds:
comp_dic[c.name] = c
EHSP = Powder({"humbabol":160,"groddlene":40,"zillene":175}, [1600, 2000], "EHSP")
FHSP = Powder({"mabon":20,"humbabol":55,"zillene":65}, [800, 1000], "FHSP")
Fertilidust = Powder({"cosmox":10,"potoxin":20,"humbabol":40}, [400, 500], "Fertilidust")
FertilidustLite = Powder({"potoxin":10,"humbabol":15}, [160, 200], "FertilidustLite")
KrazySalts = Powder({"ixite":11,"lemene":11,"tiite":11}, [89, 111], "KrazySalts")
NoNoPowder = Powder({"cosmox":11,"lemene":4}, [80, 100], "NoNoPowder")
PowderMildFecundity = Powder({"rubemycin":45,"alphose":40}, [400, 500], "PowderMildFecundity")
PowderStartlingFecundity = Powder({"cosmox":50,"rubemycin":75,"grendalinunin":60}, [1200, 1500], "PowderStartlingFecundity")
SneezePowder = Powder({"ixite":20,"tiite":65}, [120, 150], "SneezePowder")
SparklePowder = Powder({"abbasidose":30,"spriggase":30}, [360, 450], "SparklePowder")
powders = [EHSP,FHSP,Fertilidust,FertilidustLite,KrazySalts,NoNoPowder,PowderMildFecundity,PowderStartlingFecundity,SneezePowder,SparklePowder]
sparklies = 0
###############################################
# Best use of Elements, in terms of compounds #
###############################################
from pulp import *
#reshape data... just to follow this example
#http://packages.python.org/PuLP/CaseStudies/a_blending_problem.html
q = 1000
if True:
R_total = sparkly.elements[RED]*q
G_total = sparkly.elements[GREEN]*q
B_total = sparkly.elements[BLUE]*q
S_total = sparkly.elements[SHINY]*q
else:
R_total = q*5000
G_total = q*896
B_total = q*3056
S_total = q*46
price_style = SELLING
Comp_Names = []
price_dic = {}
r_dic = {}
g_dic = {}
b_dic = {}
s_dic = {}
for c in compounds:
Comp_Names.append(c.name)
price_dic[c.name] = c.price[price_style]
r_dic[c.name] = c.elements[RED]
g_dic[c.name] = c.elements[GREEN]
b_dic[c.name] = c.elements[BLUE]
s_dic[c.name] = c.elements[SHINY]
for c in powders:
Comp_Names.append(c.name)
price_dic[c.name] = c.price[price_style]
r_dic[c.name] = c.elements[RED]
g_dic[c.name] = c.elements[GREEN]
b_dic[c.name] = c.elements[BLUE]
s_dic[c.name] = c.elements[SHINY]
prob = LpProblem("Glitch Elements - Compund", LpMaximize)
compound_vars = LpVariable.dicts("Comps",Comp_Names,0,None,LpInteger)
# The objective function is added to 'prob' first
prob += lpSum([price_dic[i]*compound_vars[i] for i in Comp_Names]), "Total Selling Price for a Set of Compounds"
# The five constraints are added to 'prob'
prob += lpSum([r_dic[i]*compound_vars[i] for i in Comp_Names]) <= R_total, "Reds"
prob += lpSum([g_dic[i]*compound_vars[i] for i in Comp_Names]) <= G_total, "Greens"
prob += lpSum([b_dic[i]*compound_vars[i] for i in Comp_Names]) <= B_total, "Blues"
prob += lpSum([s_dic[i]*compound_vars[i] for i in Comp_Names]) <= S_total, "Shinies"
# The problem data is written to an .lp file
prob.writeLP("GlitchCompoundModel.lp")
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print "Status:", LpStatus[prob.status]
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
print v.name, "=", v.varValue
# The optimised objective function value is printed to the screen
print "Total Selling Price for a Set of Compounds = ", value(prob.objective)
# The optimised objective function value is printed to the screen
print "Price Increase over Sparkly rock", value(prob.objective)/(q*sparkly.price[price_style])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment