def recurse(xis, vis, actives, xd): if sum(actives) == 0: return xis, vis, actives, xd for i in range(len(xis)): if actives[i]: dr = DominantRegion([self.r], vis[i], [self.vmax], xis[i], [xd], offset=0) xw = self.target.deepest_point_in_dr(dr) dt = dist(xw, xis[i]) / vis[i] xd = xd + dt * self.vmax * (xw - xd) / dist(xw, xd) # xis[i] = np.array([x for x in xw]) xis[i] = xw actives[i] = 0 break for j in range(i + 1, len(xis)): if actives[j]: dr = DominantRegion([self.r], vis[j], [self.vmax], xis[j], [xd], offset=self.vmax * dt) xw = self.target.deepest_point_in_dr(dr) e = (xw - xis[j]) / dist(xw, xis[j]) xi = xis[j] + e * vis[j] * dt # xis[j] = np.array([x for x in xi]) xis[j] = xi return recurse(xis, vis, actives, xd)
def strategy(self): # copy to prevent change during iteration oppo_dict = {k: v for k, v in self.state_oppo_neigh.items()} xds, vds = [], [] for d, state in oppo_dict.items(): xds.append(np.array([x for x in state.x])) vds.append(state.speed) # print(str(self), vs, vd) if xds: # TODO: to add velocity estimation # dr = DominantRegion(self.env.target.size, vd/norm(self.state.v), self.state.x, xds, offset=0) dr = DominantRegion([self.r] * len(xds), self.vmax, vds, self.state.x, xds, offset=0) xw = self.target.deepest_point_in_dr(dr) dx = xw - self.state.x dist = norm(dx) if dist > 1e-6: return self.vmax * dx / dist dx = np.array([self.target.x0, self.target.y0]) - self.state.x return vmax * dx / norm(dx)
def strategy(self, iselect_mode='emin'): # iselect_mode = value, emin, emax # copy this to prevent being empty again after the following if statement # pref_dict = {k: v for k, v in self.state.pref.items()} pref_dict = dict() for p in self.state.pref: if p in self.state_oppo_neigh: pref_dict.update({p:{'s':self.state_oppo_neigh[p], 'e':self.state.pref[p]}}) if not pref_dict: return np.array([0, 0]) # TODO: return to x0 n_pref = len(pref_dict) # select the current intruder by value # if self.iselect_mode == 'value': if n_pref < 4: # value orders = [order for order in itertools.permutations(pref_dict)] values = [self.value_order(pref_dict, order) for order in orders] icurr = orders[values.index(max(values))][0] # elif self.iselect_mode == 'emin': else: # emin if self.iprev not in pref_dict: # include self.iprev == None icurr = [p for p in pref_dict][-1] self.iprev = icurr else: icurr = self.iprev # elif self.iselect_mode == 'emax': # if self.iprev not in pref_dict: # include self.iprev == None # icurr = [p for p in pref_dict][0] # self.iprev = icurr # else: # icurr = self.iprev # self.state_oppo_neigh could be changed by other threads # likely when icurr captured by other defenders if icurr not in self.state_oppo_neigh: return np.array([0, 0]) # TODO: return to x0 istate = self.state_oppo_neigh[icurr] dr = DominantRegion([self.r], istate.speed, [self.vmax], istate.x, [self.state.x], offset=0) xw = self.target.deepest_point_in_dr(dr) dx = xw - self.state.x with open(self.datadir+'/Itarg.csv', 'a') as f: f.write('%.4f,%s,%.4f,%s\n'%(self.state.t, icurr, pref_dict[icurr]['e'], '_'.join(list(map(str, [p + '=%.6f'%d['e'] for p, d in pref_dict.items()]))))) # f.write('%.4f,%s,%.4f,%s\n'%(self.state.t, icurr, self.state.pref_dict[icurr], self.prefdict_to_prefstring(pref_dict))) return self.vmax*dx/norm(dx)
def get_efficiency(self, xi, vd, vi): dr = DominantRegion([self.r], vi, [vd], xi, [self.state.x], offset=0) xw = self.target.deepest_point_in_dr(dr) tlevel = self.target.level(xw) dlevel = dist(xw, self.state.x) return tlevel/dlevel