-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
271 lines (227 loc) · 7.8 KB
/
Copy pathproject.cpp
File metadata and controls
271 lines (227 loc) · 7.8 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
// ---------------- CLEAR SCREEN ----------------
void clearScreen()
{
cout << "\033[2J\033[1;1H";
}
// ---------------- WELCOME PAGE ----------------
void displayWelcomePage()
{
cout << "----------------------------------------------" << endl;
cout << "----------------------------------------------" << endl;
cout << "|| ||" << endl;
cout << "|| ||" << endl;
cout << "|| WELCOME TO DELIVERY LOGISTIC PATH ||" << endl;
cout << "|| FINDER ||" << endl;
cout << "|| ||" << endl;
cout << "----------------------------------------------" << endl;
cout << "----------------------------------------------" << endl;
cout << "Press Enter to proceed to the main menu..." << endl;
cin.ignore();
cin.get();
clearScreen();
}
// ---------------- TASK SCHEDULING ----------------
struct Job
{
int id;
int deadline;
int penalty;
};
void TaskScheduling(int numOrders)
{
cout << "All deadlines should be entered in intervals of 10 minutes." << endl;
vector<Job> jobs(numOrders);
for (int i = 0; i < numOrders; i++)
{
cout << "Enter deadline for order " << i + 1 << ": ";
cin >> jobs[i].deadline;
jobs[i].id = i + 1;
}
for (int i = 0; i < numOrders; i++)
{
cout << "Enter penalty percentage if late (for order " << i + 1 << "): ";
cin >> jobs[i].penalty;
}
sort(jobs.begin(), jobs.end(), [](Job a, Job b)
{ return a.penalty > b.penalty; });
int maxDeadline = 0;
for (auto j : jobs)
maxDeadline = max(maxDeadline, j.deadline / 10);
vector<int> slot(maxDeadline + 1, -1);
int lateTasksCount = 0;
for (auto j : jobs)
{
int d = j.deadline / 10;
bool scheduled = false;
for (int i = d; i > 0; i--)
{
if (slot[i] == -1)
{
slot[i] = j.id;
scheduled = true;
break;
}
}
if (!scheduled)
lateTasksCount++;
}
cout << endl
<< "Orders should be delivered in the following order: " << endl;
for (int i = 1; i <= maxDeadline; i++)
{
if (slot[i] != -1)
cout << "Order " << slot[i] << " ";
}
cout << endl
<< "Number of orders delivered late: " << lateTasksCount << endl
<< endl;
}
// ---------------- KNAPSACK ----------------
void printKnapSack(int maxWeight, int numItems)
{
vector<int> profit(numItems), weight(numItems);
for (int i = 0; i < numItems; i++)
{
cout << "Enter order value for order no " << i + 1 << ": ";
cin >> profit[i];
}
for (int i = 0; i < numItems; i++)
{
cout << "Enter number of items in order " << i + 1 << ": ";
cin >> weight[i];
}
vector<vector<int>> dp(numItems + 1, vector<int>(maxWeight + 1, 0));
for (int i = 1; i <= numItems; i++)
{
for (int w = 0; w <= maxWeight; w++)
{
if (weight[i - 1] <= w)
dp[i][w] = max(profit[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}
cout << endl
<< "To achieve maximum profit, carry the following orders:" << endl;
int w = maxWeight;
for (int i = numItems; i > 0; i--)
{
if (dp[i][w] != dp[i - 1][w])
{
cout << "Order value: " << profit[i - 1] << endl;
w -= weight[i - 1];
}
}
}
// ---------------- TSP ----------------
void tsp(int graph[10][10], bool completed[10], int numHouses, int cost, int pos, int count, int &minCost)
{
if (count == numHouses && graph[pos][0])
{
minCost = min(minCost, cost + graph[pos][0]);
return;
}
for (int i = 0; i < numHouses; i++)
{
if (!completed[i] && graph[pos][i])
{
completed[i] = true;
tsp(graph, completed, numHouses, cost + graph[pos][i], i, count + 1, minCost);
completed[i] = false;
}
}
}
// ---------------- MENU ----------------
int displayMenu()
{
int choice;
cout << endl
<< "---- Welcome User ----" << endl;
cout << "You have 3 choices to deliver the orders. Choose accordingly and enter the details asked:" << endl;
cout << "1. Find the best economic way to deliver all the orders (TSP Algorithm)" << endl;
cout << "2. Check which orders can be delivered for maximum profit with quantity constraint (Knapsack Algorithm)" << endl;
cout << "3. Determine the maximum number of orders to be delivered without incurring penalties (Task Scheduling)" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;
while (choice < 1 || choice > 3)
{
cout << "Invalid choice. Please enter a number between 1 and 3: ";
cin >> choice;
}
return choice;
}
// ---------------- MAIN ----------------
int main()
{
displayWelcomePage();
char runAgain = 'y';
while (runAgain == 'y' || runAgain == 'Y')
{
int userChoice = displayMenu();
if (userChoice == 1)
{
int costMatrix[10][10], numHouses, minCost = INT_MAX;
cout << "Enter the number of houses: ";
cin >> numHouses;
numHouses++;
bool completed[10] = {false};
completed[0] = true;
cout << "Enter the Cost Matrix (all distances in km):" << endl;
for (int i = 0; i < numHouses; i++)
{
for (int j = 0; j < numHouses; j++)
{
if (i != j)
{
if (i == 0)
cout << "Enter distance from delivery point (Dispatch Hub) to house " << j << ": ";
else if (j == 0)
cout << "Enter distance from house " << i << " to Dispatch Hub: ";
else
cout << "Enter distance from house " << i << " to house " << j << ": ";
cin >> costMatrix[i][j];
}
else
{
costMatrix[i][j] = 0;
}
}
}
cout << endl << "The distance matrix is:";
for (int i = 0; i < numHouses; i++)
{
cout << endl;
for (int j = 0; j < numHouses; j++)
cout << "\t" << costMatrix[i][j];
}
cout << endl;
tsp(costMatrix, completed, numHouses, 0, 0, 1, minCost);
cout << "Minimum distance to deliver all orders = " << minCost << " km" << endl;
}
else if (userChoice == 2)
{
int numOrders, maxWeight;
cout << "Enter number of orders: ";
cin >> numOrders;
cout << "Enter the maximum weight capacity: ";
cin >> maxWeight;
printKnapSack(maxWeight, numOrders);
}
else if (userChoice == 3)
{
int numOrders;
cout << "Enter number of orders: ";
cin >> numOrders;
TaskScheduling(numOrders);
}
cout << "Would you like to run the program again? (y/n): ";
cin >> runAgain;
}
cout << "Thank you for using the delivery management system. Goodbye!" << endl;
return 0;
}