def __init__(self, system): self.ets = 0 self.newts = 0 self.newl = None self.val = Value() self.system = system self.decided = False self.proposed = False self.l = getMax(system.processes) self.val.v = 0 self.val.defined = False self.system.abstractions.append( Ep(self.system, self.ets, self.l, EpState(0, self.val)))
def check(self): notSuspected = dict() for key in self.system.processes.keys(): if key not in self.suspected.keys(): notSuspected[key] = self.system.processes[key] maximum = getMax(notSuspected) if not maximum: return if not self.leader or (not self.leader.rank == maximum.rank): self.leader = maximum message = Message() message.abstractionId = 'ec' message.type = Message.Type.ELD_TRUST message.eldTrust.process.CopyFrom(maximum) self.system.events.append(message)
def runGrover(config): setup() # Add the search specific gates oracle(config) end() # Compile and run the Quantum circuit on a simulator backend job_sim = execute(qc, "local_qasm_simulator") sim_result = job_sim.result() # Show the results print("simulation: ", sim_result) # Returns a dict utils.pprint(sim_result.get_counts(qc)) print("Searching for : " + config) print("Found : " + utils.getMax(sim_result.get_counts(qc))) print("This was found " + str(sim_result.get_counts(qc).get(config)) + " times")
if iter == 1: m = inner.getInitial(nodeEdgeIndex) Xstart, Xend = inner.runInner_iteration(timestamps, nodeEdgeIndex, m) else: m = inner.getNextInput(nodeEdgeIndex, Xstart, Xend) Xstart, Xend = inner.runInner_iteration(timestamps, nodeEdgeIndex, m) Cost_IP.append( utils.getCost(Xstart, Xend) / ((event_length - 1) * num_nodes)) p, r, f = utils.compareGT(Xstart, Xend, active_truth, timestamps) P_IP.append(p) R_IP.append(r) F_IP.append(f) Costmax_IP.append(utils.getMax(Xstart, Xend) / (event_length - 1)) name = 'test_convergence' plt.figure() plt.plot(iterations, Cost_IP, 'k') plt.xlabel('iterations', fontsize=25) plt.ylabel('Relative total length', fontsize=25) plt.tight_layout() plt.savefig(name + '_' + 'total.pdf') plt.figure() plt.plot(iterations, Costmax_IP, 'k') plt.xlabel('iterations', fontsize=25) plt.ylabel('Relative max length', fontsize=25) plt.tight_layout() plt.savefig(name + '_' + 'costmax.pdf')
#!/usr/bin/env python3 # Import the Halite SDK, which will let you interact with the game. import hlt from hlt import constants import random import logging import utils # This game object contains the initial game state. game = hlt.Game() ship_status = {} maxx, maxy = utils.getMax(game) lb, rb = utils.setBorders(game, maxx) #logging.info('lb = {}, rb = {}'.format(lb, rb)) # Respond with your name. game.ready("TheMadMan") while True: # Get the latest game state. game.update_frame() # You extract player metadata and the updated map metadata here for convenience. me = game.me game_map = game.game_map # A command queue holds all the commands you will run this turn. command_queue = [] for ship in me.get_ships():
from utils import getMax, getSum arr1 = [1, 2, 3, 4] arr2 = [4, 5, 6, 7, 8] arr3 = [1312313, 3443, 43123213] arr4 = [12, 3, 2, 1323] arrays = [arr1, arr2, arr3, arr4] for i in arrays: maxi = getMax(i) sumi = getSum(i) print(i, maxi, sumi)
G = utils.generateGraph(n=num_nodes) print 'number of nodes in the background network:', G.number_of_nodes() print 'number of edges in the background network:', G.number_of_edges() timestamps, active_truth = utils.generateIntervals( G, event_length=event_length, overlap=overlap) print 'number of timestamps', len(timestamps) if alg == 'baseline': Xstart, Xend = baseline.baseline(timestamps) elif alg == 'inner': Xstart, Xend = inner_point.runInner(timestamps) elif alg == 'budget': Xstart, Xend = budget.runBudget(timestamps) else: print('no algorithm specified') exit() print print 'relative total length of solution =', utils.getCost( Xstart, Xend) / ((event_length - 1) * num_nodes) print 'relative maximum length of solution =', utils.getMax( Xstart, Xend) / (event_length - 1) p, r, f = utils.compareGT(Xstart, Xend, active_truth, timestamps) print 'precision =', p print 'recall =', r print 'f-measure =', f
def __init__(self, system): self.lastts = 0 self.system = system self.ts = system.me.rank self.trusted = getMax(system.processes)
for li in range(0, len(line)): current_line.append(line[li]) recommendations = [] accuracies = [] for ri in range(0, len(testResourcesTS)): X0 = numpy.array(testResourcesTS[ri].features).reshape((1, -1)) Xr = X0 if use_ml_model == 1: Xr = scaler.transform(X0) #6.3 - get prediction rPred = model.predict(Xr) predTask = taskSet[rPred[0]] rProb = model.predict_proba(Xr) maxProb = getMax(rProb[0]) #6.4 - compare predicted and real value realTasks = testResourcesTS[ri].nextTask if maxProb >= current_threshold: recommendations.append(1) if predTask in realTasks: accuracies.append(1) else: accuracies.append(0) else: recommendations.append(0) #6.5 - record avg results for test resources at ts avgRecommendations = getAvg(recommendations) avgAccuracy = getAvg(accuracies)
def get_flip_score(self): cannon_len = sum(b['type'] == 2 for b in self.ai_unflipped) big_score = 0 big_block = {} current_round = self.blocks_map unflipped = self.ai_unflipped + self.player_unflipped unflipped_map = {} for b in unflipped: right_one = self.blocks_map.get(toStr(b['x'] + 1, b['y']), None) right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y']), None) left_one = self.blocks_map.get(toStr(b['x'] - 1, b['y']), None) left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y']), None) up_one = self.blocks_map.get(toStr(b['x'], b['y'] + 1), None) up_two = self.blocks_map.get(toStr(b['x'], b['y'] + 2), None) down_one = self.blocks_map.get(toStr(b['x'], b['y'] - 1), None) down_two = self.blocks_map.get(toStr(b['x'], b['y'] - 2), None) up_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 1), None) up_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 1), None) down_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 1), None) down_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 1), None) # flip, if cannons in unfliped blocks if cannon_len > 0: init_score = -9999 if right_two and right_two[ 'hasFlip'] and self.side != right_two[ 'side'] and right_two['type'] != 0: if right_one and not right_one['hasFlip']: score = POINTS[right_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif left_two and left_two['hasFlip'] and self.side != left_two[ 'side'] and left_two['type'] != 0: if left_one and not left_one['hasFlip']: score = POINTS[left_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_two and up_two['hasFlip'] and self.side != up_two[ 'side'] and up_two['type'] != 0: if up_one and not up_one['hasFlip']: score = POINTS[up_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_two and down_two['hasFlip'] and self.side != down_two[ 'side'] and down_two['type'] != 0: if down_one and not down_one['hasFlip']: score = POINTS[down_two['type']] / 50 * cannon_len if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score else: score = self.expect_score / 50 unflipped_map[toStr(b['x'], b['y'])] = score # flip, if no cannon in unfliped blocks else: init_score = -9999 if right_one and right_one[ 'hasFlip'] and self.side != right_one['side']: score = (self.expect_score - POINTS[right_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif left_one and left_one[ 'hasFlip'] and self.side != left_one['side']: score = (self.expect_score - POINTS[left_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_one and up_one[ 'hasFlip'] and self.side != up_one['side']: score = (self.expect_score - POINTS[up_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_one and down_one[ 'hasFlip'] and self.side != down_one['side']: score = (self.expect_score - POINTS[down_one['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_right and up_right[ 'hasFlip'] and self.side != up_right['side']: score = (self.expect_score - POINTS[up_right['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif up_left and up_left[ 'hasFlip'] and self.side != up_left['side']: score = (self.expect_score - POINTS[up_left['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_right and down_right[ 'hasFlip'] and self.side != down_right['side']: score = (self.expect_score - POINTS[down_right['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score elif down_left and down_left[ 'hasFlip'] and self.side != down_left['side']: score = (self.expect_score - POINTS[down_left['type']]) / 50 if score > init_score: init_score = score unflipped_map[toStr(b['x'], b['y'])] = score else: score = self.expect_score / 50 unflipped_map[toStr(b['x'], b['y'])] = score if unflipped_map: max_block_key = getMax(unflipped_map) big_block = self.blocks_map.get(max_block_key) big_score = max(unflipped_map.values()) detail = {} for xy in unflipped_map: detail[xy] = {'score': unflipped_map[xy]} return { 'action': 'flip', 'score': big_score, 'block': big_block, 'from': None, 'detail': detail } else: return { 'action': 'flip', 'score': None, 'big_block': None, 'from': None, 'detail': None }
def get_move_score(self): big_score = 0 big_block = {} ai_kill_block = {} current_round = self.blocks_map move_map = {} to_map = {} for b in self.ai_flipped: right_one = self.blocks_map.get(toStr(b['x'] + 1, b['y']), None) right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y']), None) right_three = self.blocks_map.get(toStr(b['x'] + 3, b['y']), None) right_up_two = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 2), None) right_down_two = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 2), None) left_one = self.blocks_map.get(toStr(b['x'] - 1, b['y']), None) left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y']), None) left_three = self.blocks_map.get(toStr(b['x'] - 3, b['y']), None) left_up_two = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 2), None) left_down_two = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 2), None) up_one = self.blocks_map.get(toStr(b['x'], b['y'] + 1), None) up_two = self.blocks_map.get(toStr(b['x'], b['y'] + 2), None) up_three = self.blocks_map.get(toStr(b['x'], b['y'] + 3), None) down_one = self.blocks_map.get(toStr(b['x'], b['y'] - 1), None) down_two = self.blocks_map.get(toStr(b['x'], b['y'] - 2), None) down_three = self.blocks_map.get(toStr(b['x'], b['y'] - 3), None) up_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] + 1), None) up_right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y'] + 1), None) up_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] + 1), None) up_left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y'] + 1), None) down_right = self.blocks_map.get(toStr(b['x'] + 1, b['y'] - 1), None) down_right_two = self.blocks_map.get(toStr(b['x'] + 2, b['y'] - 1), None) down_left = self.blocks_map.get(toStr(b['x'] - 1, b['y'] - 1), None) down_left_two = self.blocks_map.get(toStr(b['x'] - 2, b['y'] - 1), None) # can kill, not cannon init_score = -9999 if right_one and right_one['hasFlip'] and self.side != right_one[ 'side'] and b['type'] != 2 and vs(b, right_one): score = POINTS[right_one['type']] if (right_two and right_two['hasFlip'] and self.side != right_two['side'] and vs(b, right_two)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(b, up_right)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(b, down_right)): if vs(b, right_two): score = score + POINTS[right_two['type']] / 10 if vs(b, up_right): score = score + POINTS[up_right['type']] / 10 if vs(b, down_right): score = score + POINTS[down_right['type']] / 10 if [vs(b, right_two), vs(b, up_right), vs(b, down_right)].count(True) > 1: vs1 = POINTS[right_two['type']] if vs(b, right_two) else 0 vs2 = POINTS[up_right['type']] if vs(b, up_right) else 0 vs3 = POINTS[down_right['type']] if vs( b, down_right) else 0 score = score + min([vs1, vs2, vs3]) # kill and then be killed, killed by cannon or others if (right_two and right_two['hasFlip'] and self.side != right_two['side'] and vs(right_two, b)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(up_right, b)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(down_right, b)) or\ (right_three and right_three['hasFlip'] and self.side != right_three['side'] and right_three['type'] == 2) or\ (right_up_two and right_up_two['hasFlip'] and self.side != right_up_two['side'] and right_up_two['type'] == 2) or\ (right_down_two and right_down_two['hasFlip'] and self.side != right_down_two['side'] and right_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = right_one elif left_one and left_one['hasFlip'] and self.side != left_one[ 'side'] and b['type'] != 2 and vs(b, left_one): score = POINTS[left_one['type']] if (left_two and left_two['hasFlip'] and self.side != left_two['side'] and vs(b, left_two)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(b, up_left)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(b, down_left)): if vs(b, left_two): score = score + POINTS[left_two['type']] / 10 if vs(b, up_left): score = score + POINTS[up_left['type']] / 10 if vs(b, down_left): score = score + POINTS[down_left['type']] / 10 if [vs(b, left_two), vs(b, up_left), vs(b, down_left)].count(True) > 1: vs1 = POINTS[left_two['type']] if vs(b, left_two) else 0 vs2 = POINTS[up_left['type']] if vs(b, up_left) else 0 vs3 = POINTS[down_left['type']] if vs(b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if (left_two and left_two['hasFlip'] and self.side != left_two['side'] and vs(left_two, b)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(up_left, b)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(down_left, b)) or\ (left_three and left_three['hasFlip'] and self.side != left_three['side'] and left_three['type'] == 2) or\ (left_up_two and left_up_two['hasFlip'] and self.side != left_up_two['side'] and left_up_two['type'] == 2) or\ (left_down_two and left_down_two['hasFlip'] and self.side != left_down_two['side'] and left_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = left_one elif up_one and up_one['hasFlip'] and self.side != up_one[ 'side'] and b['type'] != 2 and vs(b, up_one): score = POINTS[up_one['type']] if (up_two and up_two['hasFlip'] and self.side != up_two['side'] and vs(b, up_two)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(b, up_left)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(b, up_right)): if vs(b, up_two): score = score + POINTS[up_two['type']] / 10 if vs(b, up_left): score = score + POINTS[up_left['type']] / 10 if vs(b, up_right): score = score + POINTS[up_right['type']] / 10 if [vs(b, up_two), vs(b, up_left), vs(b, up_right)].count(True) > 1: vs1 = POINTS[up_two['type']] if vs(b, up_two) else 0 vs2 = POINTS[up_left['type']] if vs(b, up_left) else 0 vs3 = POINTS[up_right['type']] if vs(b, up_right) else 0 score = score + min([vs1, vs2, vs3]) if (up_two and up_two['hasFlip'] and self.side != up_two['side'] and vs(up_two, b)) or\ (up_left and up_left['hasFlip'] and self.side != up_left['side'] and vs(up_left, b)) or\ (up_right and up_right['hasFlip'] and self.side != up_right['side'] and vs(up_right, b)) or\ (up_three and up_three['hasFlip'] and self.side != up_three['side'] and up_three['type'] == 2) or\ (up_right_two and up_right_two['hasFlip'] and self.side != up_right_two['side'] and up_right_two['type'] == 2) or\ (up_left_two and up_left_two['hasFlip'] and self.side != up_left_two['side'] and up_left_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = up_one elif down_one and down_one['hasFlip'] and self.side != down_one[ 'side'] and b['type'] != 2 and vs(b, down_one): score = POINTS[down_one['type']] if (down_two and down_two['hasFlip'] and self.side != down_two['side'] and vs(b, down_two)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(b, down_right)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(b, down_left)): if vs(b, down_two): score = score + POINTS[down_two['type']] / 10 if vs(b, down_right): score = score + POINTS[down_right['type']] / 10 if vs(b, down_left): score = score + POINTS[down_left['type']] / 10 if [vs(b, down_two), vs(b, down_right), vs(b, down_left)].count(True) > 1: vs1 = POINTS[down_two['type']] if vs(b, down_two) else 0 vs2 = POINTS[down_right['type']] if vs( b, down_right) else 0 vs3 = POINTS[down_left['type']] if vs(b, down_left) else 0 score = score + min([vs1, vs2, vs3]) if (down_two and down_two['hasFlip'] and self.side != down_two['side'] and vs(down_two, b)) or\ (down_right and down_right['hasFlip'] and self.side != down_right['side'] and vs(down_right, b)) or\ (down_left and down_left['hasFlip'] and self.side != down_left['side'] and vs(down_left, b)) or\ (down_three and down_three['hasFlip'] and self.side != down_three['side'] and down_three['type'] == 2) or\ (down_right_two and down_right_two['hasFlip'] and self.side != down_right_two['side'] and down_right_two['type'] == 2) or\ (down_left_two and down_left_two['hasFlip'] and self.side != down_left_two['side'] and down_left_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: init_score = score move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = down_one # can kill, cannon elif b['type'] == 2: if self.cannon_move(b): ck = self.cannon_move(b) for cb in ck: if ck[cb]: vb = ck[cb] cannon_right_one = self.blocks_map.get( toStr(vb['x'] + 1, vb['y']), None) cannon_right_two = self.blocks_map.get( toStr(vb['x'] + 2, vb['y']), None) cannon_left_one = self.blocks_map.get( toStr(vb['x'] - 1, vb['y']), None) cannon_left_two = self.blocks_map.get( toStr(vb['x'] - 2, vb['y']), None) cannon_up_one = self.blocks_map.get( toStr(vb['x'], vb['y'] + 1), None) cannon_up_two = self.blocks_map.get( toStr(vb['x'], vb['y'] + 2), None) cannon_down_one = self.blocks_map.get( toStr(vb['x'], vb['y'] - 1), None) cannon_down_two = self.blocks_map.get( toStr(vb['x'], vb['y'] - 2), None) if vb['hasFlip'] and self.side != vb['side']: score = POINTS[vb['type']] if not vb['hasFlip']: score = -(self.expect_score) * 3 vs1 = 0 vs2 = 0 vs3 = 0 vs4 = 0 if cannon_right_two and cannon_right_two[ 'hasFlip'] and self.side != cannon_right_two[ 'side']: vs1 = POINTS[cannon_right_two['type']] / 10 if cannon_left_two and cannon_left_two[ 'hasFlip'] and self.side != cannon_left_two[ 'side']: vs2 = POINTS[cannon_left_two['type']] / 10 if cannon_up_two and cannon_up_two[ 'hasFlip'] and self.side != cannon_up_two[ 'side']: vs3 = POINTS[cannon_up_two['type']] / 10 if cannon_down_two and cannon_down_two[ 'hasFlip'] and self.side != cannon_down_two[ 'side']: vs3 = POINTS[cannon_down_two['type']] / 10 if [vs1, vs2, vs3, vs4].count(0) == 3: score = score + vs1 + vs2 + vs3 + vs4 if [vs1, vs2, vs3, vs4].count(0) < 3: l = [vs1, vs2, vs3, vs4] l.remove(0) score = score + min(l) if (cannon_right_one and cannon_right_one['hasFlip'] and self.side != cannon_right_one['side'] and vs(cannon_right_one, b)) or\ (cannon_left_one and cannon_left_one['hasFlip'] and self.side != cannon_left_one['side'] and vs(cannon_left_one, b)) or\ (cannon_up_one and cannon_up_one['hasFlip'] and self.side != cannon_up_one['side'] and vs(cannon_up_one, b)) or\ (cannon_down_one and cannon_down_one['hasFlip'] and self.side != cannon_down_one['side'] and vs(cannon_down_one, b)) or\ (cannon_right_two and cannon_right_two['hasFlip'] and self.side != cannon_right_two['side'] and cannon_right_two['type'] == 2) or\ (cannon_left_two and cannon_left_two['hasFlip'] and self.side != cannon_left_two['side'] and cannon_left_two['type'] == 2) or\ (cannon_up_two and cannon_up_two['hasFlip'] and self.side != cannon_up_two['side'] and cannon_up_two['type'] == 2) or\ (cannon_down_two and cannon_down_two['hasFlip'] and self.side != cannon_down_two['side'] and cannon_down_two['type'] == 2): score = 0 - POINTS[b['type']] if score > init_score: move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = vb else: # TODO continue score = 0 move_map[toStr(b['x'], b['y'])] = score to_map[toStr(b['x'], b['y'])] = None if move_map: max_block_key = getMax(move_map) big_block = self.blocks_map.get(max_block_key) big_score = max(move_map.values()) ai_kill_block = to_map.get(max_block_key) detail = {} for xy in move_map: detail[xy] = {'score': move_map[xy], 'to': to_map[xy]} return { 'action': 'move', 'score': big_score, 'block': ai_kill_block, 'from': big_block, 'detail': detail } else: return { 'action': 'move', 'score': None, 'block': None, 'from': None, 'detail': None }