def environment_setting(req): if req.method == 'POST': Environment.set(**req.data) env = Environment.one() domain = INPUT.TEXT('domain', env.domain) cidr = INPUT.TEXT('cidr', env.cidr) gateway = INPUT.TEXT('gateway', env.gateway) noserv = INPUT.TEXT('netops', env.netops) dns_ext = INPUT.TEXT('dns_ext', env.dns_ext) return DIV().html( HEAD(1, STYLE='float:left;').html('Environment'), DIV(STYLE='float:right;margin:20px 0px 10px 20px;').html( netops.context( BUTTON(CLASS='btn-primary', STYLE='height:39px;').html('Save'), 'environment_setting', domain, cidr, gateway, noserv, dns_ext)), INPUT.GROUP().html(INPUT.LABEL_TOP('Domain'), domain), INPUT.GROUP().html(INPUT.LABEL_TOP('CIDR'), cidr), INPUT.GROUP().html(INPUT.LABEL_TOP('Network'), INPUT.DISPLAY().html(env.network)), INPUT.GROUP().html(INPUT.LABEL_TOP('Prepix'), INPUT.DISPLAY().html(env.prefix)), INPUT.GROUP().html(INPUT.LABEL_TOP('Netmask'), INPUT.DISPLAY().html(env.netmask)), INPUT.GROUP().html(INPUT.LABEL_TOP('Gateway'), gateway), INPUT.GROUP().html(INPUT.LABEL_TOP('NetOps (Internal DNS & NTP)'), noserv), INPUT.GROUP().html(INPUT.LABEL_TOP('External DNS'), dns_ext))
def test_apply_action(self): ship = Ship('uid', Location(150, 100), 90, 8, 6) environment = Environment(300, 200, [ship]) action = Action(ship.uid, 0, True) environment.apply_action(action) self.assertEqual(Location(150, 94), ship.location) # moved up 6 (the ship's speed)
def test_apply_action_collide_with_ship(self): ship1 = Ship('1', Location(150, 100), 0, 8, 6) ship2 = Ship('2', Location(155, 100), 0, 8, 6) environment = Environment(300, 200, [ship1, ship2]) action = Action(ship1.uid, 0, True) with self.assertRaises(Exception): environment.apply_action(action)
def test_to_dict(self): ship1 = Ship('1', Location(1, 2), 90, 8, 6) ship2 = Ship('2', Location(1, 2), 90, 12, 4) environment = Environment(300, 200, [ship1, ship2]) expected = { 'width': 300, 'height': 200, 'ships': [ship1.to_dict(), ship2.to_dict()] } self.assertEqual(expected, environment.to_dict())
def netops_main_static_table(table, sr_id): if isinstance(sr_id, str): sr_id = int(sr_id) env = Environment.one() sr = StaticRange.get(sr_id) if sr: hosts = Host.list(Host.ip_num >= sr.stt_num, Host.ip_num <= sr.end_num) for host in hosts: sr_id = INPUT.HIDDEN('sr_id', str(sr.id)) host_id = INPUT.HIDDEN('host_id', str(host.id)) name = INPUT.TEXT('name', host.name, CLASS='page-input-in-table') mac = INPUT.TEXT('mac', host.mac, CLASS='page-input-in-table') model_list = [m for m in _host_models] model_list.remove(host.model) model_list.insert(0, host.model) model = INPUT.SELECT('model', *model_list, CLASS='page-input-in-table') serial = INPUT.TEXT('serial', host.serial, CLASS='page-input-in-table') desc = INPUT.TEXT('desc', host.desc, CLASS='page-input-in-table') submit = DIV(STYLE='width:100%;text-align:center;').html( netops.context( BUTTON(CLASS='btn-primary btn-xs', STYLE='margin:0px;padding:0px 5px;font-size:11px;'). html('Save'), 'netops_main_static_view::' + str(sr.id), sr_id, host_id, name, mac, model, serial, desc), netops.signal( BUTTON(CLASS='btn-danger btn-xs', STYLE='margin:0px;padding:0px 5px;font-size:11px;' ).html('Clear'), 'DELETE', 'netops_main_static_view::' + str(sr.id), str(sr.id), str(host.id)), sr_id, host_id) table.record(name, host.ip, mac, model, serial, desc, submit)
def train_sarsa(num_trials, num_episodes, lr, gamma, total_trials, alarm_consistency, epsilon): undiscounted_returns = np.zeros(shape=(total_trials, num_episodes)) decayed_epsilon = epsilon environment = Environment.Environment(Constants.observations_file, Constants.log_crash_prior, alarm_consistency) agent = Agent.Agent(epsilon=epsilon) for trial in xrange(num_trials[0], num_trials[1]): agent.reset(epsilon) for episode in xrange(num_episodes): # if episode % Constants.epsilon_decay_episode: # decayed_epsilon /= 2 # agent.set_epsilon(decayed_epsilon) print "Trial: %d, Episode: %d" % (trial, episode) environment.reset() current_state = environment.current_state current_action = agent.get_action(current_state) total_undiscounted_reward = 0 while False or not environment.done: next_state, reward, done = environment.step(current_action) next_action = agent.get_action(next_state) # print current_state, current_action, reward, next_state, next_action agent.sarsa_update(current_state, current_action, reward, next_state, next_action, lr, gamma) current_state = next_state current_action = next_action total_undiscounted_reward += reward undiscounted_returns[trial][episode] = total_undiscounted_reward return undiscounted_returns
def test_init(self): ship1 = Ship('1', Location(1, 2), 90, 8, 6) ship2 = Ship('2', Location(1, 2), 90, 12, 4) environment = Environment(300, 200, [ship1, ship2]) self.assertEqual(300, environment.width) self.assertEqual(200, environment.height) self.assertEqual(2, len(environment.ships)) self.assertEqual(ship1, environment.ships[0]) self.assertEqual(ship2, environment.ships[1])
def api_setEnv(req): env = Environment.set(**req.data) return { 'domain': env.domain, 'cidr': env.cidr, 'network': env.network, 'prefix': env.prefix, 'netmask': env.netmask, 'gateway': env.gateway, 'netops': env.netops, 'dns_external': env.dns_ext }
def netops_main_total_table(table): env = Environment.one() hosts = Host.list() for host in hosts: if host.name != '' and host.range_name != '' and env.domain != '': dns = '%s.%s.%s' % (host.name.replace( ' ', '-'), host.range_name.replace(' ', '-'), env.domain) else: dns = '' if host.model == 'Unknown': model = '' else: model = host.model table.record( host.name, ANCH(HREF='http://' + dns, TARGET='_blank').html(dns), '%s (%s)' % (host.range_name, host.range_type) if host.range_name != '' else host.range_name, host.ip, host.mac, model, host.serial, host.desc)
def load(stream): data = my_yaml.load(stream) if not data.has_key('nodes'): raise ConfigError, "No nodes defined" name = 'default' if data.has_key('name'): name = data['name'] environment = Environment(name) for network_data in (data.get('networks') or []): parse_network(environment, network_data) for node_data in data['nodes']: parse_node(environment, node_data) return environment
def plot2d(width, height, steps, n, decays, sigmas, strength): df = pd.DataFrame(columns=["decay", "sigma", "iteration"]) row = 0 for decay in decays: for sigma in sigmas: print(decay, sigma) env = Environment(width=width, height=height, n_colonies=1, n_ants=30, n_obstacles=10, decay=decay, sigma=sigma, moore=False, pheromone_strength=strength) iter = get_iterations(env, steps) df.loc[row] = [decay, sigma, iter] row += 1 return df
def test_apply_action_collide_with_right_wall(self): ship = Ship('uid', Location(295, 100), 0, 8, 6) environment = Environment(300, 200, [ship]) action = Action(ship.uid, 0, True) with self.assertRaises(Exception): environment.apply_action(action)
def test_from_dict(self): ship1 = Ship('1', Location(1, 2), 90, 8, 6) ship2 = Ship('2', Location(1, 2), 90, 12, 4) environment = Environment(300, 200, [ship1, ship2]) self.assertEqual(environment, Environment.from_dict(environment.to_dict()))
from model import Environment, EMS, TradingAgent from messages import Prognosis, FlexReq, FlexOffer, FlexOrder, UDIevent #from solvers import milp_solver from pulp import * import random import datetime as dt import xlwings as xw import pandas as pd import numpy as np import matplotlib.pyplot as plt ################ TEST MILP SOLVER ####################### # %% inputs = data_import("ComOpt.xlsm") if __name__ == '__main__': env = Environment(data=inputs) env.step() # %%
plt.legend(pheromone_strengths) plt.show() if __name__ == '__main__': width = 26 height = 26 steps = 1500 ant_size = 0.4 n = 40 pheromone_strengths = np.linspace(0.7, 2.7, num=n) loops = 30 total = [] for value in pheromone_strengths: averages = [] env = Environment(width=width, height=height, n_colonies=1, n_ants=30, n_obstacles=20, decay=0.99, sigma=0.12, moore=False, pheromone_strength=value) nr_iterations = get_iterations(env, steps) total.append(nr_iterations) with open('data/plot_runs.pkl', 'wb') as f: pickle.dump(total, f)
plt.xlabel('iteration') plt.ylabel(col) df[col].plot() def animate_distribution(path_lengths, steps): fig = plt.figure() ax = fig.add_subplot(111) for i in range(steps): ax.clear() t_data = path_lengths.loc[i] t_data = t_data[t_data != np.inf] t_data.hist(ax=ax) plt.pause(0.001) if __name__ == '__main__': decay, sigma, strength = parser() env = Environment(width=WIDTH, height=HEIGHT, n_colonies=1, n_ants=40, n_obstacles=30, decay=decay, sigma=sigma, moore=False, pheromone_strength=strength) plot_continuous(env, STEPS)
from model import Environment model = Environment(25, .4, True, .5, .33, 30) for i in range(50): model.step()
ax = fig.add_subplot(111) for i in range(steps): ax.clear() t_data = path_lengths.loc[i] t_data = t_data[t_data != np.inf] t_data.hist(ax=ax) plt.pause(0.001) if __name__ == '__main__': width = 20 height = 20 steps = 10000 ant_size = 0.4 # var_params = {"n_ants": range(20, 22), 'sigma': np.arange(0, 1, 0.2)} # fixed_params = {"width": width, "height": height, "n_colonies": 1, # "n_obstacles": 0, "decay": 0.99, "moore": False} # batch_run = BatchRunner(Environment, variable_parameters=var_params, fixed_parameters=fixed_params, max_steps=100, # model_reporters={"n_agents": lambda m: m.schedule.get_agent_count()}, iterations=5) # batch_run.run_all() # print(batch_run.get_model_vars_dataframe()) env = Environment(width=width, height=height, n_colonies=1, n_ants=100, n_obstacles=0, decay=0.99, sigma=0.2, moore=False) plot_continuous(env, steps) # compute_then_plot(env, steps) # compute_no_plot(env, steps=steps) # model_data = env.datacollector.get_model_vars_dataframe() # agent_min_paths = env.datacollector.get_agent_vars_dataframe() # plot_col(model_data, 'Mean minimum path length')