Exemplo n.º 1
0
    def get_next_destination_ship(self, all_ports, all_ships):
        # where the ship will go next
        oldest_ware_score = 8  #not lesser then 1
        distance_weight = 2
        wares_list = self.wares
        scoring_table = {}

        # score for oldest ware
        scoring_table[wares_list[0].destination_port] = oldest_ware_score-1

        # score 1 for all wares

        for ware in wares_list:
            if scoring_table.has_key(ware.destination_port):
                scoring_table[ware.destination_port] += 1
            else:
                scoring_table[ware.destination_port] = 1

        # PATCH: if 80% of capacity is reached, only wares onboard are considered
        if self.number_of_wares()*5//4 < WARES_MAX:
            # We need to identify ports where no ship is heading and add score based on waiting wares
            for port in all_ports:
                if not port==self.next_destination and len(port.wares)>0:
                    any_ship_heading_here=False
                    for ship in all_ships:
                        if ship.next_destination==port:
                            any_ship_heading_here=True
                            break

                    if not any_ship_heading_here:
                        if scoring_table.has_key(port):
                            scoring_table[port]+=port.number_of_wares()
                        else:
                            scoring_table[port]=port.number_of_wares()

        # adding score if based on distance for ports we consider to visit
        for port,count in scoring_table.iteritems():
            scoring_table[port]+=(5-find_distance_between_ports(port,self.next_destination)/20)*distance_weight

        # find best option
        best_port = None
        best_score = None
        for port,count in scoring_table.iteritems():
            if best_score is None:
                best_port = port
                best_score = count
            if count>best_score:
                best_port=port
                best_score=count

        return best_port
Exemplo n.º 2
0
 def distance_between_destinations(self):
 	if self.last_destination == "":
 		return 0
 	return find_distance_between_ports(self.last_destination, self.next_destination)
Exemplo n.º 3
0
 def set_ports(self, start, destination):
     self.start_port = start
     self.destination_port = destination
     self.route_min = find_distance_between_ports(start, destination)
     return
    def get_next_destination_ship(self, all_ports, all_ships):
        # where the ship will go next
        # http://bazaar.launchpad.net/~widelands-dev/widelands/ship_scheduling/view/head:/src/economy/fleet.cc#L650
        # first: all ports in list with score=0
        port_list = []
        for port in all_ports:
            port_list.append([port, 0])

        oldest_ware_score = 8  #not lesser then 1
        wares_list = self.wares

        # line 713 - loop (calculating scores for wares onboard)
        # score 1 for all wares, oldest_ware_score for first
        all_destinations_wares = self.get_all_destinations_wares()
        for port in port_list:
            if not (port[0] in all_destinations_wares):
                continue
            for ware in wares_list:
                if port[0] == ware.destination_port:
                    if ware == wares_list[0]:
                        port[1] += 8
                    else:
                        port[1] += 1

        # line 735 - loop (wares waiting in ports)
        for port in port_list:
            if port[0].number_ships_to_call() == 0:
                continue
            if self.number_of_wares() > WARES_MAX//2:
                port[1] += 1
            else:
                port[1] += 3
            port[1] += min(self.space_left(), port[0].number_of_wares())//3

        # line 773 - loop (calculate score for distances)
        for port in port_list:
            route_length = find_distance_between_ports(port[0], self.next_destination)
            score_for_distance = 0
            # line 805 - decision what type of distance it is
            if route_length < 3:
                score_for_distance = 10
            else:
                score_for_distance = 8 - route_length//50
            if score_for_distance < 0:
                score_for_distance = 0
            port[1] += score_for_distance

        # line 817 - loop (find best port)
        best_port = port_list[0][0]
        best_score = port_list[0][1]
        for port in port_list:
            if best_score < port[1]:
                best_port = port[0]
                best_score = port[1]

        # line 872 - updating port needs
        ships = ShipList()
        ships.ships_list = all_ships
        ships_going_to_port = ships.select_ships_going_to_port(best_port)
        ships_going_to_port.append(self)
        best_port.update_calling_ships(ships_going_to_port)

        # end:
        return best_port