def main(): args = parse_args() metrics.reset() start_time = time.time() logging.basicConfig(level=logging.INFO) # Create a SimPy environment env = simpy.Environment() # Seed the random generator random.seed(args.seed) numpy.random.seed(args.seed) # Parse network and get NetworkX object and ingress network list network, ing_nodes = reader.read_network(args.network, node_cap=10, link_cap=10) # Getting current SFC list, and the SF list of each SFC, and config # use dummy placement and schedule for running simulator without algorithm # TODO: make configurable via CLI sf_placement = dummy_data.triangle_placement schedule = dummy_data.triangle_schedule # Getting current SFC list, and the SF list of each SFC, and config sfc_list = reader.get_sfc(args.sf) sf_list = reader.get_sf(args.sf, args.sfr) config = reader.get_config(args.config) # Create the simulator parameters object with the provided args params = SimulatorParams(network, ing_nodes, sfc_list, sf_list, config, args.seed, sf_placement=sf_placement, schedule=schedule) log.info(params) if args.trace: trace = reader.get_trace(args.trace) TraceProcessor(params, env, trace) # Create a FlowSimulator object, pass the SimPy environment and params objects simulator = FlowSimulator(env, params) # Start the simulation simulator.start() # Run the simpy environment for the specified duration env.run(until=args.duration) # Record endtime and running_time metrics end_time = time.time() metrics.running_time(start_time, end_time) # dump all metrics log.info(metrics.metrics)
def __init__(self, network_file, service_functions_file, config_file, resource_functions_path="", test_mode=False, test_dir=None): super().__init__(test_mode) # Number of time the simulator has run. Necessary to correctly calculate env run time of apply function self.network_file = network_file self.test_dir = test_dir # init network, sfc, sf, and config files self.network, self.ing_nodes, self.eg_nodes = reader.read_network( self.network_file) self.sfc_list = reader.get_sfc(service_functions_file) self.sf_list = self.get_sf(service_functions_file) self.config = reader.get_config(config_file) # Assume result path is the path where network file is in. self.result_base_path = os.path.dirname(self.network_file) if 'trace_path' in self.config: # Quick solution to copy trace file to same path for network file as provided by calling algo. trace_path = os.path.join(os.getcwd(), self.config['trace_path']) copyfile( trace_path, os.path.join(self.result_base_path, os.path.basename(trace_path))) self.prediction = False # Check if future ingress traffic setting is enabled if 'future_traffic' in self.config and self.config['future_traffic']: self.prediction = True write_schedule = False if 'write_schedule' in self.config and self.config['write_schedule']: write_schedule = True write_flow_actions = False if 'write_flow_actions' in self.config and self.config[ 'write_flow_actions']: write_flow_actions = True # Create CSV writer self.writer = ResultWriter(self.test_mode, self.test_dir, write_schedule, write_flow_actions) self.episode = 0 self.last_apply_time = None # Load trace file if 'trace_path' in self.config: trace_path = os.path.join(os.getcwd(), self.config['trace_path']) self.trace = reader.get_trace(trace_path) #TODO: # Create a simulator runner, which take all the parameters in and hold them, process them # Interact and store the result in metrics. self.param = SimulatorParam(config=self.config, network=self.network, ing_nodes=self.ing_nodes, eg_nodes=self.eg_nodes, sfc_list=self.sfc_list, sf_list=self.sf_list)
def main(): # parse CLI args (when using simulator as stand-alone, not triggered through the interface) parser = argparse.ArgumentParser(description="Trainer tool for LSTM prediction for Coord-sim simulator") parser.add_argument('-c', '--config', required=True, dest="sim_config", help="The simulator config file") parser.add_argument('-p', '--plot', action="store_true") args = parser.parse_args() print("Loading arguments") sim_config = reader.get_config(args.sim_config) trace = reader.get_trace(sim_config['trace_path']) dest_dir = sim_config['lstm_weights'] params = SimConfig(sim_config) print(f"Loaded trace with {len(trace)} entries") predictor = LSTM_Predictor(trace, params) print("Training LSTM model") predictor.train_model() print(f"Saving model to {dest_dir}") predictor.save_model(dest_dir) del predictor print("Load weights to test prediction") predictor = LSTM_Predictor(trace, params=params, weights_dir=dest_dir) predictions = [] for test in predictor.requested_traffic: value = test predictions.append(predictor.predict_traffic(value)) if args.plot: matplotlib.use('TkAgg') pyplot.plot(predictor.requested_traffic, label="Traffic data") pyplot.plot(predictions, label="Predictions") pyplot.legend() pyplot.show() print("Done with no errors!")
def init(self, network_file, service_functions_file, config_file, seed, trace=None, resource_functions_path=""): # Initialize metrics, record start time metrics.reset() self.run_times = int(1) self.start_time = time.time() # Parse network and SFC + SF file self.network, self.ing_nodes = reader.read_network(network_file, node_cap=10, link_cap=10) self.sfc_list = reader.get_sfc(service_functions_file) self.sf_list = reader.get_sf(service_functions_file, resource_functions_path) self.config = reader.get_config(config_file) # Generate SimPy simulation environment self.env = simpy.Environment() # Instantiate the parameter object for the simulator. self.params = SimulatorParams(self.network, self.ing_nodes, self.sfc_list, self.sf_list, self.config, seed) # Trace handling if trace: trace = reader.get_trace(trace) TraceProcessor(self.params, self.env, trace) self.duration = self.params.run_duration # Get and plant random seed self.seed = seed random.seed(self.seed) numpy.random.seed(self.seed) # Instantiate a simulator object, pass the environment and params self.simulator = FlowSimulator(self.env, self.params) # Start the simulator self.simulator.start() # Run the environment for one step to get initial stats. self.env.step() # Parse the NetworkX object into a dict format specified in SimulatorState. This is done to account # for changing node remaining capacities. # Also, parse the network stats and prepare it in SimulatorState format. self.parse_network() self.network_metrics() # Record end time and running time metrics self.end_time = time.time() metrics.running_time(self.start_time, self.end_time) simulator_state = SimulatorState(self.network_dict, self.simulator.params.sf_placement, self.sfc_list, self.sf_list, self.traffic, self.network_stats) # self.writer.write_state_results(self.env, simulator_state) return simulator_state
def init(self, seed): # reset network caps and available SFs: reader.reset_cap(self.network) # Initialize metrics, record start time metrics.reset_metrics() self.run_times = int(1) self.start_time = time.time() # Parse network and SFC + SF file # Generate SimPy simulation environment self.env = simpy.Environment() self.params = SimulatorParams(self.network, self.ing_nodes, self.sfc_list, self.sf_list, self.config) # Instantiate the parameter object for the simulator. if self.params.use_states and 'trace_path' in self.config: logger.warning( 'Two state model and traces are both activated, thi will cause unexpected behaviour!' ) if self.params.use_states: if self.params.in_init_state: self.params.in_init_state = False else: self.params.update_state() self.duration = self.params.run_duration # Get and plant random seed self.seed = seed random.seed(self.seed) numpy.random.seed(self.seed) # Instantiate a simulator object, pass the environment and params self.simulator = FlowSimulator(self.env, self.params) # Start the simulator self.simulator.start() # Trace handling if 'trace_path' in self.config: trace_path = os.path.join(os.getcwd(), self.config['trace_path']) trace = reader.get_trace(trace_path) TraceProcessor(self.params, self.env, trace, self.simulator) # Run the environment for one step to get initial stats. self.env.step() # Parse the NetworkX object into a dict format specified in SimulatorState. This is done to account # for changing node remaining capacities. # Also, parse the network stats and prepare it in SimulatorState format. self.parse_network() self.network_metrics() # Record end time and running time metrics self.end_time = time.time() metrics.running_time(self.start_time, self.end_time) simulator_state = SimulatorState(self.network_dict, self.simulator.params.sf_placement, self.sfc_list, self.sf_list, self.traffic, self.network_stats) logger.debug(f"t={self.env.now}: {simulator_state}") return simulator_state
def init(self, seed): # Reset predictor class at beginning of every init if self.prediction: self.predictor = TrafficPredictor(self.params) # increment episode count self.episode += 1 # reset network caps and available SFs: reader.reset_cap(self.network) # Initialize metrics, record start time self.run_times = int(1) self.start_time = time.time() # Generate SimPy simulation environment self.env = simpy.Environment() self.params.metrics.reset_metrics() # Instantiate the parameter object for the simulator. if self.params.use_states and 'trace_path' in self.config: logger.warning( 'Two state model and traces are both activated, thi will cause unexpected behaviour!' ) if self.params.use_states: if self.params.in_init_state: self.params.in_init_state = False else: self.params.update_state() self.duration = self.params.run_duration # Get and plant random seed self.seed = seed random.seed(self.seed) numpy.random.seed(self.seed) self.params.reset_flow_lists() # generate flow lists 1x here since we are in `init()` self.params.generate_flow_lists() # Instantiate a simulator object, pass the environment and params self.simulator = FlowSimulator(self.env, self.params) # Trace handling if 'trace_path' in self.config: trace_path = os.path.join(os.getcwd(), self.config['trace_path']) trace = reader.get_trace(trace_path) TraceProcessor(self.params, self.env, trace, self.simulator) # Start the simulator self.simulator.start() # Run the environment for one step to get initial stats. self.env.step() # Parse the NetworkX object into a dict format specified in SimulatorState. This is done to account # for changing node remaining capacities. # Also, parse the network stats and prepare it in SimulatorState format. self.parse_network() self.network_metrics() # Record end time and running time metrics self.end_time = time.time() self.params.metrics.running_time(self.start_time, self.end_time) # Check to see if traffic prediction is enabled to provide future traffic not current traffic if self.prediction: self.predictor.predict_traffic(self.env.now) stats = self.params.metrics.get_metrics() self.traffic = stats['run_total_requested_traffic'] simulator_state = SimulatorState(self.network_dict, self.simulator.params.sf_placement, self.sfc_list, self.sf_list, self.traffic, self.network_stats) logger.debug(f"t={self.env.now}: {simulator_state}") # Check to see if init called in warmup, if so, set warmup to false # This is to allow for better prediction and better overall control # in the future return simulator_state