def simulate(self, current_time, end_time, agents, receiver_agents, agents_delay_map, contracts, delay_order,
              delay_notification):
     start_time = 0.0
     market_input_ports = ['in_agent_regulator', 'in_agent_journal']
     market_output_ports = ['out_next_journal_agent', 'out_notify_order_journal_agent', 'out_next_regulator_agent']
     orderbooks_map = {
         contract: OrderbookVersion1('ob_' + contract, contract, delay_order, delay_notification)
         for contract in contracts}
     market = Version2Exchange('market', current_time, float('inf'), orderbooks_map.values(),
                               market_input_ports, market_output_ports, agents_delay_map,
                               start_time=start_time, end_time=end_time)
     connections = [((agent.identifier, 'out_order'), (market.identifier, 'in_agent_regulator'))
                    for agent in agents]
     # Reactive agent observes the output from journal
     connections += [((market.identifier, 'out_next_journal_agent'), (agent.identifier, 'in_next'))
                     for agent in receiver_agents]
     connections += [((market.identifier, 'out_notify_order_journal_agent'), (agent.identifier, 'in_notify_order'))
                     for agent in receiver_agents]
     connections += [((market.identifier, 'out_next_regulator_agent'), (agent.identifier, 'in_notify_order'))
                     for agent in receiver_agents]
     m = SimpleSystem(market=market, agents=agents, connections=connections)
     sim = Simulator(m)
     sim.setClassicDEVS()
     sim.simulate()
     return sim
Esempio n. 2
0
    def getPerformance(self):
        # Average travel time
        l = self.collector.getTrains()
        return sum(train.getPerformance() for train in l) / len(l)


totalLengths = [5000, 10000, 15000, 20000]
segmentsCount = [5, 10, 15, 20, 25, 30]
terminationTime = 1000

for length in totalLengths:
    data = []
    fileName = "../data/results{}.dat".format(length)

    for segCnt in segmentsCount:
        system = TrainTrafficSystem(segCnt, length)
        sim = Simulator(system)
        # sim.setVerbose()
        sim.setTerminationTime(terminationTime)
        sim.setClassicDEVS()
        sim.simulate()

        c = system.getCost()
        p = system.getPerformance()
        data.append((segCnt, c, p))

    with open(fileName, "w") as f:
        f.write("segmentsCount,cost,performance\n")
        for d in data:
            f.write("{},{},{}\n".format(d[0], d[1], d[2]))
Esempio n. 3
0
from pypdevs.simulator import Simulator

from model2 import TrainNetwork


def terminate_whenStateIsReached(clock, model):
    return len(model.collector.trains) == model.generator.number_of_trains


total_railway_length = 100
num_of_segments = 1
num_of_trains = 1
max_velocity = 150
acceleration = 15
iat = 10
trainNetwork = TrainNetwork("TrainNetwork", num_of_trains, max_velocity,
                            num_of_segments, total_railway_length,
                            acceleration, iat)

sim = Simulator(trainNetwork)
sim.setTerminationCondition(terminate_whenStateIsReached)
sim.setClassicDEVS(True)
sim.setVerbose(None)
# sim.setTerminationTime(20.0)
sim.simulate()
Esempio n. 4
0
from pypdevs.simulator import Simulator

from mymodel import MyModel

model = MyModel()
simulator = Simulator(model)

simulator.setVerbose()
simulator.setClassicDEVS()

simulator.simulate()
Esempio n. 5
0
def terminate_whenStateIsReached(clock, model):
    return model.trafficLight.state.get() == "manual"
sim.setTerminationCondition(terminate_whenStateIsReached)

#    A termination time is prefered over a termination condition,
#    as it is much simpler to use.
#    e.g. to simulate until simulation time 400.0 is reached
sim.setTerminationTime(400.0)

# B. Set the use of a tracer to show what happened during the simulation run
#    Both writing to stdout or file is possible:
#    pass None for stdout, or a filename for writing to that file
sim.setVerbose(None)

# C. Use Classic DEVS instead of Parallel DEVS
#    If your model uses Classic DEVS, this configuration MUST be set as
#    otherwise errors are guaranteed to happen.
#    Without this option, events will be remapped and the select function
#    will never be called.
sim.setClassicDEVS()

#    ======================================================================

# 4. Simulate the model
sim.simulate()

#    ======================================================================

# 5. (optional) Extract data from the simulated model
print("Simulation terminated with traffic light in state %s" % (trafficSystem.trafficLight.state.get()))