Giter Site home page Giter Site logo

clojure_knapsack's Introduction

doll-smuggler

Solution for the "Doll Smuggler" knapsack problem at https://github.com/micahalles/doll-smuggler

Notes

I started with brute force because i) I love combinatorial explosion and ii) it'd be useful for developing (small) testcases. With the given testcase, the exhaustive solver runs for about 2.5 minutes before java.lang.OutOfMemoryError on my system.

The first heuristic approach that comes to mind is to sort the items by descending efficiency, and start loading the knapsack from the top of the list. (i.e., favor (value/weight).) I can easily contrive a counterexample, though, so I'll have to do better. (see break_heuristics.input)

To build a correct solution that runs in reasonable time, I turned to Wikipedia. I studied the 0/1 dynamic programming approach (not the pseudocode, but rather the recursive definitions above it) and executed it a couple times on paper to wrap my head around it. Finally, with no small amount of println debugging, I managed to implement it in Clojure.

Usage

Run it like this:

$ lein run testcases/given.input

Or, to compare results for different approaches (brute force, heuristics, recursive):

$ lein run -v testcases/break_heuristics.input
max_weight:    200
total weight:  551
total value:   345

by weight:          $65 (151)
by value:           $100 (200)
by efficiency:      $130 (171)
exhaustively:       $180 (200)
recursive solution: $180 (200)

packed dolls:

name weight value
doc    100    90
seth   100    90

To run all the tests:

$ lein test

lein test doll-smuggler.core-test

Ran 9 tests containing 9 assertions.
0 failures, 0 errors.

clojure_knapsack's People

Contributors

jrr avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.