Beispiel #1
0
    def randomized_select(self, select_range, priority_fn, lowestPriority=False):
        solution = list()
        total_profit = 0
        bag = Bag(self.P, self.M, self.constraints)
        options = list()
        for item in self.items:
            options.append((item, priority_fn(item)))
        options.sort(key=lambda x: -x[1])
        if lowestPriority:
            options = list(reversed(options))
        options = [x[0] for x in options]

        while len(options) > 0 and not bag.full():
            items = options[:min(select_range, len(options))]
            item = items[int(random() * len(items))]
            options.remove(item)
            print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
            if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
                bag.take(item)
                solution.append(item)
                total_profit += item.profit
                new_options = list()
                for x in options:
                    if bag.can_take(x.classNumber):
                        new_options.append(x)
                options = new_options
        return (total_profit, solution)
Beispiel #2
0
    def randomized_select(self, select_range, priority_fn, lowestPriority=False):
        solution = list()
        total_profit = 0
        bag = Bag(self.P, self.M, self.constraints)
        options = list()
        for item in self.items:
            options.append((item, priority_fn(item)))
        options.sort(key=lambda x: -x[1])
        if lowestPriority:
            options = list(reversed(options))
        options = [x[0] for x in options]

        while len(options) > 0 and not bag.full():
            items = options[:min(select_range, len(options))]
            item = items[int(random() * len(items))]
            options.remove(item)
            print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
            if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
                bag.take(item)
                solution.append(item)
                total_profit += item.profit
                new_options = list()
                for x in options:
                    if bag.can_take(x.classNumber):
                        new_options.append(x)
                options = new_options
        return (total_profit, solution)
Beispiel #3
0
 def greedy_on_fn(self, priority_fn, lowestPriority=False):
     solution = list()
     total_profit = 0
     bag = Bag(self.P, self.M, self.constraints)
     queue = get_priority_queue(self.items, priority_fn, lowestPriority)
     
     while not queue.isEmpty() and not bag.full():
         item = queue.pop()
         #print(" remaining items: {0:.2f}%".format(queue.count / self.N * 100), end="\r")
         print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
         if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
             if bag.can_take(item.classNumber):
                 solution.append(item)
                 bag.take(item)
                 total_profit += item.profit
     return (total_profit, solution)
Beispiel #4
0
 def greedy_on_fn(self, priority_fn, lowestPriority=False):
     solution = list()
     total_profit = 0
     bag = Bag(self.P, self.M, self.constraints)
     queue = get_priority_queue(self.items, priority_fn, lowestPriority)
     
     while not queue.isEmpty() and not bag.full():
         item = queue.pop()
         #print(" remaining items: {0:.2f}%".format(queue.count / self.N * 100), end="\r")
         print(" progress: {0:.2f} %".format(max(bag._weight / bag.P, bag._cost/bag.M) * 100), end="\r")
         if bag.has_enough_weight_for(item.weight) and bag.has_enough_money_for(item.cost):
             if bag.can_take(item.classNumber):
                 solution.append(item)
                 bag.take(item)
                 total_profit += item.profit
     return (total_profit, solution)