def update_goAround(obstacle): if (self.goAround == 1): self.x += sin(tan_inv(obstacle.y - self.y / obstacle.x - self.x)) self.y += cos(tan_inv(obstacle.y - self.y / obstacle.x - self.x)) if ((pow(abs(self.x - obstacle.x), 2) + pow(abs(self.y - obstacle.y), 2)) >= 100): self.goAround = 2 return if (self.goAround == 2): self.x += sin( tan_inv(self.target_y - self.y / self.target_x - self.x)) self.y += cos( tan_inv(self.target_y - self.y / self.target_x - self.x))
def update_target(self, alpha, explore, x_limit, y_limit, mean_x, mean_y): temp_x = self.x * (1 - alpha) + alpha * explore * ( self.x + (random() * (x_limit / 2) - (x_limit / 4))) + alpha * (1 - explore) * mean_x temp_y = self.y * (1 - alpha) + alpha * explore * ( self.y + (random() * (y_limit / 2) - (y_limit / 4))) + alpha * (1 - explore) * mean_y # flag=0 # if(explore==1): # if(self.x>x_limit-25 or self.x<-x_limit+25): # temp_x=-self.x # flag=1 # else: # temp_x=self.x # if(self.y>y_limit-25 or self.y<-y_limit+25): # temp_y=-self.y # flag=1 # else: # temp_y=self.y # if(flag==1): # self.angle=tan_inv(temp_y,temp_x) # return # temp_x=self.x*(1-alpha)+alpha*explore*(self.x+(random()*(x_limit/2)-(x_limit/4)))+alpha*(1-explore)*mean_x # temp_y=self.y*(1-alpha)+alpha*explore*(self.y+(random()*(y_limit/2)-(y_limit/4)))+alpha*(1-explore)*mean_y if (temp_x > x_limit or temp_x < -x_limit): while (not (temp_x > x_limit or temp_x < -x_limit)): temp_x = self.x + (random() * (x_limit / 2) - (x_limit / 4)) if (temp_y > y_limit or temp_y < -y_limit): while (not (temp_y > y_limit or temp_y < -y_limit)): temp_y = self.y + (random() * (x_limit / 2) - (x_limit / 4)) self.target_x = temp_x self.target_y = temp_y self.angle = tan_inv((temp_y - self.y), (temp_x - self.x))
def update_target_obst(self, x_limit, y_limit, mean_x, mean_y): temp_x = mean_x temp_y = mean_y # if(temp_x>x_limit or temp_x<-x_limit): # temp_x=self.x+(random()*(x_limit/2)-(x_limit/4)) # if(temp_y>y_limit or temp_y<-y_limit): # temp_y=self.y+(random()*(x_limit/2)-(x_limit/4)) # temp_x=0.7*temp_x + 0.3*(self.x+(random()*(x_limit/2)-(x_limit/4))) # temp_y=0.7*temp_x + 0.3*(self.y+(random()*(y_limit/2)-(y_limit/4))) self.target_x = temp_x self.target_y = temp_y self.angle = tan_inv((temp_y - self.y), (temp_x - self.x))
def update_target(self, alpha, explore, x_limit, y_limit, mean_x, mean_y): if self.sleep: self.sleep = False return temp_x = self.x * (1 - alpha) + alpha * explore * ( self.x + (random() * (x_limit / 2) - (x_limit / 4))) + alpha * (1 - explore) * mean_x temp_y = self.y * (1 - alpha) + alpha * explore * ( self.y + (random() * (y_limit / 2) - (y_limit / 4))) + alpha * (1 - explore) * mean_y if (temp_x > x_limit or temp_x < -x_limit): while (not (temp_x > x_limit or temp_x < -x_limit)): temp_x = self.x + (random() * (x_limit / 2) - (x_limit / 4)) if (temp_y > y_limit or temp_y < -y_limit): while (not (temp_y > y_limit or temp_y < -y_limit)): temp_y = self.y + (random() * (x_limit / 2) - (x_limit / 4)) self.target_x = temp_x self.target_y = temp_y self.angle = tan_inv((temp_y - self.y), (temp_x - self.x))
def main_obstacle_1(no_targets, no_observers, no_obstacles, targets, observers, obstacles): step = 0 data_until_update = [] total_count_obst = 0 while (step <= total_steps): if (step % update_steps == 0): [observer_target_dict, observer_obstacle_dict ] = update_for_observers(observers, targets, obstacles) [target_observer_dict, target_obstacle_dict ] = update_for_targets(observers, targets, obstacles) for i in observer_target_dict: temp_arr_x = [] temp_arr_y = [] for j in observer_target_dict[i]: temp_arr_x.append(targets[j].x) temp_arr_y.append(targets[j].y) if (len(temp_arr_x) and len(temp_arr_y)): mean_x = mean(temp_arr_x) mean_y = mean(temp_arr_y) explore = pow(1 / (len(observer_target_dict[i]) + 1), 2) rwrd = reward_obs_1(observers[i], targets, observer_target_dict[i], obstacles, observer_obstacle_dict[i], x_limit, y_limit, explore, mean_x, mean_y) E_min = LP_CTO(rwrd, 1.0, template_probability_distribution)[0] alpha = BRLP_CTO(rwrd, template_probability_distribution, E_min) observers[i].update_target(alpha, explore, x_limit, y_limit, mean_x, mean_y) else: observers[i].update_target(1, 1, x_limit, y_limit, 0, 0) for i in target_observer_dict: temp_arr_x = [] temp_arr_y = [] for j in target_observer_dict[i]: temp_arr_x.append(observers[j].x) temp_arr_y.append(observers[j].y) target_obstacle_dict[i].sort(reverse=True, key=lambda i: obstacles[i][0]) if (len(temp_arr_x) and len(temp_arr_y)): mean_x = mean(temp_arr_x) mean_y = mean(temp_arr_y) obst_idx = -1 tan_inv_ot = tan_inv((targets[i].y - mean_y), (targets[i].x - mean_x)) for j in target_obstacle_dict[i]: tan_inv_oo = tan_inv((obstacles[j][1][0].y - mean_y), (obstacles[j][1][0].x - mean_x)) if (abs(tan_inv_oo - tan_inv_ot) <= math.pi / 2): obst_idx = j break if (obst_idx == -1): targets[i].update_target(x_limit, y_limit, mean_x, mean_y) else: sx = 0 sy = 0 for j in obstacles[obst_idx][1]: sx += j.x sy += j.y n = obstacles[obst_idx][0] targets[i].update_target_obst(x_limit, y_limit, sx / n, sy / n) for i in observers: i.update(x_limit, y_limit) for i in targets: i.update(x_limit, y_limit) step += 1 ans_dict = update_for_observers1(observers, targets) for i in ans_dict: for j in ans_dict[i]: total_count_obst += 1 return total_count_obst