-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetplants.py
More file actions
68 lines (52 loc) · 2.22 KB
/
setplants.py
File metadata and controls
68 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# Returns a dictionary of plant placement on the graph.
from grid import generate_squares, generate_peers
from itertools import permutations
from copy import deepcopy
def image_list():
f = open("plantdata/images.txt")
return [p.strip() for p in f]
def create_grid(squares, picked_plants):
"""Convert grid to a dict of possible values {square:[avaliable plants]}"""
return dict((s, picked_plants) for s in squares)
# Add this feature later
def prepicked_values(grid, squares):
"""Convert grid into a dict of {square:char} with "." for empty."""
pass
def square_benefit(values, plant, square, guide, peers):
"""Returns benefit of placing the plant in that square."""
score = 0
square_peers = peers[square]
# Check benefit/cost for assigned squares.
for peer in square_peers:
if len(values[peer]) == 1: # if assigned
peer_plant = values[peer][0]
if peer_plant in guide[plant]['avoid']:
score = score - 5
elif peer_plant in guide[plant]['friend']:
score = score + 3
elif peer_plant == plant:
score = score - 1
return score
def set_plants(squares, picked_plants):
picked_plants = list(picked_plants)
return {squares[i]:[picked_plants[i]] for i in range(len(squares))}
def score(squares, picked_plants, peers, guide):
current = set_plants(squares, picked_plants)
benefits = {}
for square in current:
plant = current[square][0]
benefits[square] = square_benefit(current, plant, square, guide, peers)
total = sum(benefits.itervalues())
return total, picked_plants, benefits
def solve(guide, picked_plants, width, length):
"""Iterates through all possible ordering of the plants.
Compares the benefits of each setting of plants, and returns the highest
score."""
peers = generate_peers(width, length)
squares = generate_squares(width, length)
patterns = {plants:True
for plants in permutations(picked_plants, len(picked_plants))}
planting_score, best_values, best_benefits = max(
score(squares, plants, peers, guide) for plants in patterns)
return set_plants(squares, best_values), best_benefits