| 1 |
# coding=utf8 |
| 2 |
""" |
| 3 |
The fridge simulation |
| 4 |
|
| 5 |
@author: Stefan Scherfke |
| 6 |
@contact: stefan.scherfke at uni-oldenburg.de |
| 7 |
""" |
| 8 |
|
| 9 |
from time import clock |
| 10 |
import logging |
| 11 |
|
| 12 |
from SimPy.Simulation import Simulation, activate, initialize, simulate |
| 13 |
import pp |
| 14 |
|
| 15 |
from processes import Fridge, FridgeObserver |
| 16 |
|
| 17 |
log = logging.getLogger('Simulator') |
| 18 |
|
| 19 |
class Simulator(object): |
| 20 |
""" |
| 21 |
This class simulates a number of fridges and gets the resulting data. |
| 22 |
""" |
| 23 |
|
| 24 |
def __init__(self, numFridges, tau, aggSteps, duration): |
| 25 |
""" |
| 26 |
Setup the simulation with the specified number of fridges. |
| 27 |
|
| 28 |
Tau specifies the simulation step for each frige. Furthermore the |
| 29 |
observer will collect data each tau. Collected data |
| 30 |
will be aggregated at the end of each aggSteps simulation steps. |
| 31 |
|
| 32 |
@param numFridges: The number of simulated fridges |
| 33 |
@type numFridges: unsigned int |
| 34 |
@param tau: simulation step size for collecting data and simulating |
| 35 |
the fridge |
| 36 |
@type tau: float |
| 37 |
@param aggSteps: Collected data will be aggregated each aggSteps |
| 38 |
simulation steps. Signals interval will be |
| 39 |
tau * aggSteps |
| 40 |
@type aggSteps: unsigned int |
| 41 |
@param duration: Duration of the simulation in hours |
| 42 |
@type duration: unsigned int |
| 43 |
""" |
| 44 |
log.info('Initializing simulator ...') |
| 45 |
self.simEnd = duration |
| 46 |
self.sim = Simulation() |
| 47 |
|
| 48 |
fridgeProperties = {'tau': tau} |
| 49 |
self._fridges = [] |
| 50 |
for i in range(numFridges): |
| 51 |
fridge = Fridge(self.sim, **fridgeProperties) |
| 52 |
self._fridges.append(fridge) |
| 53 |
self._observer = FridgeObserver(self.sim, self._fridges, tau, aggSteps) |
| 54 |
|
| 55 |
def simulate(self): |
| 56 |
""" |
| 57 |
Initialize the system, start the simulation and return the collected |
| 58 |
data. |
| 59 |
|
| 60 |
@return: The fridgerators consumption after each aggregation |
| 61 |
""" |
| 62 |
log.info('Running simulation ...') |
| 63 |
self.sim.initialize() |
| 64 |
for fridge in self._fridges: |
| 65 |
self.sim.activate(fridge, fridge.run(), at = 0) |
| 66 |
self.sim.activate(self._observer, self._observer.run(), at = 0) |
| 67 |
self.sim.simulate(until = self.simEnd) |
| 68 |
|
| 69 |
log.info('Simulation run finished.') |
| 70 |
return self._observer.getData() |
| 71 |
|
| 72 |
|
| 73 |
class ParallelSimulator(object): |
| 74 |
""" |
| 75 |
This class simulates a number of fridges and gets the resulting data. |
| 76 |
Unlike simulator, a number of jobs will be created that use all availale |
| 77 |
CPU cores or even other computers. |
| 78 |
|
| 79 |
To use clustering, ParallelPython needs to be installed on all computers |
| 80 |
and the server demon "ppserver.py" must be started. The list of the server's |
| 81 |
IPs must then be passed to the constructor of this class. |
| 82 |
""" |
| 83 |
|
| 84 |
def __init__(self, numFridges, tau, aggSteps, duration, |
| 85 |
jobSize = 100, servers = ()): |
| 86 |
""" |
| 87 |
Setup the simulation with the specified number of fridges. It will be |
| 88 |
split up in several parallel jobs, each with the specified number of |
| 89 |
jobs. |
| 90 |
|
| 91 |
Tau specifies the simulation step for each frige. Furthermore the |
| 92 |
observer will collect data each tau. Collected data |
| 93 |
will be aggregated at the end of each aggSteps simulation steps. |
| 94 |
|
| 95 |
@param numFridges: The number of simulated fridges |
| 96 |
@type numFridges: unsigned int |
| 97 |
@param tau: simulation step size for collecting data and simulating |
| 98 |
the fridge |
| 99 |
@type tau: float |
| 100 |
@param aggSteps: Collected data will be aggregated each aggSteps |
| 101 |
simulation steps. Signals interval will be |
| 102 |
tau * aggSteps |
| 103 |
@type aggSteps: unsigned int |
| 104 |
@param duration: Duration of the simulation |
| 105 |
@type duration: unsigned int |
| 106 |
@param jobSize: The number of friges per job, defaults to 100. |
| 107 |
@type jobSize: unsigned int |
| 108 |
@param servers: A list of IPs from on which the simulation shall be |
| 109 |
executed. Defaults to "()" (use only SMP) |
| 110 |
@type servers: tuple of string |
| 111 |
""" |
| 112 |
log.info('Initializing prallel simulation ...') |
| 113 |
|
| 114 |
self._jobSize = jobSize |
| 115 |
self._servers = servers |
| 116 |
self._numFridges = numFridges |
| 117 |
self._tau = tau |
| 118 |
self._aggSteps = aggSteps |
| 119 |
self.simEnd = duration |
| 120 |
|
| 121 |
def simulate(self): |
| 122 |
""" |
| 123 |
Create some simulation jobs, run them and retrieve their results. |
| 124 |
|
| 125 |
@return: The fridgerators consumption after each aggregation |
| 126 |
""" |
| 127 |
log.info('Running parallel simulation ...') |
| 128 |
oldLevel = log.getEffectiveLevel() # pp changes the log level :( |
| 129 |
jobServer = pp.Server(ppservers = self._servers) |
| 130 |
|
| 131 |
# Start the jobs |
| 132 |
remainingFridges = self._numFridges |
| 133 |
jobs = [] |
| 134 |
while remainingFridges > 0: |
| 135 |
jobs.append(jobServer.submit(self.runSimulation, |
| 136 |
(min(self._jobSize, remainingFridges),), |
| 137 |
(), |
| 138 |
("logging", "SimPy.Simulation", "processes"))) |
| 139 |
remainingFridges -= self._jobSize |
| 140 |
log.info('Number of jobs for simulation: %d' % len(jobs)) |
| 141 |
|
| 142 |
# Add each job's data |
| 143 |
pSum = [0] * int((60 / self._aggSteps) * self.simEnd) |
| 144 |
for job in jobs: |
| 145 |
data = job() |
| 146 |
for i in range(len(data)): |
| 147 |
pSum[i] += data[i] |
| 148 |
for s in pSum: |
| 149 |
s /= len(jobs) |
| 150 |
|
| 151 |
log.setLevel(oldLevel) |
| 152 |
log.info('Parallel simulation finished.') |
| 153 |
return pSum |
| 154 |
|
| 155 |
def runSimulation(self, numFridges): |
| 156 |
""" |
| 157 |
Create a job with the specified number of fridges and controllers and |
| 158 |
one observer. Simulate this and return the results. |
| 159 |
|
| 160 |
@param numFridges: The number of fridges to use for this job |
| 161 |
@type numFridges: unsigned int |
| 162 |
@return: A list with the aggregated fridge consumption |
| 163 |
""" |
| 164 |
sim = SimPy.Simulation.Simulation() |
| 165 |
sim.initialize() |
| 166 |
|
| 167 |
fridgeProperties = {'tau': self._tau} |
| 168 |
fridges = [] |
| 169 |
for i in range(numFridges): |
| 170 |
fridge = processes.Fridge(sim, **fridgeProperties) |
| 171 |
fridges.append(fridge) |
| 172 |
sim.activate(fridge, fridge.run(), at = 0) |
| 173 |
observer = processes.FridgeObserver(sim, |
| 174 |
fridges, self._tau, self._aggSteps) |
| 175 |
sim.activate(observer, observer.run(), at = 0) |
| 176 |
|
| 177 |
sim.simulate(until = self.simEnd) |
| 178 |
return observer.getData() |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == '__main__': |
| 182 |
logging.basicConfig( |
| 183 |
level = logging.INFO, |
| 184 |
format = '%(asctime)s %(levelname)8s: %(name)s: %(message)s') |
| 185 |
|
| 186 |
numFridges = 5000 |
| 187 |
tau = 1./60 |
| 188 |
aggStep = 15 |
| 189 |
duration = 4 + tau |
| 190 |
|
| 191 |
sim = Simulator(numFridges, tau, aggStep, duration) |
| 192 |
data = sim.simulate() |
| 193 |
log.info('Results: ' + str(data)) |
| 194 |
|
| 195 |
servers = () |
| 196 |
sim = ParallelSimulator(numFridges, tau, aggStep, duration, 100, servers) |
| 197 |
data = sim.simulate() |
| 198 |
log.info('Results: ' + str(data)) |