-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_knapsack.c
226 lines (181 loc) · 5.54 KB
/
double_knapsack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
typedef struct {
int n;
int m;
int z;
int *value;
} Cube;
Cube *init(int n, int m, int z);
void set(Cube *c, int value, int i, int j, int k);
int get(Cube *c, int i, int j, int k);
void print_3D(Cube *c);
Cube *max_value_two_sacks(int *values, int *weights, int n, int capacity1, int capacity2, int *result);
void find_items_in_sacks(Cube *dp, int *weights, int *values, int n, int capacity1, int capacity2, int *sack1, int *sack2, int *sack1_count, int *sack2_count);
void random_objects(int n, int a, int b, int weights[n], int values[n]);
int
main(int argc, char **argv)
{
--argc, ++argv;
if(argc != 5)
{
fprintf(stderr, "./double.out n_objects size1 size2 min max\n");
exit(1);
}
int n = atoi(*argv);
int s1 = atoi(*(++argv));
int s2 = atoi(*(++argv));
int a = atoi(*(++argv));
int b = atoi(*(++argv));
srand(time(NULL));
int *values = (int*) calloc(n, sizeof(int));
int *weights = (int*) calloc(n, sizeof(int));;
random_objects(n, a, b, values, weights);
int capacity1 = s1;
int capacity2 = s2;
int result = 0;
Cube *c = max_value_two_sacks(values, weights, n, capacity1, capacity2, &result);
//print_3D(c);
printf("n = %d\n", n);
printf("values: ");
for(int i = 0; i < n; i++)
fprintf(stdout, "%d, ", values[i]);
putchar('\n');
putchar('\n');
printf("weights: ");
for(int i = 0; i < n; i++)
fprintf(stdout, "%d, ", weights[i]);
putchar('\n');
putchar('\n');
printf("Le poids total des objets sélectionnés dans les deux sacs est : %d\n", result);
int sack1[n];
int sack2[n];
int sack1_count, sack2_count;
find_items_in_sacks(c, weights, values, n, capacity1, capacity2, sack1, sack2, &sack1_count, &sack2_count);
printf("Objets dans le sac 1 (taille initiale: %3d) : ", s1);
for (int i = 0; i < sack1_count; ++i)
{
printf("%d, ", sack1[i] + 1);
}
printf("\n");
printf("Objets dans le sac 2 (taille initiale: %3d) : ", s2);
for (int i = 0; i < sack2_count; ++i)
{
printf("%d, ", sack2[i] + 1);
}
printf("\n");
free(values);
free(weights);
free(c);
return 0;
}
Cube *init(int n, int m, int z)
{
Cube *tmp = (Cube *) malloc(1 * sizeof(Cube));
if(tmp == NULL)
{
fprintf(stderr, "Erreur: impossible d'allouer la mémoire demandée.\n");
exit(1);
}
tmp->value = (int *) calloc(n * m * z, sizeof(Cube));
if(tmp->value == NULL)
{
fprintf(stderr, "Erreur: impossible d'allouer la mémoire demandée.\n");
exit(1);
}
tmp->n = n;
tmp->m = m;
tmp->z = z;
return tmp;
}
void set(Cube *c, int value, int i, int j, int k)
{
*(c->value + j + i * c->m + k * c->n * c->m) = value;
}
int get(Cube *c, int i, int j, int k)
{
return *(c->value + j + i * c->m + k * c->n * c->m);
}
void print_3D(Cube *c)
{
int n = c->n;
int m = c->m;
int z = c->z;
for(int k = 0; k < z; ++k)
{
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
fprintf(stdout, "%3d ", *(c->value + j + i * m + k * n * m));
}
putchar('\n');
}
putchar('\n');
}
}
Cube *max_value_two_sacks(int *values, int *weights, int n, int capacity1, int capacity2, int *result)
{
Cube *dp = init(n + 1, capacity1 + 1, capacity2 + 1);
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j <= capacity1; ++j)
{
for (int k = 0; k <= capacity2; ++k)
{
if (weights[i - 1] <= j && weights[i - 1] <= k)
{
set(dp, MAX(get(dp, i - 1, j, k),
MAX(values[i - 1] + get(dp, i - 1, j - weights[i - 1], k),
values[i - 1] + get(dp, i - 1, j, k - weights[i - 1]))), i, j, k);
}
else if (weights[i - 1] <= j)
{
set(dp, MAX(get(dp, i - 1, j, k), values[i - 1] + get(dp, i - 1, j - weights[i - 1], k)), i, j, k);
}
else if (weights[i - 1] <= k)
{
set(dp, MAX(get(dp, i - 1, j, k), values[i - 1] + get(dp, i - 1, j, k - weights[i - 1])), i, j, k);
}
else
{
set(dp, get(dp, i - 1, j, k), i, j, k);
}
}
}
}
*result = get(dp, n, capacity1, capacity2);
return dp;
}
void find_items_in_sacks(Cube *dp, int *weights, int *values, int n, int capacity1, int capacity2, int *sack1, int *sack2, int *sack1_count, int *sack2_count)
{
int i = n;
int j = capacity1;
int k = capacity2;
*sack1_count = 0;
*sack2_count = 0;
while (i > 0 && (j > 0 || k > 0))
{
if (weights[i - 1] <= j && get(dp, i - 1, j - weights[i - 1], k) + values[i - 1] == get(dp, i, j, k))
{
sack1[(*sack1_count)++] = i - 1;
j -= weights[i - 1];
}
else if (weights[i - 1] <= k && get(dp, i - 1, j, k - weights[i - 1]) + values[i - 1] == get(dp, i, j, k))
{
sack2[(*sack2_count)++] = i - 1;
k -= weights[i - 1];
}
i--;
}
}
void random_objects(int n, int a, int b, int weights[n], int values[n])
{
for(int i = 0; i < n; ++i)
{
values[i] = (rand() % b) + a;
weights[i] = (rand() % b) + a;
}
}