def compute_gradient(policy, runs_to_estimate): grad = [0.0 for i in xrange(WeightedPolicy.FEATURES_COUNT)] total_reward = 0.0 # Run several games and accumulate a gradient estimate. for j in xrange(runs_to_estimate): policy.clear_gradients() arbit = Arbiter(policy, BasicPolicy(Marker.BLACK), Board()) result = arbit.run() # Draws don't contribute anything to the gradient. if result == Marker.VACANT: continue elif result == Marker.RED: total_reward += 1.0 else: total_reward -= 1.0 for pi_grad in policy.grads: if result == Marker.RED: grad = map(operator.add, grad, pi_grad) else: grad = map(operator.sub, grad, pi_grad) # Normalize the gradient estimate grad = map(lambda d: d / runs_to_estimate, grad) return total_reward / runs_to_estimate, grad
def test_sync_task(): arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) for result in arbiter.add_task(Task("simple_add", task_args=[1, 2]), sync=True): if isinstance(result, dict): assert result['state'] == 'done' assert result['result'] == 3 assert arbiter.workers()[arbiter_queue]['available'] == 10
class Game(object): def __init__(self, user_names): #self.__players = [PcPlayer(Card(), 'ddd'), PcPlayer(Card()),PcPlayer(Card(), 'player 1'), PcPlayer(Card(), 'player 2')] self.__players = [PcPlayer(Card())] for user_name in user_names: self.__players.append(UserPlayer(user_name, Card())) self.__arbiter = Arbiter(self.__players) def play(self): while True: self.show_players_card() self.__arbiter.get_new_piece() self.show_current_piece() self.__arbiter.send_piece_players() winners, losers = self.__arbiter.check_winner_players() if len(winners)>0: for winner in winners: print(f'Выйграл: {winner.name}') break else: if len(losers) != 0: for loser in losers: print(f'Проиграл: {loser.name}') def show_players_card(self): for player in self.__players: print(player.get_display_card()) def show_current_piece(self): print(f'Осталось боченков: {self.__arbiter.piece_len} Текущий номер: {self.__arbiter.current_piece}')
def port_async_processing_task(galloper_url, galloper_project_id, galloper_token, results, report_name, minio_package_name): browsertime_package = f"browsertime_{uuid4()}" params = dict(galloper_url=galloper_url, project_id=galloper_project_id, token=galloper_token, bucket=REPORTS_BUCKET, filename=browsertime_package, url=results.results["info"].get("url"), headers=results.results["info"].get("headers") if results.results["info"].get("headers") else {}, minio_package_name=minio_package_name, report_filename=report_name, browser="chrome") if RABBIT_HOST: logger.info("Connecting to Arbiter") arbiter = Arbiter(host=RABBIT_HOST, port=RABBIT_PORT, user=RABBIT_USER, password=RABBIT_PASSWORD, start_consumer=False) arbiter.apply(task_name='browsertime', queue=RABBIT_QUEUE_NAME, task_kwargs=params) return browsertime_package
def arbiter(): ar = Arbiter() ar.start() while True: ar.input_detected_evt.wait() count = ar.q_out.get() print('The count is: %s' % count) ar.input_detected_evt.clear()
def preform_runs(policy, runs): total_reward = 0.0 for run_idx in xrange(runs): arbit = Arbiter(policy, BasicPolicy(Marker.BLACK), Board()) result = arbit.run() if result == Marker.RED: total_reward += 1.0 elif result == Marker.VACANT: total_reward += 0.0 else: total_reward -= 1.0 return total_reward
def test_kill_task(): arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) start = time() tasks = arbiter.apply("long_running") for task_key in tasks: assert arbiter.status(task_key)['state'] == 'initiated' sleep(2) # time for task to settle arbiter.kill(tasks[0], sync=True) for message in arbiter.wait_for_tasks(tasks): assert message['state'] == 'done' assert time()-start < 180 # 180 sec is a length of task assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def test_kill_group(): tasks_in_squad = 3 start = time() arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) tasks = [] for _ in range(tasks_in_squad): tasks.append(Task("long_running")) squad_id = arbiter.squad(tasks) sleep(5) # time for squad to settle arbiter.kill_group(squad_id) while arbiter.status(squad_id).get("state") != "done": sleep(1) assert time() - start < 180 assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def test_squad(): tasks_in_squad = 3 arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) tasks = [] for _ in range(tasks_in_squad): tasks.append(Task("simple_add", task_args=[1, 2])) squad_id = arbiter.squad(tasks) while arbiter.status(squad_id).get("state") != "done": sleep(1) status = arbiter.status(squad_id) assert status["done"] == tasks_in_squad assert len(status["tasks"]) == tasks_in_squad assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def test_squad_finalyzer(): tasks_in_squad = 3 arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) tasks = [] for _ in range(tasks_in_squad): tasks.append(Task("simple_add", task_args=[1, 2])) tasks.append(Task("simple_add", task_args=[5, 5], task_type='finalize')) squad_id = arbiter.squad(tasks, callback=Task("simple_add", task_args=[5, 4])) while arbiter.status(squad_id).get("state") != "done": sleep(1) status = arbiter.status(squad_id) assert status["done"] == tasks_in_squad + 2 # callback + finalizer assert len(status["tasks"]) == tasks_in_squad + 2 # callback + finalizer assert status["tasks"][-1]['task_type'] == "finalize" assert status["tasks"][-1]['result'] == 10 assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def main(): pb = PiecesBank() app = UI() ### DO NOT F*****G REMOVE THIS. I DARE YOU. ### app.preloadPieces(pb.pieceslist) ai = Ai() arbiter = Arbiter() board = Board() app.setBatchMethod(lambda loop, fitness, mutation: ai.main_function( pb, app, arbiter, board, loop, fitness, mutation)) # app.drawTable(board) app.drawTable(generatedSolvedPuzzle(pb)) ### DO NOT F*****G REMOVE THIS EITHER. ### app.mainloop()
def broadphase(self) -> None: # O(n^2) broad-phase for i, bi in enumerate(self.bodies): for j, bj in enumerate(self.bodies[i + 1:]): if bi.inv_mass == 0.0 and bj.inv_mass == 0.0: continue new_arb = Arbiter(bi, bj) key = ArbiterKey(bi, bj) if new_arb.num_contacts > 0: if key not in self.arbiters: self.arbiters[key] = new_arb else: self.arbiters[key].update(new_arb.contacts) elif key in self.arbiters: self.arbiters.pop(key)
def test_pipe(): tasks_in_pipe = 20 arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) tasks = [] for _ in range(tasks_in_pipe): tasks.append(Task("add_in_pipe", task_args=[2])) pipe_id = None _loop_result = 0 _loop_id = 1 for message in arbiter.pipe(tasks, persistent_args=[2]): if "pipe_id" in message: pipe_id = message["pipe_id"] else: _loop_result = message['result'] assert _loop_result == 4 * _loop_id _loop_id += 1 status = arbiter.status(pipe_id) assert status["done"] == tasks_in_pipe assert len(status["tasks"]) == tasks_in_pipe assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def arbiterTracking(): arbiter = Arbiter() #print 'Initialized Arbiter....\n' if arbiter_tracking: arbiter_window.addstr(0, 1, 'Arbiter', curses.A_UNDERLINE) arbiter_window.refresh() #arbiter_window.addstr(1, 1, 'Searching for arbitrage opportunities....') processed_rates = rates_q.get() #arbiter_window.addstr(2, 1, str(processed_rates)) #arbiter_window.refresh() if processed_rates: #print 'Processed Rates passed to Arbiter Tracking:', processed_rates opportunity_edge = arbiter.findOpportunity(processed_rates) #arbiter_window.addstr(str(edge)) if opportunity_edge: best_polygon, best_ratio = arbiter.getBestPolygonFromPermutate(processed_rates, \ arbiter.permutateRatesAroundEdge(processed_rates, \ arbiter.findOpportunity(processed_rates))) if best_polygon: if best_ratio: arbiter_window.addstr(1, 1, 'The most profitable arbitrage cycle is:') arbiter_window.addstr(2, 1, str(best_polygon), curses.A_STANDOUT) arbiter_window.addstr(3, 1, 'With a rate of return of:') arbiter_window.addstr(4, 1, str(best_ratio), curses.A_STANDOUT) arbiter_window.addstr(' %') arbiter_window.refresh() rates_q.task_done() # Create DiGraph of best polygon #arb_dg = arbiter.createCircularDiGraph(best_polygon, processed_rates) # Create image of best polygon digraph #print ' Creating Image of DiGraph of Best Arbitrage Opportunity\n' #createImageFromDiGraph(arb_dg, 'best_opportunity') #print ' - Created image best_opportunity_digraph.png\n' else: arbiter_window.addstr(1, 1, 'Arbiter Tracking Disabled') arbiter_window.refresh()
def filter_arbiter(valid_env, aug_env, tok): import tqdm listner = Seq2SeqAgent(aug_env, "", tok, args.maxAction) arbiter = Arbiter(aug_env, listner, tok) # Load the model arbiter.load(args.load) # Create Dir os.makedirs(os.path.join(log_dir, 'arbiter_result'), exist_ok=True) # Get the prob for the validation env (may be used for determining the threshold) # arbiter.env = valid_env # valid_inst2prob = arbiter.valid(wrapper=tqdm.tqdm) # json.dump(valid_inst2prob, open(os.path.join(log_dir, 'arbiter_result', 'valid_prob.json'), 'w')) # Get the prob of the augmentation data arbiter.env = aug_env aug_inst2prob = arbiter.valid(wrapper=tqdm.tqdm) aug_data = [datum.copy() for datum in aug_env.data] for datum in aug_data: datum['instructions'] = [datum['instructions']] datum.pop( 'instr_encoding') # Remove the redundant components in the dataset for datum in aug_data: datum['prob'] = aug_inst2prob[datum['instr_id']] json.dump( aug_data, open(os.path.join(log_dir, 'arbiter_result', 'aug_prob.json'), 'w')) # Create the Dataset data = [ datum for datum in aug_data if aug_inst2prob[datum['instr_id']] > 0.5 ] for datum in aug_data: datum.pop('instr_id') return data
def get_arbiter(host=RABBIT_HOST, port=RABBIT_PORT, user=RABBIT_USER, password=RABBIT_PASSWORD, vhost="carrier"): arbiter = Arbiter(host=host, port=port, user=user, password=password, vhost=vhost) return arbiter
def __init__(self, user_names): #self.__players = [PcPlayer(Card(), 'ddd'), PcPlayer(Card()),PcPlayer(Card(), 'player 1'), PcPlayer(Card(), 'player 2')] self.__players = [PcPlayer(Card())] for user_name in user_names: self.__players.append(UserPlayer(user_name, Card())) self.__arbiter = Arbiter(self.__players)
import numpy as np from arbiter import Arbiter from tokens import Token from testnet import TestNet # starting Arbiter network abt = Arbiter(1000000) init_cap = abt.get_init_cap() fund_cap = abt.get_current_cap() print('Initial capital of Arbiter fund is: ' + str(init_cap)) print('Total capital of Arbiter fund is: ' + str(fund_cap)) t = Token(init_cap) print('Token released, amount of token is: ' + str(t.INIT_TOKEN_NUM)) testnet = TestNet() testnet_loop_num = 0 testnet_on = True while testnet_on: if testnet_loop_num < 30: inc_dec_factor = 1 else: if np.random.normal(0, 0.1) < 0: inc_dec_factor = -1 else: inc_dec_factor = 1 random_trader_num = np.random.randint(0, 100) for i in range(random_trader_num): if inc_dec_factor == 1:
# coding:utf-8 from arbiter import Arbiter from worker import Worker from world.main import WorldServer class WorldWorker(Worker): def __init__(self, id, **kwargs): super(WorldWorker, self).__init__(id) self.extra = kwargs def run(self): self.server = WorldServer(self.id, **self.extra) self.server.run() if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument('-w', '--worlds', nargs='+', type=int, required=True) parser.add_argument('-i', '--ip', nargs="?", type=str) args = parser.parse_args() a = Arbiter(args.worlds, WorldWorker, ip=args.ip) a.run()
from kraken import Kraken from bittrex import Bittrex from bitstamp import Bitstamp from bitfinex import Bitfinex from arbiter import Arbiter from wallet import Wallet kraken = Wallet(name='kraken', exchange=Kraken(), balance_usd=10000.0, balance_btc=1.0) bittrex = Wallet(name='bittrex', exchange=Bittrex(), balance_usd=10000.0, balance_btc=1.0) bitstamp = Wallet(name='bitstamp', exchange=Bitstamp(), balance_usd=10000.0, balance_btc=1.0) bitfinex = Wallet(name='bitfinex', exchange=Bitfinex(), balance_usd=10000.0, balance_btc=1.0) a = Arbiter(wallets=[kraken, bittrex, bitstamp], trade_amount=0.25, delay=20) a.arbitrate()
# gpu=True # trackType='csrt' # -------------- Objs loggerPostfix = str(int(args['gpu'])) + str(int(args['serial'])) + str( int(fixedRatio)) + str(int(args['debug'])) + str(int(args['eval'])) + str( args['ratio']) logger = MNR_logger("results", loggerPostfix) arbiter = Arbiter( args['prototxt'], args['model'], args['gpu'], args['serial'], args['tracker'], fixedTracker, logger, args['eval'], args['annotation'], args['ratio'], fixedRatio, args['debug'], ) # gst_str = ('v4l2src device=/dev/video{} ! ' # 'video/x-raw, width=(int){}, height=(int){} ! ' # 'videoconvert ! appsink').format(1, 1920, 1080) # cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture(1) # -------------- Starts cap = cv2.VideoCapture(args['video'])
def train_arbiter(arbiter_env, tok, n_iters, log_every=500, val_envs={}): writer = SummaryWriter(log_dir=log_dir) listner = Seq2SeqAgent(arbiter_env, "", tok, args.maxAction) arbiter = Arbiter(arbiter_env, listner, tok) best_f1 = 0. best_accu = 0. for idx in range(0, n_iters, log_every): interval = min(log_every, n_iters - idx) # Train for log_every interval arbiter.env = arbiter_env arbiter.train(interval) # Train interval iters print() print("Iter: %d" % idx) # Evaluation for env_name, env in val_envs.items(): print("............ Evaluating %s ............." % env_name) arbiter.env = env if env_name == 'train' or env_name == 'val_unseen': path2prob = arbiter.valid(total=500) else: # val_seen need accurate accuracy to evaluate the model performance (for early stopping) path2prob = arbiter.valid() print("len path2prob", len(path2prob)) path2answer = env.get_answer() print("len path2ans", len(path2answer)) false_probs = list([ path2prob[path] for path in path2prob if not path2answer[path] ]) true_positive = len([ 1 for path in path2prob if (path2prob[path] >= 0.5 and path2answer[path]) ]) false_positive = len([ 1 for path in path2prob if (path2prob[path] < 0.5 and path2answer[path]) ]) false_negative = len([ 1 for path in path2prob if (path2prob[path] >= 0.5 and not path2answer[path]) ]) true_negative = len([ 1 for path in path2prob if (path2prob[path] < 0.5 and not path2answer[path]) ]) true_accu = true_positive / (true_positive + false_positive) true_recall = true_positive / max( (true_positive + false_negative), 1) true_f1 = 2 * (true_accu * true_recall) / max( (true_accu + true_recall), 1) false_accu = true_negative / (true_negative + false_negative) print( "tp %d, fp %d, fn %d, tn %d" % (true_positive, false_positive, false_negative, true_negative)) print("All negative", true_negative + false_negative) print("All positive", true_positive + false_positive) writer.add_scalar("true_accu", true_accu, idx) writer.add_scalar("true_recall", true_recall, idx) writer.add_scalar("true_f1", true_f1, idx) writer.add_scalar("false_accu", false_accu, idx) if env_name == 'val_seen': if true_f1 > best_f1: best_f1 = true_f1 print('Save the model with %s f1 score %0.4f' % (env_name, best_f1)) arbiter.save( idx, os.path.join(log_dir, 'state_dict', 'best_%s_f1' % env_name)) if true_accu > best_accu: best_accu = true_accu print("Save the model with %s true accu %0.4f" % (env_name, best_accu)) arbiter.save( idx, os.path.join(log_dir, 'state_dict', 'best_%s_accu' % env_name)) print("True Accu %0.4f, False Accu %0.4f" % (true_accu, false_accu)) print("Avg False probs %0.4f" % (sum(false_probs) / len(false_probs))) sys.stdout.flush()
def test_task_in_task(): arbiter = Arbiter(host=arbiter_host, port=5672, user=arbiter_user, password=arbiter_password) assert arbiter.workers()[arbiter_queue]['total'] == 10 task_keys = [] for _ in range(20): task_keys.append(arbiter.apply("simple_add", task_args=[1, 2])[0]) for task_key in task_keys: assert arbiter.status(task_key)['state'] in ('initiated', 'running') for message in arbiter.wait_for_tasks(task_keys): assert message['state'] == 'done' assert message['result'] == 3 for task_key in task_keys: assert arbiter.status(task_key)['state'] == 'done' assert arbiter.status(task_key)['result'] == 3 assert arbiter.workers()[arbiter_queue]['available'] == 10 arbiter.close()
def request_spot_fleets(args, galloper_url, project_id, token, rabbit_host, rabbit_user, rabbit_password, rabbit_port, vhost): logger.info("Requesting Spot Fleets...") secrets_url = f"{galloper_url}/api/v1/secrets/secret/{project_id}/aws" headers = { 'Authorization': f'bearer {token}', 'Content-type': 'application/json' } aws_config = {} try: aws_config = loads( requests.get(secrets_url, headers=headers).json()["secret"]) except (AttributeError, JSONDecodeError): logger.error("Failed to load AWS config for the project") exit(1) queue_name = str(uuid4()) finalizer_queue_name = str(uuid4()) total_workers = 0 cpu = float(args.execution_params[0]["cpu_cores_limit"] ) if "cpu_cores_limit" in args.execution_params[0] else 1.0 memory = int(args.execution_params[0]["memory_limit"] ) if "memory_limit" in args.execution_params[0] else 1 for i in range(len(args.concurrency)): args.channel[i] = queue_name args.execution_params[i]["JVM_ARGS"] = f"-Xms{memory}g -Xmx{memory}g" total_workers += args.concurrency[i] cpu += 0.5 memory += 1 if cpu > 8: logger.error("Max CPU cores limit should be less then 8") exit(1) if memory > 30: logger.error("Max memory limit should be less then 30g") exit(1) total_cpu_cores = round(cpu * total_workers + 0.1) workers_per_lg = 2 if total_cpu_cores > 2 and memory < 8 else 1 lg_count = round(total_workers / workers_per_lg + 0.1) logger.info(f"CPU per worker - {cpu}. Memory per worker - {memory}g") logger.info(f"Instances count - {lg_count}") global ec2 ec2 = boto3.client( 'ec2', aws_access_key_id=aws_config.get("aws_access_key"), aws_secret_access_key=aws_config["aws_secret_access_key"], region_name=aws_config["region_name"]) user_data = '''#!/bin/bash apt update apt install docker apt install docker.io -y ''' user_data += f"docker pull {args.container[0]}\n" user_data += f"docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e RAM_QUOTA=1g -e CPU_QUOTA=1" \ f" -e CPU_CORES=1 -e RABBIT_HOST={rabbit_host} -e RABBIT_USER={rabbit_user}" \ f" -e RABBIT_PASSWORD={rabbit_password} -e VHOST={vhost} -e QUEUE_NAME={finalizer_queue_name}" \ f" -e LOKI_HOST={galloper_url.replace('https://', 'http://')} " \ f"getcarrier/interceptor:2.5\n" user_data += f"docker run -d -v /var/run/docker.sock:/var/run/docker.sock -e RAM_QUOTA={memory}g -e CPU_QUOTA={cpu}" \ f" -e CPU_CORES={workers_per_lg} -e RABBIT_HOST={rabbit_host} -e RABBIT_USER={rabbit_user}" \ f" -e RABBIT_PASSWORD={rabbit_password} -e VHOST={vhost} -e QUEUE_NAME={queue_name}" \ f" -e LOKI_HOST={galloper_url.replace('https://', 'http://')} " \ f"getcarrier/interceptor:2.5" user_data = base64.b64encode(user_data.encode("ascii")).decode("ascii") config = { "Type": "request", 'AllocationStrategy': "lowestPrice", "IamFleetRole": aws_config["iam_fleet_role"], "TargetCapacity": lg_count, "SpotPrice": "2.5", "TerminateInstancesWithExpiration": True, 'LaunchSpecifications': [] } instance_types = get_instance_types(cpu, memory, workers_per_lg) for each in instance_types: specs = { "ImageId": aws_config["image_id"], "InstanceType": each, "BlockDeviceMappings": [], "SpotPrice": "2.5", "NetworkInterfaces": [], "SecurityGroups": [], "UserData": user_data } if aws_config["security_groups"]: for sg in aws_config["security_groups"].split(","): specs["SecurityGroups"].append({"GroupId": sg}) config["LaunchSpecifications"].append(specs) response = ec2.request_spot_fleet(SpotFleetRequestConfig=config) logger.info("*********************************************") logger.info(response) fleet_id = response["SpotFleetRequestId"] arbiter = Arbiter(host=rabbit_host, port=rabbit_port, user=rabbit_user, password=rabbit_password, vhost=vhost) retry = 10 while retry != 0: try: workers = arbiter.workers() except: workers = {} logger.info(workers) if args.channel[0] in workers and workers[ args.channel[0]]["available"] >= total_workers: logger.info("Spot Fleet instances are ready") break else: logger.info("Waiting for the Spot Fleet instances to start ...") sleep(60) retry -= 1 if retry == 0: logger.info("Spot instances set up timeout - 600 seconds ...") terminate_spot_instances(fleet_id) exit(1) ec2_settings = { "aws_access_key_id": aws_config.get("aws_access_key"), "aws_secret_access_key": aws_config["aws_secret_access_key"], "region_name": aws_config["region_name"], "fleet_id": fleet_id, "finalizer_queue_name": finalizer_queue_name } return ec2_settings
from arbiter import Arbiter if __name__ == "__main__": arbiter = Arbiter() players = arbiter.wait_players() arbiter.players = players arbiter.run_game()
__all__ = ['db', 'worker', 'logger', 'arbiter'] from db import DB from worker import Worker from logger import Logger from arbiter import Arbiter db = DB() logger = Logger(db) arbiter = Arbiter(db) worker = Worker(logger, arbiter)