def test_given_environment(self): """Check if the user gives a separate environment that we don't use the simulation-wide one.""" enviro = environment.Environment(initial_ambient_temperature=10000, initial_inlet_temperature=10000) self.assertNotEqual(enviro, environment.environment()) heater = ewh.ElectricWaterHeater(environment=enviro) self.assertEqual(heater.environment, enviro) self.assertNotEqual(heater.environment, environment.environment())
def main(): p1 = agent() p2 = agent() allEnvs = generateAllStates() p1V = initializeV(allEnvs, 1) p1.setV(p1V) p1.setSymbol(1) p2V = initializeV(allEnvs, 2) p2.setV(p2V) p2.setSymbol(2) for i in range(1000): if i % 200 == 0: print(i) playGame(p1, p2, environment(), 0) hum = human() hum.setSymbol(2) p1.setVerbose(True) while True: playGame(p1, hum, environment(), 2) print("Game Over\n\n\n")
def test_environment_singleton(self): default_env = environment.Environment() singleton = environment.environment() self.assertEqual(singleton, default_env) singleton.update_environment(new_inlet=1) self.assertNotEqual(singleton, default_env) again = environment.environment() self.assertEqual(singleton, again) again.update_environment(new_inlet=2) self.assertEqual(singleton, again) self.assertNotEqual(singleton, default_env)
def testBreed4(self): #Esnure that a fox that is not ready to breed, doesn't env = environment.environment(5) #Create a fox that's not ready to breed doesn't fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=10) new_fox = fox.breed(env) self.failUnless(new_fox is None)
def __init__(self, sim_steps=20000, traj_out_freq=100, episodes=20, output_dir=None): # For reproducibility to initialize starting weights torch.manual_seed(459) self.sim_steps = sim_steps self.traj_out_freq = traj_out_freq self.episodes = episodes self.policy = Policy(input_dim=sim_steps / traj_out_freq) self.policy.apply(init_weights) # may not be necessary self.optimizer = optim.SGD(self.policy.parameters(), lr=1e-8) # Randomly choose an eps to normalize rewards self.eps = np.finfo(np.float32).eps.item() if not os.path.exists(output_dir): raise Exception("Path " + str(output_dir) + " does not exist!") self.output_dir = output_dir # Build initial directories if not os.path.exists(self.output_dir + "/results"): os.mkdir(self.output_dir + "/results", 0755) if not os.path.exists(self.output_dir + "/results/final_output"): os.mkdir(self.output_dir + "/results/final_output") if not os.path.exists(self.output_dir + "/results/final_output/intermediate_data"): os.mkdir(self.output_dir + "/results/final_output/intermediate_data") self.env = environment(cvae_weights_path="../model_150.dms", sim_steps=self.sim_steps, traj_out_freq=self.traj_out_freq, output_dir=self.output_dir)
def __init__(self, sim): self.envs = {} for e in range(0, c.numEnvs): self.envs[e] = environment(e)
def interpret_init(): env = environment() s_store = store() for op in ops: env.pushTo(op[0], s_store.alloc()) s_store.setAt(env.lookUp(op[0], s_store), op[1]) return env, s_store
def testBreed1(self): #Esnure that a fox that can breed, does breed env = environment.environment(5) #Create a fox that's ready to breed fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=21) new_fox = fox.breed(env) self.failUnless(isinstance(new_fox,agents.fox))
def testBreed1(self): #Esnure that a rabbit that can breed, does breed env = environment.environment(5) #Create a rabbit that's ready to breed rabbit = agents.rabbit(age=30,food=100,pos=[5.5,5.5],speed=2,last_breed=21) new_rabbit = rabbit.breed(env) self.failUnless(isinstance(new_rabbit,agents.rabbit))
def testDieStarveSync(self): env = environment.environment(10,'sync') # Create a starving fox rabbit= agents.rabbit(age=2,food=-1,pos=[5.5,5.5],speed=2,last_breed=7) # Ensure that it knows that its time to die rabbit.die(env) self.failUnless(rabbit.messages.dead,'Starved rabbit did not die')
def testDieOldSync(self): env = environment.environment(10,'sync') # Create an old rabbit rabbit= agents.rabbit(age=1000,food=10,pos=[5.5,5.5],speed=2,last_breed=7) # Ensure that it knows that its time to die rabbit.die(env) self.failUnless(rabbit.messages.dead,'Ancient rabbit did not die')
def testDieStarveSync(self): env = environment.environment(5,'sync') #Create a starving fox fox= agents.fox(age=2, food=-1, pos=[5.5,5.5], speed=2, last_breed=7) #Ensure that it knows that its time to die fox.die(env) self.failUnless(fox.messages.dead)
def testDieOldSync(self): env = environment.environment(5,'sync') #Create an old fox fox= agents.fox(age=1000, food=10, pos=[5.5,5.5], speed=2, last_breed=7) fox.die(env) self.failUnless(fox.messages.dead)
def testBreed3(self): #Esnure that a breeding fox correctly zeros last_breed env = environment.environment(5) #Create a fox that's ready to breed fox= agents.fox(age=30, food=100, pos=[5.5,5.5], speed=2, last_breed=21) new_fox = fox.breed(env) self.failUnless(new_fox.last_breed == 0 and new_fox.last_breed == 0)
def publish_reddit(self, links): for link in links: self.links_str += link + '\n\n' env = environment.environment() r = praw.Reddit(user_agent='Sndao self-messaging bot 1.0') r.login(env.reddit_username, env.reddit_password) r.send_message('sndao', 'Links to Eventbrite', self.links_str)
def pgm_process(self, epochs=1, steps=1, time_begin="2010-10"): #Using probabilitic graphical model to process self.env = environment.environment(self.config, time_begin) sample_rewards = self.get_sample_rewards(self.env) print(sample_rewards) max_rewards, best_actions = self.rewards_accumulate(sample_rewards) print(max_rewards[0]) print(best_actions)
def lstm_process(self, epochs=1, steps=1, time_begin="2000-10"): #Using LSTM to predict value self.env = environment.environment(self.config, time_begin) X = tf.placeholder(tf.float32, shape=(None, self.config.num_steps, self.config.hidden_size), name="batch_input") y = tf.placeholder(tf.float32, shape=(None, self.config.pairs_num)) (output, state) = self.rnn_network(X) softmax_w = tf.get_variable( "softmax_w", [self.config.hidden_size, self.config.pairs_num], dtype=data_type()) softmax_b = tf.get_variable("softmax_b", [self.config.pairs_num], dtype=data_type()) logits = tf.nn.xw_plus_b(output, softmax_w, softmax_b) q_values = tf.reshape(logits, [self.config.batch_size, self.config.pairs_num]) #xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=q_values) loss = tf.reduce_mean(tf.square(y - q_values)) learning_rate = 0.005 with tf.name_scope("train") as scope: optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = optimizer.minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: init.run() while True: if self.step > 0: tf.get_variable_scope().reuse_variables() input_batch = self.env.get_input(self.step) y_batch = self.env.get_price_pct_change(self.step) input_batch = input_batch.reshape( (self.config.batch_size, self.config.num_steps, self.config.hidden_size)) sess.run(train_op, feed_dict={X: input_batch, y: y_batch}) loss_train = loss.eval(feed_dict={X: input_batch, y: y_batch}) if (self.step % 100 == 0): print(" train loss:", loss_train) ''' action=self.env.epsilon_greedy(q_values,curr_index) reward=self.env.step_forward(action,curr_index) ''' ''' all_price_pct_change=self.daykline.get_all_price_pct_change()[type][time_begin:] print(all_price_pct_change) for epoch in range(epochs): for step in range(steps): next_state=self.get_next_state() column_name=util.states_dict[next_state] ''' self.step += 1
def __init__(self): self.module = 'SL' self.env = environment() self.tk = timekeeper() self.mail = mailer() self.sub = subscriber(self.module) self.log = logger(self.env, self.module) self.eventbrite_raw = [] self.eventbrite_links = []
def __init__(self, input_table): self.initial_state = copy.deepcopy(input_table) self.goal_state = self.SetGoalState(input_table) self.AStarQueue = utility.PriorityQueue() self.explored = utility.Explored() self.frontier = utility.Frontier() self.env = environment.environment(self.frontier)
def test_rl_process(self, time_begin="2010-10"): env = environment.environment(self.config, time_begin) with tf.Session() as sess: if os.path.isfile(self.checkpoint_path): self.saver.restore(self.checkpoint_path) env.reset(num_steps=self.config.num_steps, is_test=True) else: print("no trained parameters")
def main(): agent = Agent(state_size=3, action_size=8, seed=0) start_pos = (200, 600) end_pos = (800, 375) #start_pos = (200,500) #end_pos = (800,500) env = environment(MAP, start_pos, end_pos) """ x_end, y_end = end_pos plt.figure(figsize=(10,6), dpi=200) plt.plot(start_pos[0], start_pos[1], 'rx') plt.plot(x_end, y_end, 'bx') plt.contourf(np.array(MAP), linestyles='dashed') #plt.imshow(np.array(MAP)) plt.gca().set_aspect('equal', adjustable='box') plt.colorbar() plt.show() sys.exit(()) """ # load the weights from file agent.qnetwork_local.load_state_dict(torch.load('checkpoint.pth')) for i in range(1): path_x = [start_pos[0]] path_y = [start_pos[1]] state, _, _ = env.reset(start_pos, end_pos) for j in range(6000): action = agent.act(state) #print (j, action) print(j) #if j%100 == 0: # env.render() state, reward, done = env.step(action) path_x.append(state[0]) path_y.append(state[1]) if done: break print(done) x_end, y_end = end_pos plt.figure(figsize=(10, 6), dpi=200) plt.plot(path_x, path_y, 'ro', markevery=20) plt.plot(x_end, y_end, 'bx') plt.contourf(np.array(MAP), linestyles='dashed') #plt.imshow(np.array(MAP)) plt.gca().set_aspect('equal', adjustable='box') plt.colorbar() plt.show() env.close()
def testMigrateWhenFoodAvailable(self): #Does a rabbit at (5.5,5.5) migrate to the same position as the MATLAB version given that food is available self.env= environment.environment(10,mode='sync') numpy.random.seed(1) #MMigrate uses random numbers so need to seed to get reproducable results rabbit= agents.rabbit(age=2,food=10,pos=[5.5,5.5],speed=2,last_breed=7) rabbit.migrate(self.env) self.failUnlessAlmostEqual(rabbit.pos[0],4.917022,places=4,msg='Migrating rabbit has given different results from MATLAB') self.failUnlessAlmostEqual(rabbit.pos[1],5.22032449,places=4,msg='Migrating rabbit has given different results from MATLAB')
def lstm_process_all_currency(self, epochs=20, steps=1, time_begin="2006-10", time_end="2016-11"): self.env = environment.environment(self.config, time_begin, time_end) for i in range(len(util.states_dict) - 1): self.lstm_process_one_currency(self.env, i + 1, epochs=epochs) break
def testExtractLocalFoodTopLeftCorner(self): self.env = environment.environment(10) rabbit = agents.rabbit(age=2,food=10,pos=[1.1,1.1],speed=2,last_breed=7) #Rabbit in corner of simulation (local_food,xmin,ymin) = rabbit.extract_local_food(self.env) self.failUnlessEqual(numpy.shape(local_food),(3,3),'Incorrect local food size for rabbit in top left corner') self.failUnless(numpy.all(local_food==50),'Incorrect local food values for rabbit in top left')
def testExtractLocalFoodBottomRightCorner(self): self.env = environment.environment(10) rabbit = agents.rabbit(age=2,food=10,pos=[9.5,9.5],speed=2,last_breed=7) #Rabbit in corner of simulation (local_food,xmin,ymin) = rabbit.extract_local_food(self.env) self.failUnlessEqual(numpy.shape(local_food),(3,3),'Incorrect local food size for rabbit in bottom right corner') correct = numpy.array([[0,50,50],[50,50,50],[50,50,50]]) self.failUnless(numpy.array_equal(local_food,correct),'Incorrect local food values for rabbit in bottom right corner')
def testEatInDesert(self): self.env = environment.environment(10) self.rabbit = agents.rabbit(age=2,food=10,pos=[5.5,5.5],speed=2,last_breed=7) #In a food desert #The rabbit is in a food desert. Does it eat correctly? starting_food = self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]] self.failUnlessEqual(starting_food,0,'This rabbit was supposed to be in a desert') eaten = self.rabbit.eat(self.env) self.failUnlessEqual(self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]],0,'Starting food was 0. Rabbit incorrectly changed that!') self.failIfEqual(eaten,1,'Rabbit reports that it has eaten when it can''t have')
def testMigrateWhenFoodNotAvailable(self): #Does a rabbit at (9.8,9.8) migrate to the same posiiton as the MATLAV version given that no food is available numpy.random.seed(1) #MMigrate uses random numbers so need to seed to get reproducable results self.env= environment.environment(10,mode='sync') self.env.food= numpy.zeros((10,10)) #Eat all the food rabbit= agents.rabbit(age=2,food=10,pos=[9.8,9.8],speed=2,last_breed=7) rabbit.migrate(self.env) self.failUnlessAlmostEqual(rabbit.pos[0],7.86930887,places=4,msg='Migrating rabbit has given different results from MATLAB') self.failUnlessAlmostEqual(rabbit.pos[1],9.27805005,places=4,msg='Migrating rabbit has given different results from MATLAB')
def run_simple(): loop_in = False env = environment.environment(-1) env.dec_variable('i', 'int') env.dec_variable('counter', 'int') env.set_variable('counter', 0) while (True): if not loop_in: env.set_variable('i', 0) loop_in = True else: env.set_variable('i', env.get_variable('i') + 1) if not (env.get_variable('i') < 5): break env.set_variable('counter', env.get_variable('counter') + env.get_variable('i')) # met bracket -> jump to loop start ''' if not loop_in: env.set_variable('i', 0) else: env.set_variable('i', env.get_variable('i') + 1) env.set_variable('counter', env.get_variable('counter') + env.get_variable('i')) if not loop_in: env.set_variable('i', 0) else: env.set_variable('i', env.get_variable('i') + 1) env.set_variable('counter', env.get_variable('counter') + env.get_variable('i')) if not loop_in: env.set_variable('i', 0) else: env.set_variable('i', env.get_variable('i') + 1) env.set_variable('counter', env.get_variable('counter') + env.get_variable('i')) if not loop_in: env.set_variable('i', 0) else: env.set_variable('i', env.get_variable('i') + 1) env.set_variable('counter', env.get_variable('counter') + env.get_variable('i')) ''' loop_in = False print(env) # return here env.trace_name('counter') env.trace_name('i') env.return_func(0) print('\n' + str(env))
def testExtractLocalFoodCentral(self): correct = [[50,50,50,50,50], \ [50,50,50,50,50], \ [50,50,50,50,50], \ [50,50,50,0,0], \ [50,50,50,0,0]] self.env = environment.environment(10) rabbit = agents.rabbit(age=2,food=10,pos=[5,5],speed=2,last_breed=7) (local_food,xmin,ymin) = rabbit.extract_local_food(self.env) self.failUnless(numpy.array_equal(local_food,correct),'Incorrect local food values for rabbit in center')
def testEat1Async(self): env = environment.environment(5,'async') # Create a very fast fox fox = agents.fox(age=2, food=10, pos=[1.5,1.5], speed=10, last_breed=7) # create a rabbit very close to it env.agents = [agents.rabbit(age=2, food=10, pos=[1.52,1.52], speed=10, last_breed=7)] #Set the previous position of rabbit to be [1.52,1.52] env.agents[0].messages.pos = [1.52, 1.52] # Its almost certain that this fox ate this rabbit fox.eat(env) # Doex rabbit know that its been eaten? self.failUnless(env.agents[0].has_been_eaten)
def apply_sensory(self, environment): for gene in self.gene_traits: if is_sensory(gene.index): for other_gene in self.gene_traits: if binds(gene.binding, other_gene.binding) and not is_sensory( other_gene.index): loc = environment(location(self.current_pos)) stimulus = get_stimulus(gene.index) new_quantifier = apply_operator( gene.operator, stimulus * gene.quantifier, other_gene.quantifiers) other_gene.quantifier = new_quantifier
def testEatWhenHasFood(self): #The rabbit has some food. Does it eat correctly? self.env = environment.environment(10) self.rabbit = agents.rabbit(age=2,food=10,pos=[1.2,1.2],speed=2,last_breed=7) #Food initially=50 starting_food = self.env.food[(self.rabbit.cpos[0],self.rabbit.cpos[1])] self.failIfEqual(starting_food,0,'This rabbit was supposed to have access to food') eaten = self.rabbit.eat(self.env) self.failUnlessEqual(self.env.food[self.rabbit.cpos[0],self.rabbit.cpos[1]],starting_food-1,'Rabbit has incorrectly changed food quanity after eating.') self.failIfEqual(eaten,0,'Rabbit reports that it hasn''t eaten when it has')
def testBreed2(self): #Ensure that an eaten rabbit doesn't breed in async mode env = environment.environment(5,mode='async') env.agents = [] # Put a hungry fox next to a rabbit env.agents.append(agents.fox(20,30,[1.5,1.5],5,10)) env.agents.append(agents.rabbit(20,30,[1.5,1.25],2,10)) eaten = env.agents[0].eat(env) self.failUnless(eaten,'Fox should have eaten rabbit') new = env.agents[1].breed(env) self.failUnless(new is None,'Dead rabbit bred')
def testDieDoubleCount(self): # Ensure that we don't decrement rabbit counter twice # If an eaten rabbit was also ready to die env = environment.environment(10,'async') env.agents = [] # Put an old rabbit next to a hungry fox env.agents.append(agents.rabbit(1000,30,[1.5,1.5],2,1)) env.agents.append(agents.fox(20,30,[1.5,1.25],10,10)) # Fox eats rabbit eaten = env.agents[1].eat(env) self.failUnless(eaten,'Fox didn''t eat rabbit') # Apply die rule to the dead rabbit env.agents[0].die(env) self.failUnlessEqual(agents.rabbit.num_rabbits,0,'Should be 0 rabbits')
def __init__(self, randomize=False, state=OnState.OFF, configuration=None, env=None, hid=None): self._on_state = state if configuration is None: configuration = config.HeaterConfiguration() # use default if env is None: env = environment.environment() self._config = configuration self._environment = env self._hid = hid self._current_demand = self._environment.demand self._lower_limit = self.configuration.regular_power_lower_limit self._upper_limit = self.configuration.regular_power_upper_limit if randomize: # set temperature somewhere within deadband self._temperature = random.uniform(self.configuration.regular_power_lower_limit, self.configuration.regular_power_upper_limit) else: # set to desired temperature self._temperature = self.configuration.regular_power_upper_limit
def __init__(self): self.env = environment() self.key = self.env.pastebin_key
import pygame from pygame.locals import * from sys import exit import time, math import player, environment, score, menu #Init and Background Setup pygame.init() screen = pygame.display.set_mode((800,600)) character = player.player() terrain = environment.environment() gameMenu = menu.mainMenu(screen) playerScore = score.score() #Game Clock clock = pygame.time.Clock() sleepTime = 0.01 #Title pygame.display.set_caption('Free Runner') gameMenu.start() time.sleep(sleepTime) #Game Loop while True: events = pygame.event.get() for event in events: if event.type == QUIT: if not gameMenu.mute:
from project import project from project.project import * from optparse import OptionParser # @todo --type should be able to be computed via the env_config.json once it's complete # Remove Environment # python remove_environment.py --user dlamanna --project nysba --type development --preserve-db """ Removing an enviornment, possibly keeping the database. """ opt_parser = OptionParser() opt_parser.add_option("-u", "--user", dest="username") opt_parser.add_option("-p", "--project", dest="project") opt_parser.add_option("-t", "--type", dest="env_type", help="Type of environment, must be: development, staging, or production") opt_parser.add_option("-d", "--preserve-db", dest="preserve_db") (options, args) = opt_parser.parse_args() if not project_exists_in_manifest(options.project): print "Project does not exist in manifest, try creating it first." import sys sys.exit() else: env = environment(options.username, options.project, options.env_type) preserve_db = True if options.preserve_db else False env.remove_environment(preserve_db)
def __init__(self): environment.__init__(self) self.loginfo = environment() self.testcase = ''
if __name__=="__main__": # create the argument parser parser = argparse.ArgumentParser(description="Simulate a swarm of Mars rovers") parser.add_argument("env_file", help="The file containing the information about the environment, tab separated.") parser.add_argument("num_rovers", type=int, help="The number of rovers you want to use") parser.add_argument("x_view_dist", type=int, help="How many squares in each direction the rovers can see in the X direction") parser.add_argument("y_view_dist", type=int, help="How many squares in each direction the rovers can see in the Y direction") parser.add_argument("time_steps", type=int, help="How many time steps you'd like to simulate") pargs = parser.parse_args() # parse the environment file here: raw_env = [] # start with an empty list with open(pargs.env_file, 'r') as env_file: for line in env_file: thisline = line.strip().split("\t") x = int(thisline[0]) y = int(thisline[1]) raw_env.append([float(thisline[2]), float(thisline[3])]) # reshape the environment list into the format we want # http://stackoverflow.com/questions/10124751/convert-a-flat-list-to-list-of-list-in-python full_env = zip(*[iter(raw_env)]*(y+1)) # initialize the environment simul_env = environment(full_env, pargs.num_rovers, pargs.x_view_dist, pargs.y_view_dist) for i in range(0, pargs.time_steps): simul_env.time_step() curstate = simul_env.get_state() os.system('clear') print curstate time.sleep(1)
if proceed is not 'y': print "Aborting." import sys sys.exit() else: user = user_utility('/home/dlamanna/app/server_config.json') import getpass new_user_pass = getpass.getpass("Users Password: "******"Project does not exist in manifest, try creating it first." import sys sys.exit() env = environment(username, project, env_type, options.url) env_results = env.create() conf = json.load(get_server_config()) db_details = { "database_name": env_results["db_name"], "database_user": conf["mysql_user"]["username"], "database_pass": re.escape(conf["mysql_user"]["password"]), "database_host": "localhost" } # --with-package # @todo - httpdocs should be configurable (server_config.json) if options.with_package == "wordpress": wp = wp_installer(env_results["directory"] + "/httpdocs", db_details) elif options.with_package == "magento": mage = mage_installer(env_results["directory"] + "/httpdocs", db_details)
def test_default_environment(self): """Check if the user does not give an environment, we use the simulation-wide one.""" self.assertEqual(self.heater.environment, environment.environment())
from environment import environment from scan_stereolive import stereolive from scan_nightculture import nightculture from publish_pastebin import publish_pb from mailer import mailer from subscriber import subscriber env = environment() sl = stereolive() nc = nightculture() mail = mailer() if sl.scan(): print 'Stereo Live' sub = subscriber('SL') links = sl.get_eventbrite() pb = publish_pb() pb_str = str(pb.get_paste(links)) mail.compose(subj='', message='StereoLive: ' + pb_str, to_list=sub.list) if nc.scan(): print 'Nightculture' sub = subscriber('NC') links = nc.get_eventbrite() pb = publish_pb() pb_str = str(pb.get_paste(links)) mail.compose(subj='', message='Nightculture: ' + pb_str, to_list=sub.list)