Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. '''Build_Sustainable.py
  2. A QUIET Solving Tool problem formulation.
  3. QUIET = Quetzal User Intelligence Enhancing Technology.
  4. The XML-like tags used here serve to identify key sections of this
  5. problem formulation.
  6.  
  7. CAPITALIZED constructs are generally present in any problem
  8. formulation and therefore need to be spelled exactly the way they are.
  9. Other globals begin with a capital letter but otherwise are lower
  10. case or camel case.
  11. '''
  12. # <METADATA>
  13. QUIET_VERSION = "0.2"
  14. PROBLEM_NAME = "Designing buildings for Environmental Sustainability"
  15. PROBLEM_VERSION = "0.2"
  16. PROBLEM_AUTHORS = ['A. Krishnamoorthy', "Bum Mook Oh", "Ishan Saksena"]
  17. PROBLEM_CREATION_DATE = "27-APR-2017"
  18. PROBLEM_DESC = \
  19. '''This formulation models sustainable building design uses generic
  20. Python 3 constructs and has been tested with Python 3.4.
  21. It is designed to work according to the QUIET tools interface, Version 0.2.
  22. '''
  23.  
  24.  
  25. # </METADATA>
  26.  
  27. # <COMMON_CODE>
  28.  
  29. def can_build(s, b_property, amount):
  30. '''Tests whether we can change the construction factor'''
  31. brickAmount = 0
  32. if b_property == 'PeopleServed':
  33. newCost = brickToPeople(s)
  34. else:
  35. newCost = COSTS[b_property]
  36. try:
  37. enough_budget = (s.d['budget'] >= newCost)
  38. if (b_property in bricks(s)):
  39. brickAmount = s.d['ConstructionMaterial'][b_property]
  40. return enough_budget and (amount > 0 and amount + brickAmount > 0)
  41. except (Exception) as e:
  42. print(e)
  43.  
  44.  
  45. def build(s, b_property, quantity):
  46. '''Assuming it's legal to make the move, this computes
  47. the new state resulting from moving the topmost disk
  48. from the From peg to the To peg.'''
  49. new_state = s.__copy__() # start with a deep copy.
  50. d2 = new_state.d # grab the new state's dictionary.
  51. if (b_property in bricks(s)):
  52. d2['ConstructionMaterial'][b_property] += quantity
  53.  
  54. if b_property == 'PeopleServed':
  55. d2['budget'] -= brickToPeople(s)
  56. else:
  57. d2['budget'] -= COSTS[b_property]
  58.  
  59. return new_state # return new state
  60.  
  61.  
  62. def goal_test(s):
  63. '''If the first two pegs are empty, then s is a goal state.'''
  64. return not s.d['budget']
  65.  
  66.  
  67. def goal_message(s):
  68. return "Our building is pretty sustainable"
  69.  
  70.  
  71. class Operator:
  72. def __init__(self, name, precond, state_transf):
  73. self.name = name
  74. self.precond = precond
  75. self.state_transf = state_transf
  76.  
  77. def is_applicable(self, s):
  78. return self.precond(s)
  79.  
  80. def apply(self, s):
  81. return self.state_transf(s)
  82.  
  83.  
  84. # </COMMON_CODE>
  85.  
  86. # <COMMON_DATA>
  87.  
  88. # </COMMON_DATA>
  89.  
  90. # <STATE>
  91. class State():
  92. def __init__(self, d):
  93. self.d = d
  94.  
  95. def __str__(self):
  96. # Produces a brief textual description of a state.
  97. d = self.d
  98. txt = "["
  99. for k in d.keys():
  100. if k == 'ConstructionMaterial':
  101. for brick in self.d[k].keys():
  102. txt += brick + ': ' + str(self.d[k][brick])
  103. txt += ", "
  104. else:
  105. txt += str(k) + ': ' + str(d[k])
  106. txt += ", "
  107. return txt[:-2] + "]"
  108.  
  109. def __eq__(self, s2):
  110. if not (type(self) == type(s2)): return False
  111. d1 = self.d
  112. d2 = s2.d
  113. for k in d1['ConstructionMaterial'].keys():
  114. if d1['ConstructionMaterial'][k] != d2['ConstructionMaterial'][k]:
  115. return False
  116. return (d1['NumberOfPlants'] == d2['NumberOfPlants']) and (d1['PeopleServed'] == d2['PeopleServed'])
  117.  
  118. def __hash__(self):
  119. return (str(self)).__hash__()
  120.  
  121. def __copy__(self):
  122. # Performs an appropriately deep copy of a state,
  123. # for use by operators in creating new states.
  124. news = State({})
  125. self.d
  126. for k in self.d.keys():
  127. if k == 'ConstructionMaterial':
  128. for brick in self.d[k]:
  129. if 'ConstructionMaterial' not in news.d:
  130. news.d['ConstructionMaterial'] = {}
  131. # print(self.d)
  132. # print()
  133. # print()
  134. news.d['ConstructionMaterial'][brick] = self.d['ConstructionMaterial'][brick]
  135. else:
  136. news.d[k] = self.d[k]
  137.  
  138. return news
  139.  
  140.  
  141. # </STATE>
  142.  
  143. # <INITIAL_STATE>
  144. COSTS = {"Red Brick": 40, "Ash Brick": 30, "Concrete": 20, 'NumberOfPlants': 5}
  145. bricks = lambda s : (s.d["ConstructionMaterial"].keys())
  146. brickToPeople = lambda s: sum([s.d["ConstructionMaterial"][key] for key in s.d["ConstructionMaterial"].keys()]) / 20
  147. brickSum = lambda s: sum([s.d["ConstructionMaterial"][key] for key in s.d["ConstructionMaterial"].keys()])
  148. INITIAL_STATE = State(
  149. {'ConstructionMaterial': {'Red Brick': 0, 'Ash Brick': 0, 'Concrete': 0}, 'EmissionPenalty': 0, 'NumberOfPlants': 0,
  150. 'PeopleServed': 0, 'cost': 0, 'budget': 10000})
  151. CREATE_INITIAL_STATE = lambda: INITIAL_STATE
  152. # </INITIAL_STATE>
  153.  
  154. # <OPERATORS>
  155.  
  156. build_combinations = [('Red Brick', 1000), ('Red Brick', -1000), ('Ash Brick', 1000),
  157. ('Ash Brick', -1000), ('Concrete', 1000), ('Concrete', -1000), ('NumberOfPlants', 5),
  158. ('NumberOfPlants', -5), ('PeopleServed', 10), ('PeopleServed', -10)]
  159.  
  160.  
  161. OPERATORS = [Operator("Change " + str(b_property) + " by " + str(amount),
  162. lambda s, b_property1=b_property, amount1=amount: can_build(s, b_property1, amount1),
  163. # The default value construct is needed
  164. # here to capture the values of p&q separately
  165. # in each iteration of the list comp. iteration.
  166. lambda s, b_property1=b_property, amount1=amount: build(s, b_property1, amount1))
  167. for (b_property, amount) in build_combinations]
  168. # </OPERATORS>
  169.  
  170. # <GOAL_TEST>
  171. GOAL_TEST = lambda s: goal_test(s)
  172. # </GOAL_TEST>
  173.  
  174. # <GOAL_MESSAGE_FUNCTION>
  175. GOAL_MESSAGE_FUNCTION = lambda s: goal_message(s)
  176. # </GOAL_MESSAGE_FUNCTION>
  177.  
  178. # <HEURISTICS> (optional)
  179. def heuristic1(s):
  180. d = s.d
  181. return d["PeopleServed"] + d["NumberOfPlants"] - brickSum(s) - d["EmissionPenalty"]
  182.  
  183. HEURISTICS = {'heuristic1': heuristic1}
  184. # </HEURISTICS>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement