-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmochilaProblema.c
169 lines (157 loc) · 4.34 KB
/
mochilaProblema.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
// A Binary KnapSack(0-1) problem brute force solver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct objects{
double w; // peso objeto
double v; // valor objeto
};
typedef struct data{
int n; // quantidade itens
double w; // peso maximo mochila
struct objects *itens; // itens dos objetos lidos
int* vsolution; //solucao a ser verificada
}data;
FILE* promptArchName(FILE* *arch){
char archName[30]="mx.txt";
printf("Digite o nome do arquivo: ");
//scanf("%s",&archName[0]);
printf("nome: %s\n",archName); // comentar dps
*arch = fopen(archName, "r");
return *arch;
}
int archToStruct(FILE* *archf, data* archd){
fscanf(*(archf),"%lf",&archd->w);
fscanf(*(archf),"%d",&archd->n);
printf("archd.w: %lf\narchd.n: %d\n",archd->w,archd->n); //comentar dps
archd->itens = malloc(sizeof(struct objects)*archd->n);
archd->vsolution = (int*)malloc(sizeof(int)*archd->n);
for(int i = 0; i < archd->n; i++){
fscanf(*(archf),"%lf %lf",&archd->itens[i].w,&archd->itens[i].v);
printf("o[%d]: %lf %lf\n",i,archd->itens[i].w,archd->itens[i].v); // comentar dps
}
for(int i = 0; i < archd->n; i++){
fscanf(*(archf),"%d",&archd->vsolution[i]);
printf("vs[%d]: %d\n",i,archd->vsolution[i]); // comentar dps
}
return 1;
}
int all1(int *num, int num_digits){
for(int i=0;i<num_digits+1;i++){
if(num[i]!=1){
return 0;
}
}
return 1;
}
int plus1InBinary(int *num,int p){
if(all1(num, p)){
return 1;
}
if(num[p]==0){
num[p]=1;
}else if(num[p]==1){
num[p]=0;
plus1InBinary(num,p-1);
}
return 0;
}
double bestValuePossible(data *archd){
double vbest=0, vaux=0, waux=0;
int *combinations=(int*)malloc(sizeof(int)*archd->n); // um array representando em binario as combinacoes
// dos itens possiveis
long long unsigned int possibleComb = pow(2,archd->n);
for(int i=0; i<(archd->n); i++){
combinations[i]=0;
}
printf("Calculando melhor valor\npossibleComb: %lu\n",possibleComb);
for(long long int i=0; i<possibleComb; i++, plus1InBinary(combinations,archd->n - 1)){
for(int ii=0; ii<(archd->n); ii++){
printf("%d ",combinations[ii]);
}
printf("|");
vaux=0, waux=0;
for(int j=0; j<archd->n; j++){
if(combinations[j]==1){
vaux+=archd->itens[j].v;
waux+=archd->itens[j].w;
}
}
printf("vaux: %lf, waux: %lf\n",vaux,waux);
if(vaux >= vbest && waux <=archd->w){
printf("Nova melhor Config encontrada: %lf, anterior: %lf, peso: %lf\n",vaux,vbest,waux); // comentar dps
printf("Objetos: \n");
for(int k=0;k<archd->n;k++){
if(combinations[k]==1){
printf(" ik: %d iw: %lf iv: %lf\n",
k,
archd->itens[k].w,
archd->itens[k].v
);
}
}
vbest = vaux;
}
}
return vbest;
}
int checkSolutionViability(data *archd){
double wsum=0, vsum=0, vbest=bestValuePossible(archd);
if(vbest<0){
printf("vBest calculado deu negativo\n"); //comentar dps
return 4;
}
for(int i=0;i<archd->n;i++){
if(archd->vsolution[i]==1){
wsum+=archd->itens[i].w;
vsum+=archd->itens[i].v;
}
}
if(wsum>archd->w){
return 3; // solucao inviavel
}
if(vsum==vbest && wsum<=(archd->w)){
return 0; // solucao otima
}
if(vsum<vbest && wsum<=(archd->w)){
return 1; // solucao viavel mas nao otima
}
if(vsum>vbest){
printf("nosso melhor vbest esta errado\n"); // comentar dps
return 4; // nosso melhor valor esta errado
}
return 4; //
}
int main () {
FILE* archf; //archive file
data archd; // archive data
char buffer[500];
if(promptArchName(&archf)==NULL){ // le o arquivo do diretorio
printf("Sem arquivo\n"); //comentar dps
return 0;
}else{
rewind(archf);
printf("Lido\n");// comentar dps
}
if(archToStruct(&archf,&archd)){ // passa o arquivo lido para uma estrutura do programa
printf("archToStruct OK\n"); // comentar dps
}else{
printf("archToStruct FAILED\n"); // comentar dps
}
switch(checkSolutionViability(&archd)){
case 0:
printf("Solucao otima.");
break;
case 1:
printf("Solucao viavel mas nao otima.");
break;
case 3:
printf("Solucao inviavel.");
break;
case 4:
printf("Erro, indecindivel.");
}
fclose(archf);
return 0;
}