Beispiel #1
0
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name

        # print ('Time {}, Bot {}, Node {}'.format(t, bot, node))
        neigh = list(self.graph.successors(node))
        if bot not in self.robots.keys():
            self.robots[bot] = {}
            self.robots[bot]['last_node'] = node
            self.robots[bot]['last_visit'] = t

        p_g = []
        for n in neigh:
            g = self.graph.nodes[n]['idleness'] / self.graph[node][n][
                'duration']
            g_1 = self.L * np.exp(g * np.log(1 / self.L) / self.M)
            s = len(self.graph.nodes[n]['future_visits'].keys())
            s_1 = 2**(self.num_robots - s - 1) / (2**self.num_robots - 1)
            p_g.append(g_1 * s_1)

        if len(neigh) > 1:
            max_ids = list(np.where(p_g == np.amax(p_g))[0])
            max_id = int(rn.sample(max_ids, 1)[0])
            next_node = neigh[max_id]
        else:
            next_node = neigh[0]
        next_walk = [node, next_node]
        next_departs = [t]
        self.graph.nodes[next_node]['future_visits'][bot] = self.graph[node][
            next_node]['duration']
        # print (next_walk, next_departs)
        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #2
0
    def callback_next_task(self, req):
        for cars in range(self.num_bots):
            node = req.node_done
            t = req.stamp
            bot = req.name
            neigh = list(self.graph.successors(node))
            idles = []
            if node not in self.dead_nodes:
                self.network_arr['node_{}'.format(node)][node][cars] = 0
                for neigh_node in list(self.graph.successors(node)) :
                    if neigh_node not in self.dead_nodes:
                        self.network_arr['node_{}'.format(neigh_node)][node][cars] = 0.

                for n in neigh:
                    # print(self.network_arr['node_{}'.format(node)][n][cars])
                    idles.append(self.network_arr['node_{}'.format(node)][n][cars])
            else:
                idles = [1 for i in range(len(neigh))]

            # print(cars,node,neigh,idles)

            max_id = 0
            if len(neigh) > 1:
                max_ids = list(np.where(idles == np.amax(idles))[0])
                max_id = rn.sample(max_ids, 1)[0]
            next_walk = [node, neigh[max_id]]
            next_departs = [t]
            return NextTaskBotResponse(next_departs, next_walk)
Beispiel #3
0
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name

        neigh = list(self.graph.successors(node))
        next_node = rn.sample(neigh, 1)[0]
        next_walk = [node, next_node]
        next_departs = [t]
        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #4
0
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp

        self.graph.nodes[node]['idleness'] = 0.

        neigh = list(self.graph.successors(node))
        idles = []
        for n in neigh:
            idles.append(self.graph.nodes[n]['idleness'])

        max_id = 0
        if len(neigh) > 1:
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]
        next_walk = [node, neigh[max_id]]
        next_departs = [t]
        return NextTaskBotResponse(next_departs, next_walk)
    def callback_next_task(self, req):

        node = req.node_done
        t = req.stamp
        bot = req.name

        neigh = list(self.graph.successors(node))
        idles = []
        for n in neigh:
            idles.append(
                (self.idle_expect[bot][n]) * (self.reward_coeff[bot][node][n]))

        max_id = 0
        if len(neigh) > 1:
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]

        next_walk = [node, neigh[max_id]]
        next_departs = [t]
        return NextTaskBotResponse(next_departs, next_walk)
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name

        self.robots[bot][node] = 0.

        idles = []
        for n in self.nodes:
            idles.append(self.robots[bot][n])

        max_id = 0
        max_ids = list(np.where(idles == np.amax(idles))[0])
        max_id = rn.sample(max_ids, 1)[0]
        dest_node = self.nodes[max_id]
        while dest_node == node:
            dest_node = rn.sample(self.nodes, 1)[0]
        next_walk = nx.dijkstra_path(g, node, dest_node, 'length')
        next_departs = [t] * (len(next_walk) - 1)
        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #7
0
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name

        temp = self.nodes.copy()
        dest_node = rn.sample(temp, 1)[0]
        while dest_node == node or dest_node in self.robots.values():
            if len(temp) == 1 and dest_node != node:
                dest_node = temp[0]
                break
            elif len(temp) == 1 and dest_node == node:
                temp = self.nodes.copy()
                temp.remove(node)
                dest_node = rn.sample(temp, 1)[0]
            temp.remove(dest_node)
            dest_node = rn.sample(temp, 1)[0]
        next_walk = nx.dijkstra_path(g, node, dest_node, 'length')
        self.robots[bot] = dest_node
        next_departs = [t] * (len(next_walk) - 1)
        return NextTaskBotResponse(next_departs, next_walk)
    def callback_next_task(self, req):

        node = req.node_done
        t = req.stamp
        bot = req.name

        #this loop is to run if  bot_next_task is called before callback_idle
        if self.idle_true[node] != 0.:
            self.current_node[bot] = req.node_done
            expect = self.idle_expect[bot][node]
            true = self.idle_true[node]

            if self.old_node[bot] != 0.:

                if expect > true:
                    self.reward_coeff[bot][self.old_node[bot]][
                        self.current_node[bot]] -= ((0.9)**t) * (
                            (expect - true) / expect)
                else:
                    self.reward_coeff[bot][self.old_node[bot]][
                        self.current_node[bot]] = 1.
            self.old_node[bot] = req.node_done

        self.idle_expect[bot][node] = 0.
        self.idle_true[node] = 0.

        neigh = list(self.graph.successors(node))
        idles = []
        for n in neigh:
            idles.append(
                (self.idle_expect[bot][n]) * (self.reward_coeff[bot][node][n]))

        max_id = 0
        if len(neigh) > 1:
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]

        next_walk = [node, neigh[max_id]]
        next_departs = [t]
        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #9
0
 def callback_next_task(self, req):
     node = req.node_done
     t = req.stamp
     bot = req.name
     prob = np.random.random_sample()
     print('Time {}, Bot {}, Node {}'.format(t, bot, node))
     if prob < s.eps_prob:
         succ = np.random.choice(list(s.graph.successors(node)))
         next_walk = [node, succ]
         next_departs = [t]
         # print ('True Random')
     else:
         g_walk, _ = self.monte_carlo_sampling(node, self.plan_time)
         print(g_walk)
         next_walk = [node]
         next_departs = []
         for i, n in enumerate(g_walk[1:]):
             if n == next_walk[-1]:
                 t += s.cf
             if n != next_walk[-1]:
                 next_walk.append(n)
                 next_departs.append(t)
                 # print (n)
                 # print (self.graph[next_walk[-2]][next_walk[-1]]['duration'])
                 t += np.ceil(
                     self.graph[next_walk[-2]][next_walk[-1]]['duration'] /
                     self.cf) * self.cf
         if len(next_walk) == 1:
             # print ('Random')
             succ = np.random.choice(list(s.graph.successors(node)))
             next_walk = [node, succ]
             next_departs = [t]
         # else:
         # print ('Not random')
     for i, n in enumerate(next_walk[:-1]):
         self.graph.nodes[n]['future_visits'][
             bot] = next_departs[i] - req.stamp
     self.graph.nodes[next_walk[-1]]['future_visits'][bot] = self.plan_time
     return NextTaskBotResponse(next_departs, next_walk)
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name

        self.graph.nodes[node]['idleness'] = 0.

        
        idles = []
        for n in self.nodes:
            idles.append(self.graph.nodes[n]['idleness'])

        temp = self.nodes.copy()
        max_id = 0
        if len(self.nodes) > 1:
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]
        dest_node = temp[max_id]
        while dest_node == node or dest_node in self.robots.values():
            if len(temp) == 1 and dest_node != node:
                dest_node = temp[0]
                break
            elif len(temp) == 1 and dest_node == node:
                temp1 = self.nodes.copy()
                temp1.remove(node)
                dest_node = rn.sample(temp1, 1)[0]
                break
            temp.remove(dest_node)
            idles.pop(max_id)
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]
            dest_node = temp[max_id]
        next_walk = nx.dijkstra_path(g, node, dest_node, 'length')
        self.robots[bot] = dest_node
        next_departs = [t] * (len(next_walk) - 1)
        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #11
0
    def callback_next_task(self, req):
        t = req.stamp
        node = req.node_done

        if node in self.non_priority_assigned:
            self.non_priority_assigned.remove(node)

        if node in self.priority_nodes_cur:
            self.assigned[self.priority_nodes_cur.index(node)] = False

        print(node, self.priority_nodes_cur, self.assigned)

        best_reward = -np.inf
        next_walk = []

        self.graph.nodes[node]['idleness'] = 0.

        for j in range(len(self.priority_nodes_cur)):
            if not self.assigned[j]:
                valid_trails = '/valid_trails_{}_{}_{}.in'.format(
                    node, self.priority_nodes_cur[j],
                    str(int(self.time_periods[j])))
                with open(self.offline_folder + valid_trails, 'r') as f:
                    count = 0
                    for line in f:
                        count += 1
                        line1 = line.split('\n')
                        line2 = line1[0].split(' ')
                        r = self.tpbp_reward(line2)
                        if r > best_reward:
                            best_reward = r
                            next_walk = line2

        if all(self.assigned):
            print('alive')
            idles = []
            for i in self.non_priority_nodes:
                idles.append(self.graph.nodes[i]['idleness'])

            max_id = 0
            max_ids = list(np.where(idles == np.amax(idles))[0])
            max_id = rn.sample(max_ids, 1)[0]
            dest_node = self.non_priority_nodes[max_id]
            temp = self.non_priority_nodes.copy()
            while dest_node in self.non_priority_assigned or dest_node == node:
                if len(temp) == 1:
                    if dest_node == node:
                        temp1 = self.non_priority_nodes.copy()
                        temp1.remove(node)
                        dest_node = rn.sample(temp1, 1)[0]
                    break
                else:
                    temp.remove(dest_node)
                    idles.pop(max_id)
                    max_ids = list(np.where(idles == np.amax(idles))[0])
                    max_id = rn.sample(max_ids, 1)[0]
                    dest_node = temp[max_id]
            if not dest_node in self.non_priority_assigned:
                self.non_priority_assigned.append(dest_node)
            next_walk = nx.dijkstra_path(g, node, dest_node, 'length')

        else:
            self.assigned[self.priority_nodes_cur.index(next_walk[-1])] = True

        next_departs = [t] * (len(next_walk) - 1)

        return NextTaskBotResponse(next_departs, next_walk)
Beispiel #12
0
    def callback_next_task(self, req):
        node = req.node_done
        t = req.stamp
        bot = req.name
        # prob = np.random.random_sample()
        print('Time {}, Bot {}, Node {}'.format(t, bot, node))
        priors = []
        neigh = list(self.graph.successors(node))
        sum_idle = 0
        conds = []
        prob_move = []
        chis = []
        if bot not in self.robots.keys():
            self.robots[bot] = {}
            self.robots[bot]['last_node'] = node
            self.robots[bot]['last_visit'] = t
            self.robots[bot]['arc_strengths'] = []
            for edge in self.edges:
                self.robots[bot]['arc_strengths'].append(self.k)
        sum_strength = np.sum(self.robots[bot]['arc_strengths'])
        for i in list(self.graph.nodes()):
            sum_idle += self.graph.nodes[i]['idleness']

        for n in neigh:
            if sum_idle > 0.:
                priors.append(self.graph.nodes[n]['idleness'] / sum_idle)
            else:
                priors.append(1. / len(neigh))
            edge_ind = self.edges.index((node, n))
            conds.append(self.robots[bot]['arc_strengths'][edge_ind] /
                         sum_strength)
            prob_move.append(priors[-1] * conds[-1] + self.eps)

        prob_move /= np.sum(prob_move)
        prob_move = list(prob_move)
        # print ('neigh', len(neigh), 'prob_move', prob_move)
        if len(neigh) > 1:
            ent = -np.sum(prob_move * np.log2(prob_move)) / np.log2(len(neigh))
            max_ids = list(np.where(prob_move == np.amax(prob_move))[0])
            max_id = rn.sample(max_ids, 1)[0]
        else:
            ent = 0.
            max_id = 0
        # print ('ent', ent)

        next_node = neigh[max_id]

        temp1 = prob_move.copy()
        temp2 = neigh.copy()
        while len(self.graph.nodes[next_node]['future_visits'].keys()) > 0:
            if len(temp1) == 1:
                break
            temp2.remove(next_node)
            # max_ids.pop(0)
            temp1.pop(max_id)
            # if len(max_ids) > 0:
            #     max_id = rn.sample(max_ids, 1)[0]
            #     next_node = temp2[max_id]
            # else:
            #     print (temp1)
            max_ids = list(np.where(temp1 == np.amax(temp1))[0])
            max_id = rn.sample(max_ids, 1)[0]
            next_node = temp2[max_id]

        for n in neigh:
            temp = self.graph.nodes[n]['num_visits']
            if n == next_node:
                temp += 1
            chis.append(temp / len(list(self.graph.predecessors(n))))

        pri_ids1 = list(np.where(priors == np.amax(priors))[0])
        pri_ids2 = list(np.where(priors == np.amin(priors))[0])
        chi_ids1 = list(np.where(chis == np.amax(chis))[0])
        chi_ids2 = list(np.where(chis == np.amin(chis))[0])

        for n in neigh:
            edge_ind = self.edges.index((node, n))
            s = 0.
            if len(neigh) > 1:
                if n in chi_ids1:
                    if (len(chi_ids1) > 1) and (n in pri_ids2):
                        s = -1.
                    elif (len(chi_ids1 == 1)):
                        s = -1.
                if n in chi_ids2:
                    if (len(chi_ids2) > 1) and (n in pri_ids1):
                        s = 1.
                    elif (len(chi_ids2 == 1)):
                        s = 1.
            self.robots[bot]['arc_strengths'][edge_ind] = self.robots[bot][
                'arc_strengths'][edge_ind] + s * (1 - ent)
        next_walk = [node, next_node]
        next_departs = [t]
        self.graph.nodes[next_node]['future_visits'][bot] = self.graph[node][
            next_node]['duration']
        # print (next_walk, next_departs)
        return NextTaskBotResponse(next_departs, next_walk)