-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcores.java
More file actions
293 lines (203 loc) · 9.16 KB
/
cores.java
File metadata and controls
293 lines (203 loc) · 9.16 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
*---------------------------------INFO-------------------------------------------*
| [*]Ονοματεπώνυμο:: Αναστάσιος Παντζαρτζής (Anastasios Pantzartzis) | |
| [*]Email::apantzar@csd.auth.gr |
*--------------------------------------------------------------------------------*
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Cores {
public static void main(String[] args) throws IOException {
int coreValue = 0; //first line of the txt will be stored here(coreValue is equal to zero(0)).
//arrayListNumber1: user demands -> number of core
ArrayList<Integer> arrayListNumber1 = new ArrayList<>();
//arrayListNumber2: price provided / core.
ArrayList<Float> arrayListNumber2 = new ArrayList<>();
try (BufferedReader in = new BufferedReader( new FileReader((args[0])));)
{
String stringL1;
int lineCounter=1;
//reading from file txt
while ((stringL1 = in.readLine()) != null)
{
if (lineCounter==1){
//convert INT --> STRING
coreValue=Integer.parseInt(stringL1);
lineCounter++;
continue;
}
//put lines been spited in file:in dif variables
String[] stringL2=stringL1.split(" ");
int xValue=Integer.parseInt(stringL2[0]);
float yValue=Float.parseFloat(stringL2[1]);
//adding the element to our arraylist
arrayListNumber1.add(xValue);
arrayListNumber2.add(yValue);
lineCounter++;
}
}
/*
*---------------------------------------------------------------------------------------------------------------------------*
[*]exercise 1...
[*] using coins
INFO :: MIN COINS (Minimum)
--->https://stackoverflow.com/questions/4247662/the-minimum-number-of-coins-the-sum-of-which-is-s/4247707
*----------------------------------------------------------------------------------------------------------------------------*
*/
//coins array
int coins[] = {1, 2, 7, 11};
Minimum minValueDynamic = new Minimum();
for (Integer x:arrayListNumber1) {
minValueDynamic.minCoinDownToUp(x, coins, arrayListNumber1.indexOf(x));
}
/*
[*] exercise 2 ..
[*] using knapsack
*/
knapsackClass knapValue = new knapsackClass();
knapValue.algorithUsedInKnapsack(arrayListNumber2, arrayListNumber1, coreValue);
}
}
/*
INFO FOR knapsackClass ::
-->https://en.wikipedia.org/wiki/Knapsack_problem
-->https://medium.com/@fabianterh/how-to-solve-the-knapsack-problem-with-dynamic-programming-eb88c706d3cf
*/
class knapsackClass {
/**
* @param pricePercore -> price/core
* @param coreTotal -> the value:: coresUsing the client needs
* @param coresUsing -> total coresUsing
*/
/*
Using Knapsack algorithm
->[*] Creates total2d array.
->[*] Fill Array : if coreTotal is bigger than the current coresUsing(jCounter) then "total" is equal with the above.
->Else becomes the value MAX from (^) above and (one:: -1 row and :: -coreTotal)
total price that the client "SUBMITS".
*/
public void algorithUsedInKnapsack(ArrayList pricePercore, ArrayList coreTotal, int coresUsing) {
float total2d[][] = new float[pricePercore.size() + 1][coresUsing + 1];
for (int iCounter = 1; iCounter < pricePercore.size() + 1; iCounter++) {
total2d[0][iCounter]=0;
float price = (float) pricePercore.get(iCounter - 1); //VALUE OF PRICE
int coreAm = (int) coreTotal.get(iCounter - 1);
for (int jCounter = 1; jCounter < coresUsing + 1; jCounter++) {
if (coreAm > jCounter)
total2d[iCounter][jCounter] = total2d[iCounter - 1][jCounter];
else
total2d[iCounter][jCounter] = Math.max(total2d[iCounter - 1][jCounter], total2d[iCounter - 1][jCounter - coreAm] + price * coreAm);
}
}
magicPrinter(pricePercore, coreTotal, coresUsing, total2d);
}
/**
* [*}Using magicPrinter in order to print results
* [*]Select client:
* ->change when above (^) number is not equal to (this) current
*/
public void magicPrinter(ArrayList ar, ArrayList coreTotal, int numberOfCores, float[][] total2d) {
int valueForSize = ar.size();
int valueForSizer = numberOfCores;
String sString;
sString = "Clients accepted: ";
while (valueForSize > 0 && valueForSizer > 0) {
if (total2d[valueForSize][valueForSizer] == total2d[valueForSize - 1][valueForSizer]) {
valueForSize -= 1;
}
else {
sString += valueForSize;
valueForSize -= 1;
valueForSizer -= (int) coreTotal.get(valueForSize);
if (valueForSize > 0 && valueForSizer > 0) {
sString += ",";
}
}
}
//System.out.println(sString.substring(0, sString.length() - 1));
System.out.println("Total amount: " + total2d[ar.size()][numberOfCores]);
}
}
class Minimum {
/**
* @param totalValue -> Amount of cores asked from client.
* @param comOfCoins -> The combinations of Coins.
* @param numberOfClient -> Number of clients
*/
/*
[*] Min coin Down to Up to solve the problem (D->U).
[*] If the input != sorted:
-> Otherwise jCounter - exampleArr[iCounter] + 1 will be Integer.Max_value + 1 (possible to be low negative)
[*] Finally, (Integer.MAX_VALUE - 1) if solution is not possible enough.
*/
public void minCoinDownToUp(int totalValue, int comOfCoins[], int numberOfClient){
int iTAr[] = new int[totalValue + 1];
int iRAr[] = new int[totalValue + 1];
//init first element -> 0
iTAr[0] = 0;
for(int iCounter=1; iCounter <= totalValue; iCounter++){
iTAr[iCounter] = Integer.MAX_VALUE-1;
iRAr[iCounter] = -1;
}
for(int jCounter=0; jCounter < comOfCoins.length; jCounter++){
for(int internalIcounter=1; internalIcounter <= totalValue; internalIcounter++){
if(internalIcounter >= comOfCoins[jCounter]){
if (iTAr[internalIcounter - comOfCoins[jCounter]] + 1 < iTAr[internalIcounter]) {
iTAr[internalIcounter] = 1 + iTAr[internalIcounter - comOfCoins[jCounter]];
iRAr[internalIcounter] = jCounter;
}
}
}
}
magicPrinterCompinationOfCoins(iRAr, comOfCoins,numberOfClient);
}
private void magicPrinterCompinationOfCoins(int rArray[], int strCoinsAr[], int numberOfClient) {
if (rArray[rArray.length - 1] == -1) {
System.out.print("No solution is possible");
return;
}
/*
[*] adder: icores needed
[*] adder: starts from zero(0)
-> is int => init in zero when created
[*]dynamic allocation
->for adder using "new"
*/
int adder[]=new int[strCoinsAr.length];
//startPoint -> list size - 1
int startPoint = rArray.length - 1;
while ( startPoint != 0 ) {
int xCounter = rArray[startPoint];
adder[xCounter]+=1;
startPoint = startPoint - strCoinsAr[xCounter];
}
/*
[*] Let's print :: printing results for client
[*] limit < strCoinsAr
*/
System.out.print("Client "+(numberOfClient+1)+":");
int value1 =0;
int value2=0;
int value3 =0;
int valuetotal=0;
for (int printerCounter=0;printerCounter<strCoinsAr.length;printerCounter++){
if(printerCounter==0)//Checking::if printerCounter is zero
value1=adder[printerCounter];
//System.out.printf(" %d %d-core", adder[printerCounter],strCoinsAr[printerCounter]);//PRINT FOR TEST
else if(printerCounter!=strCoinsAr.length-1)
//System.out.printf(", %d %d-core", adder[printerCounter],strCoinsAr[printerCounter]);//PRINT FOR TEST
value2=adder[printerCounter];
//strCoinsAr[printerCounter]= strCoinsAr[printerCounter];
else {
//System.out.printf(" and %d %d-core ", adder[printerCounter],strCoinsAr[printerCounter]);//PRINT FOR TEST
value3 = adder[printerCounter];
}
valuetotal = value1 + value2 + value3;
// System.out.printf(" %d",valuetotal);//PRINT FOR TEST
}
System.out.printf(" %d ",valuetotal);
System.out.println("VMs");//PRINT
}
}