def get_alpha_G(self, K): robot_ids = Set([e.id for e in self.robots]) task_ids = Set([e.id for e in self.tasks]) V = Set.descartes_product(robot_ids, task_ids) V_K = Set([Set(e) for e in itertools.combinations(V, K)]) S_K = self.history S_K_1 = Set(self.history[:-1]) print("...Calculating alpha_G...") alpha_G = -float('inf') N = len(V_K) progress_bar = ProgressBar(N) instrument = Instrument() instrument.start() k = 0 for Omega in V_K: progress_bar.progress(k) k = k + 1 for j_i in S_K_1.setminus(Omega): i = S_K.index(j_i) S_i_1 = Allocation(self.history[0:i]) lhs = Allocation(S_i_1.union(Omega)).get_derivative(Set([j_i])) rhs = S_i_1.get_derivative(Set([j_i])) frac = (lhs - rhs) / lhs if frac > alpha_G and rhs != 0 and lhs != 0: alpha_G = frac progress_bar.progress(N, 'Finished!\n') calculation_time = instrument.stop() print("Calculation time [s]:", calculation_time) print("Finished calculating alpha_G!\n") return alpha_G
def __init__(self, function_frame): Allocation.set_up(function_frame) self.function_frame = function_frame self.path_planner = function_frame.path_planner self.robots = copy.deepcopy(function_frame.robots) self.tasks = copy.deepcopy(function_frame.tasks) self.instrument = Instrument()
def get_gamma_G(self, K): robot_ids = Set([e.id for e in self.robots]) task_ids = Set([e.id for e in self.tasks]) V = Set.descartes_product(robot_ids, task_ids) V_K = Set([Set(e) for e in itertools.combinations(V, K)]) print("...Calculating gamma_G...") gamma_G = float('inf') N = len(self.history) * len(V_K) progress_bar = ProgressBar(N) instrument = Instrument() instrument.start() k = 0 for t in range(len(self.history)): S_t = Allocation(self.history[0:t]) for Omega in V_K: progress_bar.progress(k) k = k + 1 lhs = 0 for omega in Omega.setminus(Set(S_t)): lhs = lhs + S_t.get_derivative(Set([omega])) rhs = S_t.get_derivative(Omega) frac = rhs / lhs if frac < gamma_G and rhs != 0 and lhs != 0: gamma_G = frac progress_bar.progress(N, 'Finished!\n') calculation_time = instrument.stop() print("Calculation time [s]:", calculation_time) print("Finished calculating gamma_G!\n") return gamma_G
def get_allocation(self, assignment): allocation = Allocation([]) for i_e, e in enumerate(assignment): r_id = self.robots[e].id a_id = self.tasks[i_e].id allocation.add(r_id, a_id) return allocation
def test_making_allocation(self): from AllocationService import AllocationService from Allocation import Allocation AllocationService.initialize() allocationObj=Allocation() allocationObj.createExecutionContext() allocationObj.allocate()
def mkTrace(trace_filename, first, last, raw_inputs): print "Loading trace.." reil_code = ReilPath(trace_filename, first, last) Inputs = parse_inputs(raw_inputs) if (raw_inputs <> []): print "Using these inputs.." for op in Inputs: print op,"=", Inputs[op] print "Detecting callstack layout..." Callstack = CS(reil_code)#, Inputs) #TODO: it should recieve inputs also! reil_code.reset() print Callstack AllocationLog = Allocation() MemAccess = MemAccessREIL() FuncParameters = FuncParametersREIL() reil_size = len(reil_code) start = 0 Callstack.reset() print "Detecting memory accesses and function parameters.." for (end,ins) in enumerate(reil_code): Callstack.nextInstruction(ins) if ins.instruction in ["stm", "ldm"]: MemAccess.detectMemAccess(reil_code[start:end+1], Callstack, Inputs, end) AllocationLog.check(MemAccess.getAccess(end), end) elif ins.isCall() and ins.called_function <> None: ##print "detect parameters of", ins.called_function, "at", ins_str FuncParameters.detectFuncParameters(reil_code[start:end+1], MemAccess, Callstack, Inputs, end) if (ins.called_function == "malloc"): try: size = int(FuncParameters.getParameters(end)[0][1].name) except ValueError: size = None AllocationLog.alloc(ins.address, end, size) elif (ins.called_function == "free"): ptr = (FuncParameters.getParameters(end)[0][1].mem_source) AllocationLog.free(ptr, end) print MemAccess print FuncParameters AllocationLog.report() Callstack.reset() reil_code.reset() # trace definition trace = dict() trace["code"] = reil_code trace["initial_conditions"] = Inputs trace["final_conditions"] = dict() trace["callstack"] = Callstack trace["mem_access"] = MemAccess trace["func_parameters"] = FuncParameters return trace
def mkTrace(path, raw_inputs, debug = False): if debug: print "Loading trace.." inputs = parse_inputs(raw_inputs) #if (raw_inputs <> []): # print "Using these inputs.." # for op in Inputs: # print op,"=", Inputs[op] if debug: print "Detecting callstack layout..." callstack = Callstack(path)#, Inputs) #TODO: it should recieve inputs also! if debug: print callstack allocationLog = Allocation() memAccess = MemAccess() funcParameters = FuncParameters() path_size = len(path) # we reset path iterator and callstack path.reset() callstack.reset() #print "Detecting memory accesses and function parameters.." for ins in path: counter = ins.getCounter() callstack.nextInstruction(ins) #print ins,counter if ins.isReadWrite(): memAccess.detectMemAccess(path[0:counter+1], callstack, inputs, counter) #AllocationLog.check(MemAccess.getAccess(end), end) elif ins.isCall() and ins.called_function <> None: funcParameters.detectFuncParameters(path[0:counter+1], memAccess, callstack, inputs, counter) #if (ins.called_function == "malloc"): #try: #size = int(FuncParameters.getParameters(end)[0][1].name) #except ValueError: #size = None #AllocationLog.alloc(ins.address, end, size) #elif (ins.called_function == "free"): #ptr = (FuncParameters.getParameters(end)[0][1].mem_source) #AllocationLog.free(ptr, end) if debug: print memAccess print funcParameters allocationLog.report() callstack.reset() path.reset() # trace definition trace = dict() trace["code"] = path trace["initial_conditions"] = inputs trace["final_conditions"] = dict() trace["callstack"] = callstack trace["mem_access"] = memAccess trace["func_parameters"] = funcParameters return trace
def get_allocation(self): allocation = Allocation([]) for r in self.robots: for a in r.S_r: allocation.add(r.id, a.id) return allocation
def brute_force(n, m, eval_func, log = True, alpha=1): d = np.zeros((m+1, )) leximin = Allocation(n, m, eval_matrix) max_obj = 0 max_allocation = Allocation(n, m, eval_matrix) flag = False epsilon = 0.01 while d[0] == 0: new_allocation = Allocation(n, m, eval_func) for i in range(1, m+1): new_allocation.allocate(d[i], i - 1) efx, pair = new_allocation.is_EFX() u = new_allocation.utility_measure() u = np.array(u).astype("float") u = u + epsilon u = np.power(u, alpha) obj = np.sum(u) if efx and not flag: if log: print("One possible EFX Allocation is: ") print(new_allocation) flag = True if leximin < new_allocation: leximin = new_allocation if obj > max_obj: max_obj = obj max_allocation = new_allocation j = m d[j] += 1 while d[j] == n: d[j] = 0 j -= 1 d[j] += 1 if not flag and log: print("There is No EFX Allocation.") if log: print("Leximin Allocation is:") print(leximin) lex_efx, max_pair = leximin.is_EFX() max_efx = max_allocation.is_EFX() if lex_efx: #if log: print("Leximin Allocation is a EFX Allocation") else: #if log: print(eval_func) print(leximin) print(max_pair) print(max_allocation) print("Leximin Allocation is not a EFX Allocation") if max_efx: print("Max %f objective is a EFX Allocation" % (alpha,)) else: print(eval_func) print(max_allocation) print("Max %f objective is not a EFX Allocation" % (alpha,)) temp = input() return flag
def mkTrace(path, raw_inputs, debug=False): if debug: print "Loading trace.." inputs = parse_inputs(raw_inputs) #if (raw_inputs <> []): # print "Using these inputs.." # for op in Inputs: # print op,"=", Inputs[op] if debug: print "Detecting callstack layout..." callstack = Callstack( path) #, Inputs) #TODO: it should recieve inputs also! if debug: print callstack allocationLog = Allocation() memAccess = MemAccess() funcParameters = FuncParameters() path_size = len(path) # we reset path iterator and callstack path.reset() callstack.reset() #print "Detecting memory accesses and function parameters.." for ins in path: counter = ins.getCounter() callstack.nextInstruction(ins) #print ins,counter if ins.isReadWrite(): memAccess.detectMemAccess(path[0:counter + 1], callstack, inputs, counter) #AllocationLog.check(MemAccess.getAccess(end), end) elif ins.isCall() and ins.called_function <> None: funcParameters.detectFuncParameters(path[0:counter + 1], memAccess, callstack, inputs, counter) #if (ins.called_function == "malloc"): #try: #size = int(FuncParameters.getParameters(end)[0][1].name) #except ValueError: #size = None #AllocationLog.alloc(ins.address, end, size) #elif (ins.called_function == "free"): #ptr = (FuncParameters.getParameters(end)[0][1].mem_source) #AllocationLog.free(ptr, end) if debug: print memAccess print funcParameters allocationLog.report() callstack.reset() path.reset() # trace definition trace = dict() trace["code"] = path trace["initial_conditions"] = inputs trace["final_conditions"] = dict() trace["callstack"] = callstack trace["mem_access"] = memAccess trace["func_parameters"] = funcParameters return trace