def runTest(self): """ Test if addStateForExperiment works correctly """ # Since we are creating new states for experiments from the first one # the test is going to create the first state from all the others by applying # first experiment changes and then check if it produces the same state state, experiments = readFromFile('inputs/testExperiments.dat') results = createStatesFromExperiments(state, experiments) firstState, firstExperiment = results[0] for state, _ in results[1:]: state = addStateForExperiment(firstExperiment, state) #Buses buses = {} for route in state.routes: buses[route.number] = 0 buses2 = deepcopy(buses) for bus in state.buses: buses[bus.routeNr] += 1 for bus in firstState.buses: buses2[bus.routeNr] += 1 self.failUnless(buses == buses2, 'The number of buses in states are not the same: %(one)s and %(two)s' % {'one':buses, 'two':buses2}) #Capacity for bus in state.buses: for bus2 in firstState.buses: if bus.id == bus2.id: self.failUnless(bus.capacity == bus2.capacity, 'Bus capacities are not the same for buses: %(one)s and %(two)s' % {'one':bus.__dict__, 'two':bus2.__dict__}) #Roads for road in state.roads: for road2 in firstState.roads: if road.starts == road2.starts and road.ends == road2.ends: self.failUnless(road.__eq__(road2), 'Roads from %(starts)s to %(ends)s are not the same' % {'starts':road.starts, 'ends':road.ends}) #Boards rate self.failUnless(firstState.boards == state.boards, 'Board rates are not the same for states: %(one)s and %(two)s' % {'one':state.__dict__, 'two':state.__dict__}) #Disembarks rate self.failUnless(firstState.disembarks == state.disembarks, 'Disembarks rates are not the same for states: %(one)s and %(two)s' % {'one':state.__dict__, 'two':state.__dict__}) #Depart rate self.failUnless(firstState.busDeparts == state.busDeparts, 'Bus depart rates are not the same for states: %(one)s and %(two)s' % {'one':state.__dict__, 'two':state.__dict__}) #New passengers rate self.failUnless(firstState.paxArrives == state.paxArrives, 'New passenger rates are not the same for states: %(one)s and %(two)s' % {'one':state.__dict__, 'two':state.__dict__})
def simulateAll(state, experiments): ''' Simulate only once if no experiment present, otherwise simulate every possible state. If some experiment and optimise parameters present - find these optimised parameters ''' states = createStatesFromExperiments(state, experiments) if len(states) > 1: costs = [] for state, vari in states: printExperiment(vari) simulate(state, False, False) costs.append((costFunction(state.missedTotal, vari), vari)) #Find experiment that has minimum cost if states[0][0].optimise: optimised = min(costs, key=lambda item: item[0])[1] print "Optimised parameters:" printExperiment(optimised) else: if not states[0][0].optimise: simulate(states[0][0], True, False) else: print "Error: Cannot optimise parameters when there are no experiment present"
def simulateAll(state, experiments): ''' Simulate only once if no experiment present, otherwise simulate every possible state. If some experiment and optimise parameters present - find these optimised parameters ''' states = createStatesFromExperiments(state, experiments) if len(states) > 1: costs = [] for state, vari in states: printExperiment(vari) simulate(state, False, False) costs.append((costFunction(state.missedTotal, vari), vari)) #Find experiment that has minimum cost if states[0][0].optimise: optimised = min(costs, key=lambda item:item[0])[1] print "Optimised parameters:" printExperiment(optimised) else: if not states[0][0].optimise: simulate(states[0][0], True, False) else: print "Error: Cannot optimise parameters when there are no experiment present"
def runTest(self): """ Test if addStateForExperiment works correctly """ # Since we are creating new states for experiments from the first one # the test is going to create the first state from all the others by applying # first experiment changes and then check if it produces the same state state, experiments = readFromFile('inputs/testExperiments.dat') results = createStatesFromExperiments(state, experiments) firstState, firstExperiment = results[0] for state, _ in results[1:]: state = addStateForExperiment(firstExperiment, state) #Buses buses = {} for route in state.routes: buses[route.number] = 0 buses2 = deepcopy(buses) for bus in state.buses: buses[bus.routeNr] += 1 for bus in firstState.buses: buses2[bus.routeNr] += 1 self.failUnless( buses == buses2, 'The number of buses in states are not the same: %(one)s and %(two)s' % { 'one': buses, 'two': buses2 }) #Capacity for bus in state.buses: for bus2 in firstState.buses: if bus.id == bus2.id: self.failUnless( bus.capacity == bus2.capacity, 'Bus capacities are not the same for buses: %(one)s and %(two)s' % { 'one': bus.__dict__, 'two': bus2.__dict__ }) #Roads for road in state.roads: for road2 in firstState.roads: if road.starts == road2.starts and road.ends == road2.ends: self.failUnless( road.__eq__(road2), 'Roads from %(starts)s to %(ends)s are not the same' % { 'starts': road.starts, 'ends': road.ends }) #Boards rate self.failUnless( firstState.boards == state.boards, 'Board rates are not the same for states: %(one)s and %(two)s' % { 'one': state.__dict__, 'two': state.__dict__ }) #Disembarks rate self.failUnless( firstState.disembarks == state.disembarks, 'Disembarks rates are not the same for states: %(one)s and %(two)s' % { 'one': state.__dict__, 'two': state.__dict__ }) #Depart rate self.failUnless( firstState.busDeparts == state.busDeparts, 'Bus depart rates are not the same for states: %(one)s and %(two)s' % { 'one': state.__dict__, 'two': state.__dict__ }) #New passengers rate self.failUnless( firstState.paxArrives == state.paxArrives, 'New passenger rates are not the same for states: %(one)s and %(two)s' % { 'one': state.__dict__, 'two': state.__dict__ })