Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab code for burglar optimization problem

Subject: Matlab code for burglar optimization problem

From: Namo

Date: 5 Dec, 2010 15:36:04

Message: 1 of 3

Matlab code for burglar optimization problem

A burglar with a bag break into a house and find some items that have different weights and values but there is a maximum wight that he can curry. what items the burglar should take to maximize the value.

the program should have three inputs and at least two outputs.

inputs:
w: the weight vector of the item
v: the value of the item {ie v(n) is the value of item that its wight w(n)}
Wmax: the maximum total weight the burglar can carry.

output:
Vmax: The total optimize value of the items
x: a vector with the same length of w that has zero to represent the item that is not taken and 1 to represent taken items.

Note: I have related python code but I don't understand python but this could help:
def maxVal0(w, v, i, aW):
m = {}
return fastMaxVal(w, v, i, aW, m)

def fastMaxVal(w, v, i, aW, m)
global numCalls
numCalls += 1
try: return m[(i, aW)]
except KeyError:
if i == 0:
if w[i] <= aW:
m[(i, aW)] = v[i]
return v[i]
else:
m[(i, aW)] = 0
return 0
without_i = fastMaxVal(w, v, i-1, aW, m)
if w[i] > aW:
m[(i, aW)] = without_i
return without_i
else: with_i = v[i] + fastMaxVal(w, v, i-1, aW - w[i], m)
res = max(with_i, without_i)
m[(i, aW)] = res
return res

Subject: Matlab code for burglar optimization problem

From: ImageAnalyst

Date: 5 Dec, 2010 15:55:06

Message: 2 of 3

This is just another name for the knapsack problem:
http://en.wikipedia.org/wiki/Knapsack_problem

Now I don't know how to program in Python either but even I could take
that code and translate it into MATLAB code - it doesn't look like it
needs very many changes at all. Looks remarkably similar. Looks like
"bag" is "knapsack," "def" is "function" and "except" is "catch" but
pretty much everything else is the same. I know you can do it.

Reading back over your posting, it looks like you didn't actually ask
a question. (The only question in there was the question in the
problem statement itself.) So, do you have a MATLAB question?

Subject: Matlab code for burglar optimization problem

From: Namo

Date: 5 Dec, 2010 21:17:07

Message: 3 of 3

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <3f4aee62-3775-4a33-a7c5-d1edf157ed33@l32g2000yqc.googlegroups.com>...
> This is just another name for the knapsack problem:
> http://en.wikipedia.org/wiki/Knapsack_problem
>
> Now I don't know how to program in Python either but even I could take
> that code and translate it into MATLAB code - it doesn't look like it
> needs very many changes at all. Looks remarkably similar. Looks like
> "bag" is "knapsack," "def" is "function" and "except" is "catch" but
> pretty much everything else is the same. I know you can do it.
>
> Reading back over your posting, it looks like you didn't actually ask
> a question. (The only question in there was the question in the
> problem statement itself.) So, do you have a MATLAB question?

After I learn that is called knapsack and search for it , I found a code in here:

http://www.mathworks.com/matlabcentral/fileexchange/22783-0-1-knapsack

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us