Skip to content

Commit

Permalink
readme updated and some messed up prints removed
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascamata committed Aug 16, 2015
1 parent 322c650 commit ad5c940
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
16 changes: 9 additions & 7 deletions README.md
@@ -1,7 +1,7 @@
Multidimensional Knapsack Problem
=================================

A repository made to host something like a tiny framework to apply heuristics and metaheuristics to the
A repository made to host something like a tiny framework to apply heuristics and metaheuristics to the
multidimensional knapsack problem for a college exercise.

To Run
Expand All @@ -13,7 +13,7 @@ Simply call `python main.py`. You may edit it to use your own created functions
Instructions
------------

The **Knapsack** object receives in its constructor the two constraints for the problem (yes, it's still
The **Knapsack** object receives in its constructor the two constraints for the problem (yes, it's still
limited to two dimensions, but I'm working on this) and a array of **Items** that may be added to the
knapsack. Each **Item** have a **value** and how much of each constraint it consumes. Each additional
keyword argument will be converted into an attribute. This will provide some object injection
Expand All @@ -22,22 +22,24 @@ to implement some cool stuff.
To solve the problem, you use the method *optimize* and pass, in this order: the **initial solution
function** (that will receive the knapsack as its only parameter), **the local search function** (that
will receive the neighborhood function and the knapsack as parameters) and, the last but not least
important, the **neighborhood function**.
important, the **neighborhood function**.

**Note that it's the local search function responsibility to update
the execute movements in the knapsack's actual solution until some criteria is achieved.**
**Note that it's the local search function responsibility to execute movements
in the knapsack's actual solution until some criteria is achieved.**

So, to clarify: `Knapsack.optimize(initial_solution_function, local_search_function, neighborhood_function)`

You may use the **Movement** class to create movements for the problem. It receives two arrays as arguments.
The first represents the Items that will be added to the solution and the second (guess what?) the **Items**
that will be removed from the **Knapsack**. The movement avaliation **(how it will affect the solution total value)**
is accessible through the `Movement.movement_avaliation` property. You can execute Movements by using the
is accessible through the `Movement.movement_avaliation` property. You can execute Movements by using the
`Knapsack.execute_movement(movement)` method.

**For more useful methods on the Knapsack, like check if an Item can be added or removed from the Knapsack,
or if two Items, one out of the Knapsack and on inside it, can be swapped, check `knapsack/knapsack.py`.**

**You can find examples of initial solution functions, local search functions and neighborhood_functions
inside the knapsack folder in the respective files. Note that there's a file with a Tabu Search metaheuristic
implementation, which is the focus of this exercise.**
implementation, which is the focus of this exercise and the only working heuristic. The local search file
includes two functions that are obsolete and won't work because they don't execute movements in the knapsack.
This happened because I needed to change a lot of code for the Tabu Search implementation.**
5 changes: 1 addition & 4 deletions knapsack/tabu.py
Expand Up @@ -69,17 +69,14 @@ def __call__(self, neighborhood_function, knapsack):
# print "Current iter %d, actual solution %d, better solution found in %d with %d" % (self.iter_counter, actual_solution, self.iter_better, best_solution)

print "Better solution found in %d with %d" % (self.iter_better, best_solution)
print knapsack.value
print best_solution
knapsack.value = best_solution
knapsack.items = best_solution_items
knapsack.moves_made = best_solution_moves

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)
if not tabu_ended:
print 'Ended by iteration limit.'
return False

def sort_moves(self, moves):
return sorted(moves, key=lambda x: x.movement_avaliation, reverse=True)

0 comments on commit ad5c940

Please sign in to comment.