class SimDetect(Detect): """ - net_desc: description of the network - norm_desc: description of the normal case - ano_list: list of anomalies. """ def __init__(self, argv): Detect.__init__(self, argv) self.sim = Sim(argv) self.output_flow_file = self.ROOT + \ '/Simulator/n%i_flow.txt'%(self.args.detect_node_id) self.args.data = self.output_flow_file self.export_abnormal_flow_file = self.ROOT + \ '/Simulator/abnormal_n%i_flow.txt'%(self.args.detect_node_id) def init_parser(self, parser): Detect.init_parser(self, parser) parser.add_argument('--detect_node_id', default=0, type=int, help = """ specify the node id you want to monitor, default value is 0""") parser.add_argument('--no_sim', default=False, action='store_true', help = """turn on this switch to disable the fs simulaiton""") def run(self): if not self.args.no_sim: self.sim.run() Detect.run(self) return self.detector
class SimDetect(Detect): """ - net_desc: description of the network - norm_desc: description of the normal case - ano_list: list of anomalies. """ def __init__(self, argv): Detect.__init__(self, argv) self.sim = Sim(argv) self.output_flow_file = self.ROOT + \ '/Simulator/n%i_flow.txt'%(self.args.detect_node_id) self.args.data = self.output_flow_file self.export_abnormal_flow_file = self.ROOT + \ '/Simulator/abnormal_n%i_flow.txt'%(self.args.detect_node_id) def init_parser(self, parser): Detect.init_parser(self, parser) parser.add_argument('--detect_node_id', default=0, type=int, help=""" specify the node id you want to monitor, default value is 0""") parser.add_argument( '--no_sim', default=False, action='store_true', help="""turn on this switch to disable the fs simulaiton""") def run(self): if not self.args.no_sim: self.sim.run() Detect.run(self) return self.detector
def replay(): print('dqn_setting') sess = tf.Session() game = Sim(SCREEN_WIDTH, SCREEN_HEIGHT, show_game=True) brain = DQN(sess, VIEW_WIDTH, VIEW_HEIGHT, NUM_ACTION) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state('model') saver.restore(sess, ckpt.model_checkpoint_path) # start game for episode in range(MAX_EPISODE): terminal = False total_reward = 0 state = game.Reset() brain.init_state(state) while not terminal: action = brain.get_action() print('action_choice : ' + str(action)) # get data state, reward, terminal = game.Update(action) total_reward += reward brain.remember(state, action, reward, terminal) # show the play time.sleep(10) print('Number of game: %d Score: %d' % (episode + 1, total_reward))
def train_evaluate(search_params): hyperparameters = {} pick_kwargs = {} for k in list(search_params.keys()): if k in ['w_dfh', 'w_sharpe', 'w_100d', 'v_100d', 'v_dfh', 'v_rfl']: pick_kwargs[k] = search_params[k] else: hyperparameters[k] = search_params[k] hyperparameters['pick_kwargs'] = pick_kwargs print('------------') print(json.dumps(hyperparameters, indent=2, sort_keys=True)) sim = Sim(neptune=neptune, period='2y', timedelay=100, window=100, timestep=1, budget=5000, stockPicks=5, avoidDowntrends=True, sellAllOnCrash=False, **hyperparameters) stats = sim.run() analysis = Analysis(neptune=neptune, stats=stats, positions=sim.portfolio.holdings, prices=sim.downloader.prices) #analysis.chart() output, advanced_stats, obj_stats = analysis.positionStats() for k in list(obj_stats.keys()): neptune.log_metric(k, obj_stats[k]) print(output) #neptune.log_artifact('data/output_1y.pkl') sharpe = analysis.sharpe() stats = sim.portfolio.summary() if math.isnan(sharpe) or math.isinf(sharpe) or sharpe <= -2 or sharpe >= 5: sharpe = -5 #neptune.log_metric('sharpe', sharpe) #neptune.log_metric('start_value', 5000) #neptune.log_metric('end_value', stats['total_value']) report = { 'hyperparameters': hyperparameters, 'sharpe': sharpe, 'end_value': stats['total_value'], 'gains': (stats['total_value'] - 5000.0) / 5000.0 } neptune.log_text('report', json.dumps(report, indent=2, sort_keys=True)) return sharpe
def __init__(self, argv): Detect.__init__(self, argv) self.sim = Sim(argv) self.output_flow_file = self.ROOT + \ '/Simulator/n%i_flow.txt'%(self.args.detect_node_id) self.args.data = self.output_flow_file self.export_abnormal_flow_file = self.ROOT + \ '/Simulator/abnormal_n%i_flow.txt'%(self.args.detect_node_id)
def L(p, pmt, q, ps, ls, ID): global t0, t1 Ma = p[0] Sa = p[1] lamb = p[2] if np.any(p <= 0): return 1e10 * (1 - np.amin(p)) S, A = Sim(Ma, Sa, lamb, th[pmt], Abins, DCspec[pmt], DCAreas[pmt]) l = 0 # # data=spec[pmt] # model=S*np.sum(spec[pmt]) # l-=np.sum(data*np.log((model+1e-10)/(data+1e-10))+data-model) bool = 0.5 * (Abins[1:] + Abins[:-1]) > th[pmt] data = np.sum(rec['Areas'][:, pmt], axis=0)[bool] model = A[bool] * np.sum(np.sum(rec['Areas'][:, pmt], axis=0)[bool]) l -= np.sum(data * np.log((model + 1e-10) / (data + 1e-10)) + data - model) ps[len(ls)] = p ls.append(l) print('PMT: ', pmt, 'ID: ', ID) print(len(ls), time.time() - t1, l) t1 = time.time() np.savez('Q{}_{}'.format(pmt, ID), param=q, ps=ps, ls=ls, T=time.time() - t0, note=note) return l
def __init__(self): root = Tk() # Creating the simulation self.sim = Sim() # Creating the parameter input panel self.input_panel = InputPanel(root, self.sim) self.input_panel.input_panel.grid(row=0, column=1) # Creating the grain input panel self.grain_panel = GrainPanel(root, self.sim) self.grain_panel.grain_panel.grid(row=0) #Creating the results panel self.results_panel = ResultsPanel(self.sim, root) self.results_panel.results_panel.grid(row=1, column=1) # Configuring the menubar self.menubar = Menu(root) root.config(menu=self.menubar) self.filemenu = Menu(self.menubar, tearoff=0) self.filemenu.add_command(label="New", command=self.new_file) self.filemenu.add_command(label="Open", command=self.open_file) # self.filemenu.add_command(label="Open Propellant", command=self.open_prop_file) self.filemenu.add_command(label="Save", command=self.save_file) self.menubar.add_cascade(label="File", menu=self.filemenu) # Creating the calculate button calc = Button(root, text="Calculate", command=self.plot_sim) calc.grid(row=2, column=1) # Creating the figure to plot pressure and force self.figure = plt.Figure(figsize=(10, 5)) self.chart = FigureCanvasTkAgg(self.figure, root) self.chart.show() self.chart.get_tk_widget().grid(row=1, column=0) root.iconbitmap('rocketry_icon.ico') root.title("Olin Rocketry Burn Simulator") self.is_saved = FALSE self.update_val() root.mainloop()
def main(): first_seen = [] random = [] sim_time = 100 # N, P, R for i in range(sim_time): sr = Sim(100, 1, 100, "r") sf = Sim(100, 1, 100, "f") sr_res = sr.run() sf_res = sf.run() # print("random: ", sr_res) # print("first_seen: ", sf_res) first_seen.append(sf_res) random.append(sr_res) print("round: ", i) # print("first seen: ", sum(first_seen) / len(first_seen)) # print(first_seen) # print("random: ", sum(random) / len(random)) # print(random) print(first_seen) print(random) plt.scatter(range(sim_time), first_seen, color='red') plt.hlines(sum(first_seen) / len(first_seen), 0, sim_time, colors='red') plt.scatter(range(sim_time), random, color='blue') plt.hlines(sum(random) / len(random), 0, sim_time, colors='blue') plt.show()
def L(p, pmt, q, ps, ls, ID): global t0 T=p[:6] St=p[6:] if np.any(St<=0): return 1e10*(1-np.amin(p)) S=Sim(T, St, bins) if np.any(np.isnan(S)): return 1e10*np.amax(np.abs(T)) l=0 for i in range(len(T)): data=H[i, np.argmax(H[i])-3:np.argmax(H[i])+4] model=S[i, np.argmax(H[i])-3:np.argmax(H[i])+4]*np.amax(H[i]) l-=np.sum(data*np.log((model+1e-10)/(data+1e-10))+data-model) ps[len(ls)]=p ls.append(l) print('PMT: ', pmt, 'ID: ', ID) print(len(ls), l) np.savez('Q{}_{}'.format(pmt, ID), param=q, ps=ps, ls=ls, T=time.time()-t0, note=note) return l
from Sim import Sim s = Sim(1000)
def train(): print('dqn_setting') sess = tf.Session() game = Sim(SCREEN_WIDTH, SCREEN_HEIGHT) brain = DQN(sess, VIEW_WIDTH, VIEW_HEIGHT, NUM_ACTION) rewards = tf.placeholder(tf.float32, [None]) tf.summary.scalar('avg.reward/ep.', tf.reduce_mean(rewards)) saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('logs', sess.graph) summary_merged = tf.summary.merge_all() # initialize target network brain.update_target_network() # decide action decision time using dqn after this time epsilon = 1.0 # number of frames time_step = 0 total_reward_list = [] # Starting for episode in range(MAX_EPISODE): terminal = False total_reward = 0 # gmae reset and get state (=poisition data) state = game.Reset() brain.init_state(state) # set state of dqn while not terminal: # choice random action when epsilon < random var, else ( :epilon > random var), choice with dqn # Later time, unless use random action if np.random.rand() < epsilon: action = game.get_action() else: action = brain.get_action() # epsion decrease if episode > OBSERVE: epsilon -= 1 / 1000 # game updates, get data (state / reward / is_gameover) state, reward, terminal = game.Update(action) total_reward += reward # brain save this state brain.remember(state, action, reward, terminal) # After little time, In interval, do training if time_step > OBSERVE and time_step % TRAIN_INTERVAL == 0: brain.train() # In interval, do training if time_step % TARGET_UPDATE_INTERVAL == 0: brain.update_target_network() time_step += 1 print('Number of game: %d Score: %d' % (episode + 1, total_reward)) f = open('ac.txt', 'rw') f.write('Number of game: %d Score: %d ::\n' % (episode + 1, total_reward)) f.close() total_reward_list.append(total_reward) if episode % 10 == 0: summary = sess.run(summary_merged, feed_dict={rewards: total_reward_list}) writer.add_summary(summary, time_step) total_reward_list = [] if episode % 100 == 0: saver.save(sess, 'model/dqn.ckpt', global_step=time_step)
bins=np.arange(6) - 0.5) th = np.array([0.45, 0.9, 1, 0.7, 0.7, 0.55]) p = [0.82839096, 0.38661561, 0.27190796] Ma = p[0] Sa = p[1] q = p[2] iter = 10 S = np.zeros((iter, len(spec[chn]))) A = np.zeros((iter, len(DCAreas[chn]))) for j in range(iter): print(j) S[j], A[j] = Sim(Ma, Sa, q, th[chn], Abins, DCspec[chn], DCAreas[chn]) l = 0 bool = 0.5 * (Abins[1:] + Abins[:-1]) > th[chn] data = np.sum(rec['Areas'][:, chn], axis=0)[bool] model = A[j, bool] * np.sum(np.sum(rec['Areas'][:, chn], axis=0)[bool]) l -= np.sum(data * np.log((model + 1e-10) / (data + 1e-10)) + data - model) print(l) fig, ax = plt.subplots(1) np.ravel(ax)[0].step(0.5 * (bins[1:] + bins[:-1]), spec[chn], where='mid', label='PMT{}'.format(i), linewidth=3) np.ravel(ax)[0].step(0.5 * (bins[1:] + bins[:-1]), DCspec[chn],
# task_request = 100 # getWorkload(task_request) placement_schedulers = [FirstFit(),Multi_resource_alignment(),TBF(),EAGLE(),EAGLE_MOD()] # placement_schedulers = [EAGLE(), Multi_resource_alignment()] # placement_schedulers = [FirstFit()] # placement_schedulers = [Multi_resource_alignment()] # placement_schedulers = [EAGLE_MOD()] # placement_schedulers = [FirstFit_disc rd(),Multi_resource_alignment_discard()] for placement in placement_schedulers: # taskEvents = getTaskSubmissionEventsFromFile("Workloads/Workload_Traces_test") # taskEvents = getTaskSubmissionEventsFromFile("Workloads/Actual_Workload_Traces") taskEvents = getTaskSubmissionEventsFromFile("Workloads/Workload_Traces_Backlogged") # taskEvents = getTaskSubmissionEventsFromFile("Workloads/Workload_Traces_OnebyOne") # taskEvents = getTaskSubmissionEventsFromFile("Workloads/Workload_Traces_Small") print("Now using %s ..." % placement) placement.cluster = cluster placement.tasks = taskEvents[:] sim = Sim(placement) sim.placeTasks() for machine in cluster: machine.cleanMachine() print("simulation finished...")
#!/usr/bin/python import StatsReader from Sim import Sim from config import * def processEventLog(log): """ Given an event.log file produced by the Monitor, processEventLog() will read in the events line-by-line and """ pass xr = StatsReader.StatsReader(goodFile) s = Sim() stList = xr.extractSourceTracks() s.pushTracks(stList) systList = xr.extractSystemTracks() s.pushTracks(systList) s.listTracks()
import numpy as np import networkx as nx import matplotlib.pyplot as plt import sys sys.path.append("/Users/lisiyu/Desktop/Network Generation/SingleOD") from InitWorld import InitWorld from Ant import Ant from Sim import Sim from pyshow import pyshow ###################Simulation Control Variables##################### X = 80 Y = 80 origin = (10, 10) destination = [(70, 61), (20, 40), (50, 20), (30, 70)] richness = [8, 2, 5, 7] screen_size = (600, 600) title = "Ant Viz" color = (0, 250, 150) d_rate = 0.95 ############################END##################################### G, pos = InitWorld(length=X, height=Y) X = pyshow(screen_size, title, color, G, origin, destination, richness) N = Sim(1500, origin, destination, richness, G, X, d_rate) #execfile("vidcap.py") #Sim()
def Main(): if len(sys.argv) <= 1: print("Argument required: create, watch, buy, sell, stats") else: if sys.argv[1] == 'create': if len(sys.argv) < 3: print("Invalid arguments. `python main.py create 5000`") else: watcher = Watcher(filename='data/portfolio', hyperparameters=hyperparameters) watcher.create(balance=float(sys.argv[2])) elif sys.argv[1] == 'watch': watcher = Watcher(filename='data/portfolio', hyperparameters=hyperparameters) watcher.start() elif sys.argv[1] == 'buy': # main.py buy 20 AMD 81.26 if len(sys.argv) < 5: print("Invalid arguments. `python main.py buy 20 AMD 81.26`") else: executor = Executor(filename='data/portfolio', hyperparameters=hyperparameters) print( executor.buy(symbol=sys.argv[3], count=int(sys.argv[2]), value=float(sys.argv[4]))) elif sys.argv[1] == 'sell': # main.py sell 20 AMD 81.26 if len(sys.argv) < 5: print("Invalid arguments. `python main.py sell 20 AMD 81.26`") else: executor = Executor(filename='data/portfolio', hyperparameters=hyperparameters) print( executor.sell(symbol=sys.argv[3], count=int(sys.argv[2]), value=float(sys.argv[4]))) elif sys.argv[1] == 'stats': watcher = Watcher(filename='data/portfolio', hyperparameters=hyperparameters) watcher.stats() elif sys.argv[1] == 'sim': from Sim import Sim from Analysis import Analysis sim = Sim(period='3y', timedelay=100, window=100, timestep=1, budget=5000, stockPicks=5, avoidDowntrends=True, sellAllOnCrash=False, **hyperparameters) stats = sim.run() analysis = Analysis(stats=stats, positions=sim.portfolio.holdings, prices=sim.downloader.prices) analysis.chart('data/best_optimized_3y.png') output, advanced_stats = analysis.positionStats() print(output) g = sim.portfolio.holdings.copy().groupby('label').sum() g['profits_pct'] = (g['current_price'] - g['purchase_price'] ) / g['purchase_price'] * 100 print(g) elif sys.argv[1] == 'check': watcher = Watcher(filename='data/portfolio', hyperparameters=hyperparameters) watcher.check(sys.argv[2]) else: print("Unknown command")
import json import signal import time from std_msgs.msg import String, Float64 from sensor_dist_ros.msg import floatArray as floatArray from sensor_dist_ros.msg import floatArray2 as floatArray2 from sensor_dist_ros.msg import sens as Data from Cloud import Cloud from Sim import Sim from BoundedVoronoi import BoundedVoronoi global bigS, bounding_box, simVoronoi, sim, points, BigC bounding_box = np.array([0, 10, 0, 10]) soft_bounding_box = np.array([0, 10, 0, 10]) simVoronoi = BoundedVoronoi(None, bounding_box) sim = Sim(simVoronoi, bounding_box, soft_bounding_box) points = np.zeros((4, 2)) BigC = Cloud(sim) count = 0 def robot0Callback(data): global points, BigC BigC.updateData([data.x, data.y, data.sensor]) points[0, :] = np.array(([data.x, data.y])) def robot1Callback(data): global points, BigC BigC.updateData([data.x, data.y, data.sensor]) points[1, :] = np.array(([data.x, data.y]))
import matplotlib.pyplot as plt import pandas as pd pd.set_option('display.max_columns', None) pd.set_option('display.expand_frame_repr', False) pd.set_option('max_colwidth', None) from Sim import Sim from Analysis import Analysis sim = Sim(period='1y', timedelay=100, window=100, timestep=1, budget=10000, stockPicks=5, sl=-0.04, tp=3.0, ts=0.05, ts_threshold=0.05, avoidDowntrends=True, sellAllOnCrash=False) stats = sim.run() analysis = Analysis(stats=stats, positions=sim.portfolio.holdings, prices=sim.downloader.prices) analysis.chart('data/output_1y.png') output, advanced_stats = analysis.positionStats() print(output) g = sim.portfolio.holdings.copy().groupby('label').sum()
DC = data['DC'] bins = data['BinsDelay'] names = data['names'] p = [ 0., -2.49534478, -2.54319998, -0.84819275, -1.95004707, -2.40912782, 0.78272572, 0.91402686, 0.90760073, 0.70031788, 0.83687314, 0.88971754 ] T = p[:6] St = p[6:] N = 10 S = np.zeros((N, 15, len(bins) - 1)) for i in range(N): print(i) S[i] = Sim(T, St, bins) fig, ax = plt.subplots(3, 5, figsize=(20, 10)) x = 0.5 * (bins[1:] + bins[:-1]) for i in range(15): np.ravel(ax)[i].step(x, H[i], where='mid', label='Ph') np.ravel(ax)[i].step(x, DC[i], where='mid', label='DC') np.ravel(ax)[i].legend() np.ravel(ax)[i].errorbar( 0.5 * (bins[1:] + bins[:-1])[np.argmax(H[i]) - 3:np.argmax(H[i]) + 4], np.mean(S[:, i, np.argmax(H[i]) - 3:np.argmax(H[i]) + 4], axis=0) * np.amax(H[i]), np.std(S[:, i, np.argmax(H[i]) - 3:np.argmax(H[i]) + 4], axis=0) * np.amax(H[i]), fmt='.', color='k')
class Main: """The main window for the program""" def __init__(self): root = Tk() # Creating the simulation self.sim = Sim() # Creating the parameter input panel self.input_panel = InputPanel(root, self.sim) self.input_panel.input_panel.grid(row=0, column=1) # Creating the grain input panel self.grain_panel = GrainPanel(root, self.sim) self.grain_panel.grain_panel.grid(row=0) #Creating the results panel self.results_panel = ResultsPanel(self.sim, root) self.results_panel.results_panel.grid(row=1, column=1) # Configuring the menubar self.menubar = Menu(root) root.config(menu=self.menubar) self.filemenu = Menu(self.menubar, tearoff=0) self.filemenu.add_command(label="New", command=self.new_file) self.filemenu.add_command(label="Open", command=self.open_file) # self.filemenu.add_command(label="Open Propellant", command=self.open_prop_file) self.filemenu.add_command(label="Save", command=self.save_file) self.menubar.add_cascade(label="File", menu=self.filemenu) # Creating the calculate button calc = Button(root, text="Calculate", command=self.plot_sim) calc.grid(row=2, column=1) # Creating the figure to plot pressure and force self.figure = plt.Figure(figsize=(10, 5)) self.chart = FigureCanvasTkAgg(self.figure, root) self.chart.show() self.chart.get_tk_widget().grid(row=1, column=0) root.iconbitmap('rocketry_icon.ico') root.title("Olin Rocketry Burn Simulator") self.is_saved = FALSE self.update_val() root.mainloop() def open_file(self): """Opens the selected file and updates the sim parameters and displayed parameters""" filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file", filetypes=(("Text files", "*.txt"), ("all files", "*.*"))) # Update the sim parameters self.sim.open_file(filename) # Update the displayed values in the inputs self.input_panel.update_inputs() self.grain_panel.update_inputs() self.is_saved = TRUE def open_prop_file(self): """Opens the selected .bsx file and updates the sim parameters. Unused due to inconsistencies with how ProPep3 saves .bsx files""" filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file", filetypes=(("ProPep3 Files", "*.bsx"), ("all files", "*.*"))) # Update the sim parameters self.sim.open_bsx_file(filename) # Update the displayed values in the inputs self.input_panel.update_inputs() self.grain_panel.update_inputs() def update_val(self): """Updates the sim parameters from the inputs""" # Get and update the sim values for key, value in self.input_panel.inputs.items(): if float( value.get() ) <= 0: # Check if any of the inputs are <= 0. If so, send error popup messagebox.showerror("Error", "Values must be greater than 0") value.delete(0, END) value.insert(0, 1) self.sim.values[key] = 1 else: self.sim.values[key] = float(value.get()) # Update the engine parameters self.sim.engine.update(self.sim.values["ri"], self.sim.values["ro"], self.sim.values["l"], self.sim.values["rt"], self.sim.values["re"]) # Get and update the propellant grains self.grain_panel.update_sim_vals() self.is_saved = FALSE def plot_sim(self): """Runs the simulation, calculates engine attributes, and plots the result""" # Updates sim parameters before running sim self.update_val() # Runs simulation and calculates thrust self.sim.run_sim_grains() self.sim.calc_thrust() # Clears figure and plots pressure and force self.figure.clf() pres_plot = self.figure.add_subplot(121) pres_plot.plot(self.sim.tspan, self.sim.p) force_plot = self.figure.add_subplot(122) force_plot.plot(self.sim.tspan, self.sim.F) self.chart.show() # Displays results (total impulse, specific impulse, etc) self.results_panel.update_vals() def save_file(self): """Saves the current simulation file""" filename = filedialog.asksaveasfilename( initialdir=os.getcwd(), title="Select file", filetypes=(("text files", "*.txt"), ("all files", "*.*"))) if filename != '': f = open(filename, 'w') # Save the sim parameters for key, value in self.sim.values.items(): f.write(str(key) + " = " + str(value) + "\n") # Save the propellant grain dimensions f.write("grain\n") for grain in self.sim.engine.grains: f.write(str(grain.ri) + "," + str(grain.l) + "\n") f.close() self.is_saved = TRUE def new_file(self): """Resets all parameters""" if not self.is_saved: if messagebox.askyesno("Unsaved simulation!" "Would you like to save?"): self.save_file() for i in self.input_panel.inputs: self.input_panel.inputs[i].delete(0, END) self.input_panel.inputs[i].insert(0, 1) self.update_val() self.is_saved = FALSE