PGA/route.py
def try_insert(self, node: int):
        """
        try to insert a node into this route, ensure no extra punish
        :param node: the node to be inserted
        :return: cost change, if cannot insert will return huge
        """
        old_cost = self.cost
        old_punish = self.get_total_punish()
        try_route_list = []
        nearby_station = self.g_map.get_nearby_station(node)
        for insert_pos in range(0, len(self.sequence) + 1):
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_sequence.insert(insert_pos, nearby_station)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_sequence.insert(insert_pos + 1, node)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
        best_insert = min(try_route_list, key=lambda x: x.cost)
        del try_route_list
        if best_insert.get_total_punish() <= old_punish:
            self.sequence = best_insert.sequence.copy()
            self.refresh_state()
            return self.cost - old_cost
        else:
            return huge

        try to insert a node into this route, ensure no extra punish
        :param node: the node to be inserted
        :return: cost change, if cannot insert will return huge
        
try to insert a node into this route, ensure no extra punish
try to insert a node into this route ensure no extra punish
def try_insert(self, node: int):
        old_cost = self.cost
        old_punish = self.get_total_punish()
        try_route_list = []
        nearby_station = self.g_map.get_nearby_station(node)
        for insert_pos in range(0, len(self.sequence) + 1):
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_sequence.insert(insert_pos, nearby_station)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
            try_sequence = self.sequence.copy()
            try_sequence.insert(insert_pos, node)
            try_sequence.insert(insert_pos + 1, node)
            try_route_list.append(Route(g_map=self.g_map, sequence=try_sequence, punish=self.punish))
        best_insert = min(try_route_list, key=lambda x: x.cost)
        del try_route_list
        if best_insert.get_total_punish() <= old_punish:
            self.sequence = best_insert.sequence.copy()
            self.refresh_state()
            return self.cost - old_cost
        else:
            return huge
def try_insert ( self , node : int ) : old_cost = self . cost old_punish = self . get_total_punish ( ) try_route_list = [ ] nearby_station = self . g_map . get_nearby_station ( node ) for insert_pos in range ( 0 , len ( self . sequence ) + 1 ) : try_sequence = self . sequence . copy ( ) try_sequence . insert ( insert_pos , node ) try_route_list . append ( Route ( g_map = self . g_map , sequence = try_sequence , punish = self . punish ) ) try_sequence = self . sequence . copy ( ) try_sequence . insert ( insert_pos , node ) try_sequence . insert ( insert_pos , nearby_station ) try_route_list . append ( Route ( g_map = self . g_map , sequence = try_sequence , punish = self . punish ) ) try_sequence = self . sequence . copy ( ) try_sequence . insert ( insert_pos , node ) try_sequence . insert ( insert_pos + 1 , node ) try_route_list . append ( Route ( g_map = self . g_map , sequence = try_sequence , punish = self . punish ) ) best_insert = min ( try_route_list , key = lambda x : x . cost ) del try_route_list if best_insert . get_total_punish ( ) <= old_punish : self . sequence = best_insert . sequence . copy ( ) self . refresh_state ( ) return self . cost - old_cost else : return huge
try to insert a node into this route, ensure no extra punish
try to insert a node into this route ensure no extra punish
"""
        try to insert a node into this route, ensure no extra punish
        :param node: the node to be inserted
        :return: cost change, if cannot insert will return huge
        """
cost change, if cannot insert will return huge
cost change if cannot insert will return huge
self
node
int
the node to be inserted
the node to be inserted