-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineMenu.cpp
More file actions
136 lines (105 loc) · 4.38 KB
/
Copy pathCommandLineMenu.cpp
File metadata and controls
136 lines (105 loc) · 4.38 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
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "TruckandPallet.h"
#include "DataLoader.h"
#include "ExhaustiveBF.h"
#include "knapsackDP.h"
#include "GreedyByRatio.h"
/**
* @brief Função que implementa o menu interativo para o programa.
*
* Permite ao usuário escolher o algoritmo para executar e o dataset para usar.
* Imprime resultados detalhados e, no caso do ILP, chama o script Python.
*/
void command_line(){
TruckandPallet truck;
string datasetChoice;
int choice=0;
while (choice != 5){
cout << "What planning do you want to run the program?" << endl;
cout << "1. Exhaustive (Brute-Force) Approach" << endl;
cout << "2. Dynamic Programming Approach " << endl;
cout << "3. Approximation Algorithms (Greedy Approach)" << endl;
cout << "4. Integer Linear Programming Algorithm (ILP)" << endl;
cout << "5. Exit" << endl;
cin >> choice;
if (choice == 1){
cout << " Select the dataset (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
cin>> datasetChoice;
loadData(datasetChoice,truck);
std::vector<Pallet> bestSolution;
int maxProfit = ExhaustiveBF(truck.pallets, truck.truckCapacity, bestSolution);
cout << "Max profit: " << maxProfit << endl;
cout << "Selected pallets:"<< endl;
int totalWeight = 0;
for (const auto& p : bestSolution) {
cout << " - Pallet " << p.id << " (Weight: " << p.weight << ", Profit: " << p.profit << ")"<< endl;
totalWeight += p.weight;
}
cout << "Total weight: " << totalWeight << endl;
cout << endl;
}
else if (choice == 2) {
cout << " Select the dataset (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
cin>> datasetChoice;
loadData(datasetChoice,truck);
bool usedItems[truck.pallets.size()] = {false};
unsigned int maxProfit = knapsackDP(truck.pallets, truck.truckCapacity, usedItems);
cout << "Max profit (Dynamic Programming): " << maxProfit << endl;
cout << "Selected pallets:" << endl;
int totalWeight = 0;
// Exibe as paletes selecionadas
for (size_t i = 0; i < truck.pallets.size(); ++i) {
if (usedItems[i]) {
cout << " - Pallet " << truck.pallets[i].id << " (Weight: " << truck.pallets[i].weight
<< ", Profit: " << truck.pallets[i].profit << ")" << endl;
totalWeight += truck.pallets[i].weight;
}
}
cout << "Total weight: " << totalWeight << endl;
cout << endl;
}
else if (choice == 3) {
cout << " Select the dataset (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
cin>> datasetChoice;
loadData(datasetChoice,truck);
bool usedItems[truck.pallets.size()] = {false};
unsigned int maxProfit= GreedyByRatio(truck.pallets, truck.truckCapacity, usedItems);
int totalWeight = 0;
cout << "Max profit (Approximation Algorithm): " << maxProfit << endl;
cout << "Selected pallets:"<< endl;
for (size_t i = 0; i < truck.pallets.size(); ++i) {
if (usedItems[i]) {
cout << " - Pallet " << truck.pallets[i].id << " (Weight: " << truck.pallets[i].weight
<< ", Profit: " << truck.pallets[i].profit << ")" << endl;
totalWeight += truck.pallets[i].weight;
}
}
cout << "Total weight: " << totalWeight << endl;
cout << endl;
}
else if (choice == 4) {
cout << " Select the dataset (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)";
cin>> datasetChoice;
loadData(datasetChoice,truck);
//ofstream out("C:\\Users\\anita\\DA\\Projeto2\\Truck_Pallet\\input.txt");
ofstream out("../input.txt");
out << truck.numPallets << endl;
out << truck.truckCapacity << endl;
for (auto & pallet : truck.pallets) {
out << pallet.weight << " ";
}
out << endl;
for (auto & pallet : truck.pallets) {
out << pallet.profit << " ";
}
out << endl;
int ret = system("..\\.venv\\Scripts\\python.exe ../KnapsackSolver.py ../input.txt");
if (ret != 0) {
std::cerr << "Failed to run solver" << std::endl;
}
}
}
}