Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

old and bugged code removed

  • Loading branch information...
commit 322c650cd6d08e5a7ebf2710081db4d508594c59 1 parent 3f029db
Douglas Camata authored
21  knapsack/knapsack.py
@@ -29,31 +29,12 @@ def __init__(self, weight, volume, all_items, **kwargs):
29 29
         self.value = 0
30 30
         self.all_items = all_items
31 31
         self.initial_value = 0
32  
-        self._items = []
  32
+        self.items = []
33 33
         self.movement_counter = 0
34 34
         self.moves_made = []
35 35
         for key in kwargs.keys():
36 36
             setattr(self, key, kwargs[key])
37 37
 
38  
-    def set_items(self, items):
39  
-        if len(items) == 0:
40  
-            self._items = []
41  
-        total_weight = reduce(lambda x, y: x + y, map(lambda item: item.weight, items))
42  
-        total_volume = reduce(lambda x, y: x + y, map(lambda item: item.volume, items))
43  
-        total_value = reduce(lambda x, y: x + y, map(lambda item: item.value, items))
44  
-        if total_weight < self.initial_weight and total_volume < self.initial_volume:
45  
-            self._items = items
46  
-            self.weight = self.initial_weight - total_weight
47  
-            self.volume = self.initial_volume - total_volume
48  
-            self.value = total_value
49  
-        else:
50  
-            return False
51  
-    def get_items(self):
52  
-        return self._items
53  
-    def del_items(self):
54  
-        del self._items
55  
-    items = property(get_items, set_items, del_items, 'Items')
56  
-
57 38
     def optimize(self, initial_solution_function, heuristic_function, neighborhood_function):
58 39
         start = clock()
59 40
         initial_solution_function(self)
5  knapsack/neighborhood.py
@@ -44,12 +44,13 @@ def all_neighborhood(knapsack):
44 44
         volume_to_lose = item.volume
45 45
         weight_to_lose = item.weight
46 46
         while volume_to_lose > 0 and weight_to_lose > 0:
47  
-            solution_item = choice(knapsack.all_items)
  47
+            solution_item = choice(knapsack.items)
48 48
             to_remove.append(solution_item)
49 49
             weight_to_lose -= solution_item.weight
50 50
             volume_to_lose -= solution_item.volume
51 51
             i += 1
52  
-        movement = Movement(add_items=[item,], remove_items=to_remove)
  52
+            movement = Movement(add_items=[item,], remove_items=to_remove)
  53
+            neighborhood.append(movement)
53 54
 
54 55
         for solution_item in knapsack.items:
55 56
             if knapsack.can_add_item(item):
14  knapsack/tabu.py
@@ -51,8 +51,8 @@ def __call__(self, neighborhood_function, knapsack):
51 51
                     self.iter_better = self.iter_counter
52 52
             else: # se eliminou todos vizinhos
53 53
                 # encontra o tabu com melhor melhora
54  
-                if len(knapsack.tabu_list) == 0:
55  
-                    return False
  54
+                # if len(knapsack.tabu_list) == 0:
  55
+                    # return False
56 56
                 best_tabu = reduce(lambda x, y: x if x.movement_avaliation > y.movement_avaliation else y, knapsack.tabu_list)
57 57
                 if best_tabu.movement_avaliation > 0: # se ele apresentar uma melhora real na solucao atual
58 58
                     actual_solution = knapsack.value + best_tabu.movement_avaliation
@@ -63,19 +63,17 @@ def __call__(self, neighborhood_function, knapsack):
63 63
                         best_solution_items = deepcopy(knapsack.items)
64 64
                         self.iter_better = self.iter_counter
65 65
                     knapsack.execute_movement(best_tabu)
66  
-                else:
67  
-                    print knapsack.tabu_list
68  
-                    print 'Ended by tabu list.'
69  
-                    tabu_ended = True
70  
-                    break
71 66
             solutions = neighborhood_function(knapsack)
72 67
             sorted_moves = self.sort_moves(solutions)
73 68
             [sorted_moves.remove(tabu.reverse()) for tabu in knapsack.tabu_list if tabu.reverse() in sorted_moves]
74 69
             # print "Current iter %d, actual solution %d, better solution found in %d with %d" % (self.iter_counter, actual_solution, self.iter_better, best_solution)
  70
+
  71
+        print "Better solution found in %d with %d" % (self.iter_better, best_solution)
  72
+        print knapsack.value
  73
+        print best_solution
75 74
         knapsack.value = best_solution
76 75
         knapsack.items = best_solution_items
77 76
         knapsack.moves_made = best_solution_moves
78  
-        print "Better solution found in %d with %d" % (self.iter_better, best_solution)
79 77
         
80 78
         print 'Script ran with tabu search using a max of %d iterations and a tabu list with size %d.' % (self.max_iter, knapsack.tabu_list.size)
81 79
         if not tabu_ended:

0 notes on commit 322c650

Please sign in to comment.
Something went wrong with that request. Please try again.