|
| 1 | +#!/usr/bin/python3 |
| 2 | +import multiprocessing |
| 3 | +import random |
| 4 | +import time |
| 5 | +import math |
| 6 | +import numpy |
| 7 | +from pprint import pprint |
| 8 | + |
| 9 | +debug = True |
| 10 | +threads = 1 if debug else multiprocessing.cpu_count() |
| 11 | + |
| 12 | +config = {'barn_width': 10, |
| 13 | + 'barn_height' : 10, |
| 14 | + 'number_of_cows' : 5} |
| 15 | + |
| 16 | +class Simulation: |
| 17 | + config = None |
| 18 | + barn = None |
| 19 | + cows = None |
| 20 | + |
| 21 | + def __init__(self, config): |
| 22 | + self.barn = Barn(config) |
| 23 | + self.cows = Cows(config) |
| 24 | + self.config = config |
| 25 | + |
| 26 | + for i in range(self.cows.count()): |
| 27 | + x, y = None, None |
| 28 | + while True: |
| 29 | + x = random.randrange(config['barn_width']) |
| 30 | + y = random.randrange(config['barn_height']) |
| 31 | + if self.barn.is_cell_empty((x, y)): break |
| 32 | +# energy = self.random.randrange(2 * self.cows_gain_from_food) |
| 33 | +# cows = Cow(self.next_id(), (x, y), self, False, energy) |
| 34 | + cow = Cow() |
| 35 | + self.barn.place(cow, (x, y)) |
| 36 | + # self.schedule.add(cows) |
| 37 | + |
| 38 | + def state(self): |
| 39 | + return {'barn': self.barn.state()} |
| 40 | + |
| 41 | + def run(self): |
| 42 | + pass |
| 43 | + |
| 44 | +class Cows: |
| 45 | + cows = None |
| 46 | + |
| 47 | + def __init__(self, config): |
| 48 | + self.cows = [Cow() for x in range(config['number_of_cows'])] |
| 49 | + |
| 50 | + def count(self): |
| 51 | + return len(self.cows) |
| 52 | + |
| 53 | + def cows(self): |
| 54 | + return self.cows |
| 55 | + |
| 56 | +class Barn: |
| 57 | + barn = None |
| 58 | + |
| 59 | + def __init__(self, config): |
| 60 | + w, h = config['barn_width'], config['barn_height'] |
| 61 | + self.barn = [[0 for x in range(w)] for y in range(h)] |
| 62 | + |
| 63 | + def is_cell_empty(self, x_y): |
| 64 | + x, y = x_y |
| 65 | + return False if self.barn[x][y] else True |
| 66 | + |
| 67 | + def place(self, cow, x_y): |
| 68 | + x, y = x_y |
| 69 | + # TODO: check |
| 70 | + self.barn[x][y] = cow |
| 71 | + |
| 72 | + def state(self): |
| 73 | + return self.barn |
| 74 | + |
| 75 | +class Wall: |
| 76 | + def __repr__(self): |
| 77 | + return '#' |
| 78 | + |
| 79 | +class Cow: |
| 80 | + water = 50 |
| 81 | + |
| 82 | + def water(self): |
| 83 | + return water |
| 84 | + |
| 85 | + def __repr__(self): |
| 86 | + return 'K' |
| 87 | + |
| 88 | +def sim(config): |
| 89 | + sim = Simulation(config) |
| 90 | + pprint(sim.state()) |
| 91 | + rand = bool(random.getrandbits(1)) |
| 92 | + |
| 93 | + return rand |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + print ("There are ", threads, " threads") |
| 98 | + |
| 99 | + start = time.time() |
| 100 | + |
| 101 | + jobs = [] |
| 102 | + for k in range(threads): |
| 103 | + jobs.append(config) |
| 104 | + |
| 105 | + pool = multiprocessing.Pool(threads) |
| 106 | + |
| 107 | + results = pool.map(sim, jobs) |
| 108 | + print(results) |
| 109 | + |
| 110 | + end = time.time() |
| 111 | + |
| 112 | + print ("The average number of steps is ", int(sum(results) / len(results)), " steps") |
| 113 | + print ("The simulation(s) took ", end - start, " seconds to run") |
| 114 | + |
| 115 | +print("Mean:", numpy.mean(results), " Median:", numpy.median(results), " Stdev:", numpy.std(results)) |
0 commit comments