def learn(): while len(Chromosome.all()) < 30: Chromosome().create() test_set = Match.test_set() if not test_set: print('There are no matches to learn from') return agent_pool = [Agent(c) for c in Chromosome.all()] reserve = [] n = 1 with Pool(4) as pool: while True: try: print('Starting Generation {}'.format(n)) start = datetime.now() for n2, agent in enumerate(agent_pool): print('Running Agent {}'.format(n2)) guesses = pool.map(agent.guess, test_set) agent.right = guesses.count(True) agent.wrong = guesses.count(False) agent.chromosome.agent_score = agent.accuracy agent.chromosome.save() agent_pool.extend(reserve) agent_pool.sort(key=get_score, reverse=True) print('Generation took {}'.format(datetime.now() - start)) print('Best Agent: {}% accurate'.format(agent_pool[0].accuracy * 100)) Chromosome.delete_many(ids=[agent.chromosome.id for agent in agent_pool[15:]]) agent_pool = [Agent(agent.chromosome.mutate()) for agent in agent_pool[:15]] # mutate the top half reserve = agent_pool[:15] n += 1 except KeyboardInterrupt: Chromosome.clean() # keep best 15 break print('Finished learning')
def test_agent_move_south(self): world = World(10, 5) agent = Agent(0) agent.direction = Direction.SOUTH agent.move = True world.agents[(0, 4)] = agent self.assertEqual(world.agents[(0, 4)], agent, '(0) world incorrectly set agent position') world.update() self.assertTrue((0, 4) not in world.agents, 'agent not moved from previous location') self.assertEqual(world.agents[(0, 3)], agent, '(1) world incorrectly updated to move agent South') world.update() self.assertEqual(world.agents[(0, 2)], agent, '(2) world incorrectly updated to move agent South') world.update() self.assertEqual(world.agents[(0, 1)], agent, '(3) world incorrectly updated to move agent South') world.update() self.assertEqual(world.agents[(0, 0)], agent, '(4) world incorrectly updated to move agent South') world.update() self.assertEqual( world.agents[(0, 4)], agent, '(5) world incorrectly updated to move agent South (wrap)') self.assertEqual(len(world.agents), 1, 'World has incorrect number of agents')
def test_agent_move_east(self): world = World(5, 10) agent = Agent(0) agent.direction = Direction.EAST agent.move = True world.agents[(0, 0)] = agent self.assertEqual(world.agents[(0, 0)], agent, '(0) world incorrectly set agent position') world.update() self.assertTrue((0, 0) not in world.agents, 'agent not moved from previous location') self.assertEqual(world.agents[(1, 0)], agent, '(1) world incorrectly updated to move agent East') world.update() self.assertEqual(world.agents[(2, 0)], agent, '(2) world incorrectly updated to move agent East') world.update() self.assertEqual(world.agents[(3, 0)], agent, '(3) world incorrectly updated to move agent East') world.update() self.assertEqual(world.agents[(4, 0)], agent, '(4) world incorrectly updated to move agent East') world.update() self.assertEqual( world.agents[(0, 0)], agent, '(5) world incorrectly updated to move agent East (wrap)') self.assertEqual(len(world.agents), 1, 'World has incorrect number of agents')
def GET(self): #return "development test" tanggal = web.input().get('d') if not tanggal: tanggal = datetime.date.today() else: try: tanggal = datetime.datetime.strptime(tanggal, "%Y-%m-%d").date() except: tanggal = datetime.datetime.strptime(tanggal, "%d %b %y").date() HIDE_THIS = [ a.strip() for a in open('HIDE_AWLR.txt').read().split(',') ] agents = Agent.select( AND(OR(Agent.q.AgentType == HIDROLOGI, Agent.q.AgentType == 0), Agent.q.expose == True)).orderBy(["wilayah", "urutan"]) agents = [a for a in agents if a.table_name not in HIDE_THIS] data = [ Struct(**{ 'pos': a, 'tma': Struct(**a.get_segmented_wl(tanggal)) }) for a in agents ] js = """ <script type="text/javascript"> $(function(){ $('.tanggal').datepicker({dateFormat: 'd M y'}); $('.show-current-date').bind('change', function () { $(this).parent().submit()}); }); </script>""" sebelum = tanggal - datetime.timedelta(days=1) sesudah = tanggal + datetime.timedelta(days=1) # water level tertinggi sql = "SELECT MAX(pagi), agent_id, waktu FROM tma" res = Agent._connection.queryAll(sql) if res: try: (total, agent_id, waktu) = res[0] tertinggi = Struct(**{ 'agent': Agent.get(agent_id), 'total': total, 'waktu': waktu }) except IndexError: tertinggi = Struct() return render.tma.tma_diff(tma=data, meta={ 'now': tanggal.strftime('%d %b %y'), 'before': sebelum.strftime('%d %b %y'), 'after': sesudah.strftime('%d %b %y'), 'tertinggi': tertinggi }, wilayah=WILAYAH, js=js)
def GET(self, oid, tahun=None, bulan=None, tgl=None): try: bd = Agent.get(oid) if bd.AgentType != 3: raise web.notfound(404) except: raise web.notfound(404) if not tgl: try: tanggal = datetime.date(int(tahun), int(bulan), 1) return self.tma_monthly(oid, tahun, bulan) except: pass try: tanggal = datetime.date(int(tahun), int(bulan), int(tgl)) except: tanggal = datetime.date.today() sebelum = tanggal - datetime.timedelta(days=1) sesudah = tanggal + datetime.timedelta(days=1) # '#248F8F', '#70DBDB', '#EBFAFA' try: bendungan = Agent.get(int(oid)) if bendungan.AgentType != 3: raise SQLObjectNotFound except SQLObjectNotFound: return web.notfound(404) plotlines = "" tma = bendungan.get_segmented_tma_bendungan() series = [float(d[0]) for d in tma.get('kapasitas_series')] if tma.get('elevasi'): plotlines = "{ value: " + str(tma.get('elevasi') or 0) + "\ , color: '#cc0000', width: 4, label: {text: '+'" +str(tma.get('elevasi') or 0) + "} }, " ''' val_min = min(series, key=lambda x: abs(x - elevasi)) idx = series.index(val_min) series.insert(idx + 1, {'y': elevasi, 'dataLabels': { 'enabled': 'true', 'style': {'fontSize': 24} }, 'marker': {'fillColor': '#33CC33', 'radius': 5}}) ''' return render_anon.bendungan.show( bendungan, meta={ 'now': tanggal.strftime('%d %b %y'), 'before': sebelum.strftime('%d %b %y'), 'after': sesudah.strftime('%d %b %y'), 'categories1': [float(d[1]) for d in tma.get('kapasitas_series')], 'categories': [], 'series': series, 'plotlines': plotlines, 'tma': tma })
def populate(): agent_add1 = Agent(name='Test 1',phone='0',postcode='AA1AA1') agent_add1.save() agent_add2 = Agent(name='Test 2',phone='1',postcode='AA1AA2') agent_add2.save() agent_add3 = Agent(name='Test 3',phone='2',postcode='AA1AA3') agent_add3.save() count = 0 artist = 0 agents = [agent_add1,agent_add2,agent_add3] while count < 1000: artist = int(artist) if artist % 5 == 0: artist += 1 artist = str(art) ag = agents[count%3] art = Art(name=str(count), sub_category=str(int(artist)%3), category=str(count%10), artist=artist, price=50, quantity=0, agent=ag) art.save() count += 1
def GET(self, pid): try: oid = Agent.select(Agent.q.prima_id == pid)[0].AgentId bd = Agent.get(oid) if bd.AgentType != 3: raise web.notfound(404) except: raise web.notfound(404) tanggal = web.input().get('d') if not tanggal: tanggal = datetime.date.today() else: tanggal = to_date(tanggal) try: bendungan = Agent.get(int(oid)) if bendungan.AgentType != 3: raise SQLObjectNotFound except SQLObjectNotFound: return web.notfound(404) sql = "SELECT SamplingDate, SamplingTime, WLevel, Temperature, Humidity, Rain4 FROM %(table_name)s \ ORDER BY SamplingDate DESC, SamplingTime DESC \ LIMIT 0, 25" % ({ 'table_name': bendungan.table_name }) rst = [(int(r[0].strftime('%s')) + r[1].seconds, r[2], r[3], r[4], r[5]) for r in bendungan._connection.queryAll(sql)] latest = {} if not rst: sql = "SELECT SamplingDate, SamplingTime, WLevel, Temperature, Humidity, Rain4 FROM %(table_name)s \ ORDER BY SamplingDate, SamplingTime LIMIT 0, 25" % ({ 'table_name': bendungan.table_name }) rst = [(int(r[0].strftime('%s')) + r[1].seconds, r[2], r[3], r[4], r[5]) for r in bendungan._connection.queryAll(sql)] d = rst[0] samp = datetime.datetime.fromtimestamp(d[0]) if samp.date() == datetime.date.today(): sampling = samp.strftime('%H:%M') else: sampling = samp.strftime('%d %b %H:%M') latest = dict(sampling=sampling, wlevel=d[1] / 100.0, temperature=d[2], humidity=d[3], sq=d[4]) return render.bendungan.liveprima(bendungan, meta={ 'now': tanggal.strftime('%d %b %y'), 'tma': rst, 'latest_sampling': latest })
def create_new_agent(): ''' Create a new Agent --- tags: - Agents parameters: - name: Agent in: body schema: type: object properties: name: type: string email: type: string phone_number: type: string responses: 201: description: Returns the new Agent ''' try: body = request.get_json() if body is None: abort(400) user_input = { "name": body.get('name', None), "phone_number": body.get('phone_number', None), "email": body.get('email', None) } agent = Agent(name=user_input['name'], phone_number=user_input['phone_number'], email=user_input['email']) db.session.add(agent) db.session.commit() return jsonify({ "success": True, "result": agent.format(), "timestamp": time.time() }), 201 except AuthError: abort(401) except Exception as err: db.session.rollback() return jsonify({"success": False, "error": str(err)}), 500 finally: db.session.close()
def upload(): to_load = {} with open('/tmp/svrdata.pkl', 'rb') as f: to_load = cPickle.load(f) for d in to_load.get('pos_to_put', []): try: agent = Agent.select(Agent.q.AgentName == d['name'])[0] except IndexError: try: agent = Agent(**d['data']) # karena 'id'nya tidak standar, # SQLObject bilang Not Found except SQLObjectNotFound: agent = Agent.select(Agent.q.AgentName == d['name'])[0] # Periksa apakah table pemuat Logs untuk # pos ini telah tersedia try: rs = conn.queryAll("SELECT SamplingDate FROM %s \ LIMIT 0, 1" % (agent.table_name)) except: rs = conn.queryAll("CREATE TABLE %s \ LIKE tpl_agent" % agent.table_name) print agent.AgentName for l in d['logs']: print '\t', l['SamplingDate'], l['SamplingTime'] sql = "SELECT COUNT(*) FROM %s \ WHERE SamplingDate='%s' AND \ SamplingTime='%s'" % (agent.table_name, l['SamplingDate'], l['SamplingTime']) rs = conn.queryAll(sql) print '\t\trs[0][0]:', rs[0][0] if rs[0][0] == 0: sql = "INSERT INTO %s (RID, ReceivedDate, \ ReceivedTime, DataType, StatusPort, \ SamplingDate, SamplingTime, Temperature, \ Humidity, Rain, Rain1, Rain2, Rain3, \ Rain4, WLevel, Wlevel1, WLevel2, \ WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \ '%s', %s, '%s', '%s', '%s', %s, %s, \ %s, %s, %s, %s, %s, %s, %s, %s, %s, \ %s, '%s', %s)" % ( agent.table_name, l['RID'], l['ReceivedDate'], l['ReceivedTime'], l['DataType'], l['StatusPort'], l['SamplingDate'], l['SamplingTime'], l['Temperature'], l['Humidity'], l['Rain'], l['Rain1'], l['Rain2'], l['Rain3'], l['Rain4'], l['WLevel'], l['WLevel1'], l['WLevel2'], l['WLevel3'], l['WLevel4'], l['up_since'], l['sq']) rs = conn.query(sql) print '\tsaved:', l['SamplingDate'], l['SamplingTime'] del l
def run_single_episode(env: UnityEnvironment, brain_name, agent: Agent = None, train_mode=False, max_t=2000, epsilon=0.0): """ Execute a single episode Params ====== env (UnityEnvironment): enviroment brain_name (string): default brain name agent (Agent): agent that is responsible for control the actions (if no agent, a random action is chosen) train_mode (bool): indicate if the environment is on the train mode max_t (int): max number of steps in each episode epsilon (float): attenuate the noise applied to the action Return ====== scores (float): episode scores of all agents """ env_info = env.reset(train_mode=train_mode)[brain_name] num_agents = len(env_info.agents) action_size = env.brains[brain_name].vector_action_space_size states = env_info.vector_observations scores = np.zeros(num_agents) # initialize the score (for each agent) # Run all the steps of one episode for time_step in range(1, max_t + 1): if agent: # if a agent is provide, get all the action (for each agent) actions = agent.act(states, epsilon=epsilon, add_noise=train_mode) else: # select a random action (if no agent) actions = np.random.randn(num_agents, action_size) actions = np.clip(actions, -1, 1) # all actions between -1 and 1 env_info = env.step(actions)[ brain_name] # send the action to the environment next_states = env_info.vector_observations # get the next state rewards = env_info.rewards # get the reward dones = env_info.local_done # get the done flag if agent and train_mode: # learn if in train mode agent.step(states, actions, rewards, next_states, dones, time_step) states = next_states scores += rewards # increase the scores (for each agent) if np.any(dones): # Exit episode if done break return scores
def main(): # Initialize world world = World(5, 5) world.agents[(0,0)] = Agent() world.agents[(0,0)].move = True world.agents[(0,0)].direction = Direction.EAST world.agents[(3,3)] = Agent() world.agents[(3,3)].move = True print('0', world) # Run simulation for t in range(1,10): world.update() print(t, world)
def GET(self, loc): agents = dict([(r.table_name, r.AgentId) for r in Agent.select(OR(Agent.q.AgentType==3, Agent.q.AgentType==2))]) if agents.get(loc): sql = "SELECT SamplingDate, SamplingTime, WLevel/100.0 FROM %s \ ORDER BY SamplingDate DESC, SamplingTime DESC \ LIMIT 0, 1" % loc rst = conn.queryAll(sql) if rst: pos = Agent.get(agents.get(loc)) sampling = datetime.datetime.fromtimestamp(int(rst[0][0].strftime('%s')) + rst[0][1].seconds) web.header('Content-Type', 'text/plain') web.header('Access-Control-Allow-Origin', '*') return pos.cname + '|' + sampling.strftime('%d%b%y|%H:%M') + '|' + 'TMA: %.2f' % (pos.DPL + float(rst[0][2] or 0)) return web.notfound()
def get_command(agent_id): agent = Agent.query.get(agent_id) if not agent: agent = Agent(agent_id) db.session.add(agent) db.session.commit() # Report basic info about the agent info = request.json if info: if 'platform' in info: agent.operating_system = info['platform'] if 'hostname' in info: agent.hostname = info['hostname'] if 'username' in info: agent.username = info['username'] agent.last_online = datetime.now() agent.remote_ip = request.remote_addr agent.geolocation = geolocation(agent.remote_ip) db.session.commit() # Return pending commands for the agent cmd_to_run = '' cmd = agent.commands.order_by(Command.timestamp.desc()).first() if cmd: cmd_to_run = cmd.cmdline db.session.delete(cmd) db.session.commit() return cmd_to_run
def GET(self, pid): try: pos = Agent.get(pid) except SQLObjectNotFound: return web.notfound() try: dari = to_date(web.input().dari) dari = datetime.datetime.combine(dari, datetime.time(7, 0, 0)) hingga = to_date(web.input().hingga) hingga = datetime.datetime.combine(hingga, datetime.datetime.now().time()) except: hingga = datetime.datetime.now() dari = hingga - datetime.timedelta(days=2) dari = dari.replace(hour=7, minute=0, second=0) #print dari, hingga tma_trend = pos.get_log_tma(dari, hingga) series = [ '{x: %s, y: %s}' % (int(a[0].strftime('%s')) + a[1].seconds, float(a[2] or 0) * 0.01) for a in tma_trend ] data_series = [{ 'nama': pos.cname, 'series': ', '.join(series), 'satuan': 'M' }] return render.bendungan.show_tma({ 'pos': pos, 'data': data_series, 'dari': dari, 'hingga': hingga })
def GET(self, oid): bendung = Agent.get(int(oid)) meta = { "dari": datetime.date(2017, 1, 1), "hingga": datetime.date(2017, 3, 1) } return render.bendungan.keamanan.index(bendung, meta)
def GET(self): auth = web.ctx.env.get('HTTP_AUTHORIZATION') authreq = False allowed = [(u.username, u.password) for u in Authuser.select()] if auth is None: authreq = True else: auth = re.sub('^Basic', '', auth) username, password = base64.decodestring(auth).split(':') password = md5(password).hexdigest() if (username, password) in allowed: if web.input()['what'] == 'pos': poses = [] for a in Agent.select(): try: samp_date = a.get_last_log()[5] samp_time = a.get_last_log()[6] if type(samp_time) == datetime.timedelta: samp_time = datetime.datetime.strptime( str(samp_time), '%H:%M:%S') last_log = str(samp_date) + ' ' + str( samp_time.time()) except: last_log = None poses.append({'AgentName': a.AgentName, 'md5': md5( str(a.sqlmeta.asDict())).hexdigest(), 'last_log': last_log}) return json.dumps(poses) else: authreq = True if authreq: web.header('WWW-Authenticate', 'Basic realm="Remote Data"') web.ctx.status = '401 unauthorized' return """<html>
def GET(self, pos_name): poses = dict([(a.table_name, a.AgentId) for a in Agent.select()]) pos_id = poses.get(pos_name, None) if pos_id: return Show().GET(pos_id) else: return web.notfound()
def GET(self): tanggal = web.input().get('d') if not tanggal: tanggal = datetime.date.today() - datetime.timedelta(days=1) else: try: tanggal = to_date(tanggal) except: tanggal = to_date(tanggal) HIDE_THIS = [a.strip() for a in open('HIDE_ARR.txt').read().split(',')] agents = Agent.select( AND(OR(Agent.q.AgentType == KLIMATOLOGI, Agent.q.AgentType == 0.0), Agent.q.expose == True)).orderBy(( 'wilayah', 'urutan', )) agents = [a for a in agents if a.table_name not in HIDE_THIS] data = [] for a in agents: rain = a.get_segmented_rain(tanggal) row = { 'pos': a.cname or a.AgentName, 'id': a.AgentId, 'telemetri': rain.get('total'), 'manual': rain.get('manual') } data.append(row) return json.dumps({ 'tanggal': tanggal, 'curahhujan': data }, default=json_serialize)
def add_new_agent(agent_name): """ валидация и запись нового контрагента в БД agent_name (str): имя контрагента из формы на стр. agent_add.html """ agent = session.query(Agent).filter_by(name=agent_name).all() # проверка на наличие контрагента в базе данных if agent: eel.alert_message(f'Контрагент "{agent[0].name}" уже сущесвует') print(f'Контрагент "{agent[0].name}" уже сущесвует') return False # валидация имени контрагента if agent_name: print(agent_name) new_agent = Agent(name=agent_name) session.add(new_agent) session.commit() new_agent_id = session.query(Agent).filter_by( name=agent_name).first().id eel.alert_message( f'Добавлен новый контрагент: {agent_name} c id: {new_agent_id}') print(f'Добавлен новый контрагент: {agent_name} c id: {new_agent_id}') else: eel.alert_message(f'Не верное имя агента: {agent_name}') print(f'Не верное имя агента: {agent_name}') return False return True
def run_single_episode(env: UnityEnvironment, brain_name, agent: Agent = None, max_t=1000, eps=0., train_mode=False): """ Execute a single episode Params ====== env (UnityEnvironment): enviroment brain_name (string): default brain name agent (Agent): agent that is responsible for control the actions (if no agent, a random action is chosen) max_t (int): max steps in each episode train_mode (bool): indicate if the environment is on the train mode Return ====== score (float): total score of episode """ env_info = env.reset(train_mode=train_mode)[brain_name] action_size = env.brains[brain_name].vector_action_space_size state = env_info.vector_observations[0] score = 0 for _ in range(max_t): # Run each step in episode action = agent.act(state, eps) if agent else np.random.randint(action_size) env_info = env.step(action)[ brain_name] # send the action to the environment next_state = env_info.vector_observations[0] # get the next state reward = env_info.rewards[0] # get the reward done = env_info.local_done[0] # get the done flag if agent and train_mode: agent.step(state, action, reward, next_state, done) state = next_state score += reward if done: # Exit episode if done break return score
def delete(self): workflow = find_workflow(self.request) if workflow is None: self.error(404) else: # remove from agent workflow sets then delete for agent in Agent.query(Agent.workflows == workflow.key.id()): agent.workflows.remove(workflow.key.id()) workflow.key.delete()
def main( env_name, n_epoch, learning_rate, gamma, n_hidden, seed_val=0, max_steps=1000 ): '''train an a2c network some gym env''' # define env env = gym.make(env_name) env.seed(seed_val) np.random.seed(seed_val) torch.manual_seed(seed_val) # define agent state_dim, n_actions, action_space_type = get_env_info(env) agent = Agent(state_dim, n_hidden, n_actions) optimizer = torch.optim.Adam(agent.parameters(), lr=learning_rate) # train log_step = np.zeros((n_epoch,)) log_return = np.zeros((n_epoch,)) log_loss_v = np.zeros((n_epoch,)) log_loss_p = np.zeros((n_epoch,)) for i in range(n_epoch): cumulative_reward, step, probs, rewards, values = run( agent, env, gamma=gamma, max_steps=max_steps ) # update weights returns = compute_returns(rewards, gamma=gamma, normalize=True) loss_policy, loss_value = compute_a2c_loss(probs, values, returns) loss = loss_policy + loss_value optimizer.zero_grad() loss.backward() optimizer.step() # log message log_step[i] = step log_return[i] = cumulative_reward log_loss_v[i] = loss_value.item() log_loss_p[i] = loss_policy.item() if np.mod(i, 10) == 0: print( 'Epoch : %.3d | R: %.2f, steps: %4d | L: pi: %.2f, V: %.2f' % (i, log_return[i], log_step[i], log_loss_p[i], log_loss_v[i]) ) # save weights # ckpt_fname = f'../log/agent-{env_name}-{n_epoch}.pth' ckpt_fname = f'../log/agent-{env_name}.pth' torch.save(agent.state_dict(), ckpt_fname) '''show learning curve: return, steps''' f, axes = plt.subplots(2, 1, figsize=(7, 7), sharex=True) axes[0].plot(log_return) axes[1].plot(log_step) axes[0].set_title(f'Learning curve: {env_name}') axes[0].set_ylabel('Return') axes[1].set_ylabel('#steps') axes[1].set_xlabel('Epoch') sns.despine() f.tight_layout() f.savefig(f'../figs/lc-{env_name}.png', dpi=120)
def GET(self): '''Mengambil data Log terakhir dari masing-masing Pos Pos yang dipilih yang ada LatLonnya''' data = [] for agent in Agent.select(Agent.q.AgentType == 3): if agent.get_tma(): data.append(agent.get_tma()) web.header('Content-Type', 'application/json') return json.dumps(data)
def simple_bandit_experiment(): b = Bandit() alpha = 0.1 beta = 1.2 print('alpha: {:.2f} beta: {:.2f}\n'.format(alpha, beta)) agent = Agent(b, alpha, beta) trials = 1000 for _ in range(trials): agent.run() df = agent.get_df() ml = ML(df, 2) r = ml.ml_estimation() alpha_hat, beta_hat = r.x print(r) fig, ax = plt.subplots(1, 1) ml.plot_ml(ax, alpha, beta, alpha_hat, beta_hat) plt.show()
def GET(self): '''Ringkasan seluruh bendungan hari ini''' bdgs = Agent.select(Agent.q.AgentType == 3) nums = bdgs.count() ids = [b.AgentId for b in bdgs] today = datetime.date.today() # mengambil nilai ROTW (Rencana Operasi Tahunan Waduk) # sql = "SELECT SUM(po_outflow_q) AS po_outflow, \ SUM(intake_q) AS outflow, SUM(inflow_q) AS inflow, \ SUM(vol6) AS vol6, SUM(po_vol) AS po_vol, \ SUM(po_inflow_q) AS po_inflow_q \ FROM waduk_daily \ WHERE waktu='%s'" % (today) # mengambil nilai ROTW (Rencana Operasi Tahunan Waduk) # jhar = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] waktu_rtow = today.replace(day=15) if today.day >= 16: waktu_rtow = today.replace(day=jhar[today.month - 1]) sql2 = "SELECT SUM(po_outflow_q) AS po_outflow, \ SUM(po_vol) AS po_vol, \ SUM(po_inflow_q) AS po_inflow_q \ FROM waduk_daily \ WHERE waktu = '%s'" % (waktu_rtow) rst = Agent._connection.queryAll(sql) rst2 = Agent._connection.queryAll(sql2) rec = rst[0] rec2 = rst2[0] real_vol = rec[3] and commify('%d' % (rec[3])) or '-' rotw_vol = rec2[1] and commify('%d' % (rec2[1])) or '-' rotw_inflow_q = rec2[2] and commify('%d' % (rec2[2])) or '-' real_inflow_q = rec[2] and commify('%d' % (rec[2])) or '-' # total kapasitas volume waduk tot_vol = commify(int(sum([b.volume for b in bdgs]))) # total luas baku irigasi tot_lbi = sum([b.lbi for b in bdgs]) all_pos = [{ 'id': a.AgentId, 'name': a.cname, 'll': a.ll } for a in bdgs] js_foot = """ """ return render.bendungan.summary({ 'nums': nums, 't_vol': tot_vol, 'today': today, 'r_vol': real_vol, 'rotw_vol': rotw_vol, 'po_outflow_q': rec2[0], 'outflow_q': rec[1], 'po_inflow_q': rotw_inflow_q, 'inflow_q': real_inflow_q, 't_lbi': tot_lbi, 'js_foot': js_foot })
def GET(self, oid): try: bendung = Agent.get(int(oid)) except SQLObjectNotFound: return web.notfound() inp = web.input() if inp.get('periode'): periode = to_date(inp.periode) else: periode = datetime.date.today() if periode.month < 11: periode = datetime.date(periode.year - 1, 11, 1) # 1 Nop tahun lalu else: periode = datetime.date(periode.year, 11, 1) tanggal = periode if periode.month > 10: tanggal = datetime.date(tanggal.year + 1, 11, 1) if tanggal.year <= 2018: jhar = [30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31] rr = [] for i in range(12): rr.append(periode.replace(day=1)) rr.append(periode.replace(day=16)) periode += datetime.timedelta(days=jhar[i]) if tanggal.year > 2018: jhar = [30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31] rr = [] for i in range(12): rr.append(periode.replace(day=15)) rr.append(periode.replace(day=jhar[i])) periode += datetime.timedelta(days=jhar[i]) sql = "SELECT waktu, po_tma, po_inflow_q, po_inflow_v, \ po_outflow_q, po_outflow_v, \ tma6, vol6, inflow_q, inflow_v, intake_q, intake_v, po_bona, po_bonb, vol_bona, vol_bonb \ FROM waduk_daily \ WHERE waktu IN (%s) AND pos_id=%s \ ORDER BY waktu" % (','.join(["'" + str(i) + "'" for i in rr]), oid) rst = Agent._connection.queryAll(sql) sql_realisasi = "SELECT \ tma6, vol6, inflow_q, inflow_v, intake_q, intake_v \ FROM waduk_daily \ WHERE pos_id=%s AND waktu BETWEEN %s AND %s \ ORDER BY waktu" meta = { 'rst': rst, 'daily': [], 'dari': periode, 'hingga': periode, 'tanggal': tanggal } return render.bendungan.operasi(bendung, meta)
def GET(self): try: tanggal = to_date(web.input().get('d')) except: tanggal = datetime.date.today() poses = dict([l.split('\t') for l in open( 'agent_table.txt').readlines()]) agent_klimatologi = [Agent.get(poses.get(a)) for a in KLIMATOLOGI_POS] agent_klimatologi = [(a, a.get_segmented_klimatologi(tanggal)) for a in agent_klimatologi] return render.klimatologi.index({'pos': agent_klimatologi, 'tanggal': tanggal, 'sebelum': tanggal - datetime.timedelta(days=1), 'sesudah': tanggal + datetime.timedelta(days=1)})
def GET(self): HIDE_THIS = [ a.strip() for a in open('HIDE_AWLR.txt').read().split(',') ] agents = Agent.select( AND(OR(Agent.q.AgentType == HIDROLOGI, Agent.q.AgentType == 0), Agent.q.expose == True)).orderBy(["wilayah", "cname"]) agents = [a for a in agents if a.table_name not in HIDE_THIS] tgl = datetime.date.today() return render.tma.addt(agents, tgl)
def GET(self, table_name): '''Masuk ke sini jika authuser.is_admin == 4''' agent_id = dict([a.split('\t') for a in open('agent_table.txt').readlines()]).get(table_name) pos = Agent.get(agent_id) webinput = web.input(sampling=str(datetime.date.today() - datetime.timedelta(days=1))) tg = datetime.datetime.strptime(webinput.sampling, '%Y-%m-%d').date() rst = KlimatManual.select(AND(func.YEAR(KlimatManual.q.sampling)==tg.year, func.MONTH(KlimatManual.q.sampling)==tg.month, KlimatManual.q.agent==agent_id)) return render.adm.klimatologi.show({'tg': tg, 'pos': pos, 'data': rst})
def main(env_name, n_hidden): '''render the performance of a saved ckpt''' # define env and agent env = gym.make(env_name).env state_dim, n_actions, action_space_type = get_env_info(env) agent = Agent(state_dim, n_hidden, n_actions) agent.load_state_dict(torch.load(f'../log/agent-{env_name}.pth')) agent.eval() cumulative_reward, step, probs, rewards, values = run(agent, env, render=True)
def GET(self, pid): try: pos = Agent.get(pid) except SQLObjectNotFound: return web.notfound() hingga = datetime.datetime.now() dari = hingga - datetime.timedelta(days=3) tma_trend = pos.get_log_tma(dari, hingga) series = [(str(a[0]), str(a[1]), float(a[2] or 0) * 0.01) for a in tma_trend] return render.tma.show({'pos': pos, 'data': series})
def agents(tenant_id): if request.method == 'POST': if isinstance(request.json, type('string')): request.json = json.loads(request.json) tenant = Tenant.query.get(tenant_id) agent = Agent(tenant=tenant, uuid=request.json['uuid'], hostname=request.json['hostname'], os_version=request.json['os_version'], agent_version=request.json['agent_version']) #tags = [] #for t in request.json['tags']: # tag = Tag(name=t["name"], value=t["value"]) # tags.append(tag) #agent.tags.extend(tags) db_session.add(agent) db_session.commit() return jsonify(agent.as_dict()) else: agents = Agent.query.filter_by(tenant_id=tenant_id) agents_dicts = map(Agent.as_dict, agents.all()) return Response(json.dumps(agents_dicts, cls=DateTimeJsonEncoder), mimetype='application/json')
def GET(self): tanggal = web.input().get('d') if not tanggal: tanggal = datetime.date.today() else: try: tanggal = datetime.datetime.strptime(tanggal, "%Y-%m-%d").date() except: tanggal = datetime.datetime.strptime(tanggal, "%d %b %y").date() HIDE_THIS = [ a.strip() for a in open('HIDE_AWLR.txt').read().split(',') ] agents = Agent.select( AND(OR(Agent.q.AgentType == HIDROLOGI, Agent.q.AgentType == 0), Agent.q.expose == True)).orderBy(["wilayah", "urutan"]) agents = [a for a in agents if a.table_name not in HIDE_THIS] data = [ Struct(**{ 'pos': a, 'tma': Struct(**a.get_segmented_wl(tanggal)) }) for a in agents ] js = """ <script type="text/javascript"> $(function(){ $('.tanggal').datepicker({dateFormat: 'd M y'}); $('.show-current-date').bind('change', function () { $(this).parent().submit()}); }); </script>""" sebelum = tanggal - datetime.timedelta(days=1) sesudah = tanggal + datetime.timedelta(days=1) if web.ctx.env.get('HTTP_X_PJAX') is not None: return render_plain.tma.index_table( tma=data, meta={ 'now': tanggal.strftime('%d %b %y'), 'before': sebelum.strftime('%d %b %y'), 'after': sesudah.strftime('%d %b %y') }, wilayah=WILAYAH) return render.tma.index(tma=data, meta={ 'now': tanggal.strftime('%d %b %y'), 'before': sebelum.strftime('%d %b %y'), 'after': sesudah.strftime('%d %b %y') }, wilayah=WILAYAH, js=js)
def post(self): workflow = Workflow(**json.decode(self.request.body)) versions = Workflow.query(Workflow.name == workflow.name).order(-Workflow.version).fetch(1) if any(versions): # bump version to one greater that last known one workflow.version = versions[0].version + 1 new_key = workflow.put() logging.info("Create/update: %s", new_key.id()) if any(versions): # replace earlier with this version in relevant agent workflow sets old_id = versions[0].key.id() for agent in Agent.query(Agent.trackVersion == True, Agent.workflows == old_id): agent.workflows.remove(old_id) agent.workflows.append(new_key.id()) self.redirect('/workflows')
def validate(self, item_type, curuser): result = True if not Form.validate(self): return False if self.is_ip.data and len(str(self.inn.data)) != 12: self.inn.errors.append(u"Неверно указан ИНН") result = False if (not self.is_ip.data) and len(str(self.inn.data)) != 10: self.inn.errors.append(u"Неверно указан ИНН") result = False if item_type == 'agent': if Agent.exist_inn(self.inn.data, curuser) and (self.uid.data == ''): self.inn.errors.append(u"Контрагент с таким ИНН уже создан") result = False return result
def heartbeat(id): key = ndb.Key("Agent",id) agent = Agent(key=key, agent_id=id, version='0.1', hostname='test', ip='192.168.1.1', updated_at=datetime.datetime.utcnow()) agent.put() return "OK"
def put(self): agent = Agent(**json.decode(self.request.body)) agent.put()