2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
commit dcf5d15da3
7965 changed files with 139916 additions and 31064 deletions

View file

@ -1,91 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
const char * name;
int weight, value;
char *name;
int weight;
int value;
} item_t;
item_t item[] = {
{"map", 9, 150},
{"compass", 13, 35},
{"water", 153, 200},
{"sandwich", 50, 160},
{"glucose", 15, 60},
{"tin", 68, 45},
{"banana", 27, 60},
{"apple", 39, 40},
{"cheese", 23, 30},
{"beer", 52, 10},
{"suntancream", 11, 70},
{"camera", 32, 30},
{"T-shirt", 24, 15},
{"trousers", 48, 10},
{"umbrella", 73, 40},
{"waterproof trousers", 42, 70},
{"waterproof overclothes", 43, 75},
{"note-case", 22, 80},
{"sunglasses", 7, 20},
{"towel", 18, 12},
{"socks", 4, 50},
{"book", 30, 10}
item_t items[] = {
{"map", 9, 150},
{"compass", 13, 35},
{"water", 153, 200},
{"sandwich", 50, 160},
{"glucose", 15, 60},
{"tin", 68, 45},
{"banana", 27, 60},
{"apple", 39, 40},
{"cheese", 23, 30},
{"beer", 52, 10},
{"suntan cream", 11, 70},
{"camera", 32, 30},
{"T-shirt", 24, 15},
{"trousers", 48, 10},
{"umbrella", 73, 40},
{"waterproof trousers", 42, 70},
{"waterproof overclothes", 43, 75},
{"note-case", 22, 80},
{"sunglasses", 7, 20},
{"towel", 18, 12},
{"socks", 4, 50},
{"book", 30, 10},
};
#define n_items (sizeof(item)/sizeof(item_t))
typedef struct {
uint32_t bits; /* 32 bits, can solve up to 32 items */
int value;
} solution;
void optimal(int weight, int idx, solution *s)
{
function solve(itemArray, capacity){
matrix = create2DMatrix(itemArray.length, capacity);
keep_matrix = create2DMatrix(itemArray.length, capacity);
for(var c=0; c <= capacity; c++){
matrix[0][c] = 0;
keep_matrix[0][c] = 0;
}
for(var r=1; r<itemArray.length+1; ++r){//rows
for(var c=0; c<=capacity; ++c){
var toMatrix = 0;
//fit in itself?
var fit = items[r-1].weight<= c;
if(fit){//add remaining mini-knapsack if any, and compare to not putting it
var restCap = c-items[r-1].weight;
toMatrix = items[r-1].value+ matrix[r-1][restCap];
if( toMatrix > matrix[r-1][c])
keep_matrix[r][c] = 1;
else
keep_matrix[r][c] = 0;
toMatrix = Math.max(toMatrix, matrix[r-1][c]);
}else{//copy the knapsack from row above
toMatrix = matrix[r-1][c];
keep_matrix[r][c] = 0;
}
matrix[r][c] = toMatrix;
}
}
return matrix;
}
}
int main(void)
{
int i = 0, w = 0;
solution s = {0, 0};
optimal(400, n_items - 1, &s);
for (i = 0; i < n_items; i++) {
if (s.bits & (1 << i)) {
printf("%s\n", item[i].name);
w += item[i].weight;
}
int *knapsack (item_t *items, int n, int w) {
int i, j, a, b, *mm, **m, *s;
mm = calloc((n + 1) * (w + 1), sizeof (int));
m = malloc((n + 1) * sizeof (int *));
m[0] = mm;
for (i = 1; i <= n; i++) {
m[i] = &mm[i * (w + 1)];
for (j = 0; j <= w; j++) {
if (items[i - 1].weight > j) {
m[i][j] = m[i - 1][j];
}
else {
a = m[i - 1][j];
b = m[i - 1][j - items[i - 1].weight] + items[i - 1].value;
m[i][j] = a > b ? a : b;
}
}
printf("Total value: %d; weight: %d\n", s.value, w);
return 0;
}
s = calloc(n, sizeof (int));
for (i = n, j = w; i > 0; i--) {
if (m[i][j] > m[i - 1][j]) {
s[i - 1] = 1;
j -= items[i - 1].weight;
}
}
free(mm);
free(m);
return s;
}
int main () {
int i, n, tw = 0, tv = 0, *s;
n = sizeof (items) / sizeof (item_t);
s = knapsack(items, n, 400);
for (i = 0; i < n; i++) {
if (s[i]) {
printf("%-22s %5d %5d\n", items[i].name, items[i].weight, items[i].value);
tw += items[i].weight;
tv += items[i].value;
}
}
printf("%-22s %5d %5d\n", "totals:", tw, tv);
return 0;
}