Discrete Optimization - Knapsack
The course submits assignments using python, so there are solver.py and submit.py scripts given to students. There's also a solverJava.py for students who want to program in java and have python grab their results.
Since many of the jobs I've looked at want Python, I decided to use Python instead of tackling these assignments in R.
I took the whole 13-hour sequence for Python on Codecademy, but that was last summer, about six months ago. Apparently, I didn't remember as much as I thought I did.
Python 3.4 is already installed on my computer, but the course files use 2.7.x, so I installed that; Google helped me figure out how to run scripts from the command line, and the course's forums pointed to the IDE Anaconda, which is SO much easier.
Parts of the next ten hours would have made a great 80's musical montage, as I googled "python dictionaries" and a hundred other absolute basics like syntax of for loops and then tried out variations to see what I could do and what would throw me errors.
Either I remembered more than I thought or I'm just amazing with picking up new things (probably a mix of both) because by 10PM I had finished a simple greedy algorithm and implemented a relaxation/branch/bound depth-first tree search.
I got stuck for almost two hours trying to track down a "bug" that turned out to be a fundamental part of how Python works. Lists, you see, are mutable. So when I say:
The output shows that y is also the list [1,2,3]. That's because when I created y, it only created a memory reference to where x is also pointing. I was passing item_values around to at least three functions, which is why it took me so long to find out exactly what was going on.
Now the output for y is [1,2].
It's always such an interesting puzzle when you can do something simple by hand, like list every permutation of 1's and 0's for n spaces, but you have to figure out how to tell the computer how to do what you can easily see.
The first graded set has 30 items. There are 2^30 (about a billion) ways to permute those items (for inclusion or not).
My program hadn't finished running in two hours, so I went to bed. This morning, it had spit out the optimal solution [sunglasses emoji], but due to particulars with the item set, this method is simply not very good. Even pruning 70% of the possibilities meant it had to explore close to 300 million trees.
Now, I'm off to implement dynamic programming. It should go quicker today, since I won't have to look up how to write an IF statement.