Repository navigation
Expand file tree
/
Copy pathsimplex.c
More file actions
55 lines (50 loc) · 1.6 KB
/
Copy pathsimplex.c
File metadata and controls
55 lines (50 loc) · 1.6 KB
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
#include "Simplex_aux.h"
int main(){
int status;
MatrixSystem system;
Base *base;
double *costs, *basicSolution, *solution;
getSimplexSystem(&system, &costs);
basicSolution = (double*)malloc(system.size * sizeof(double));
solution = (double*)malloc(system.columns * sizeof(double));
if(solution == NULL || basicSolution == NULL){
printf("Erro de alocação de memória.\n");
exit -1;
}
base = getBase(system, basicSolution);
if(base == NULL)
printf("Problema infactível.\n");
else{
status = simplexMethod(base, basicSolution, costs);
switch (status){
case 1:
for (int i = 0; i < system.columns; i++){
if(i < system.size)
solution[base->basicPartition[i]] = basicSolution[i];
else
solution[base->nonBasicPartition[i - system.size]] = 0;
}
printf("Solução do problema: ");
for (int i = 0; i < system.columns; i++)
printf("%lf ", solution[i]);
printf("\nValor ótimo da solução: f(x) = %lf\n", dotProduct(costs, solution, system.columns));
break;
case 0:
printf("O problema possui solução ilimitada.\n");
break;
}
freeBase(base);
base = NULL;
}
system.size = system.columns;
freeMatrixSystem(system);
free(costs);
costs == NULL;
free(solution);
solution = NULL;
free(basicSolution);
basicSolution = NULL;
free(solution);
solution = NULL;
return 0;
}