-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTransportationProblem.java
More file actions
224 lines (165 loc) · 6.76 KB
/
TransportationProblem.java
File metadata and controls
224 lines (165 loc) · 6.76 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transportationproblem;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
/**
*
* @author amjad
*/
public class TransportationProblem {
double []required;
double []stock;
double [][]cost;
LinkedList<Variable> feasible = new LinkedList<Variable>();
int stockSize;
int requiredSize;
public TransportationProblem(int stockSize, int requiredSize ){
this.stockSize = stockSize;
this.requiredSize = requiredSize;
stock = new double[stockSize];
required = new double[requiredSize];
cost = new double[stockSize][requiredSize];
for(int i=0; i < (requiredSize + stockSize -1); i++)
feasible.add(new Variable());
}
public void setStock(double value, int index){
stock[index] = value;
}
public void setRequired(double value, int index){
required[index] = value;
}
public void setCost(double value, int stock, int required){
cost[stock][required] = value;
}
/**
* initializes the feasible solution list using the North-West Corner
* @return time elapsed
*/
public double northWestCorner() {
long start = System.nanoTime();
double min;
int k = 0; //feasible solutions counter
//isSet is responsible for annotating cells that have been allocated
boolean [][]isSet = new boolean[stockSize][requiredSize];
for (int j = 0; j < requiredSize; j++)
for (int i = 0; i < stockSize; i++)
isSet[i][j] = false;
//the for loop is responsible for iterating in the 'north-west' manner
for (int j = 0; j < requiredSize; j++)
for (int i = 0; i < stockSize; i++)
if(!isSet[i][j]){
//allocating stock in the proper manner
min = Math.min(required[j], stock[i]);
feasible.get(k).setRequired(j);
feasible.get(k).setStock(i);
feasible.get(k).setValue(min);
k++;
required[j] -= min;
stock[i] -= min;
//allocating null values in the removed row/column
if(stock[i] == 0)
for(int l = 0; l < requiredSize; l++)
isSet[i][l] = true;
else
for(int l = 0; l < stockSize; l++)
isSet[l][j] = true;
}
return (System.nanoTime() - start) * 1.0e-9;
}
/**
* initializes the feasible solution list using the Least Cost Rule
*
* it differs from the North-West Corner rule by the order of candidate cells
* which is determined by the corresponding cost
*
* @return double: time elapsed
*/
public double leastCostRule() {
long start = System.nanoTime();
double min;
int k = 0; //feasible solutions counter
//isSet is responsible for annotating cells that have been allocated
boolean [][]isSet = new boolean[stockSize][requiredSize];
for (int j = 0; j < requiredSize; j++)
for (int i = 0; i < stockSize; i++)
isSet[i][j] = false;
int i = 0, j = 0;
Variable minCost = new Variable();
//this will loop is responsible for candidating cells by their least cost
while(k < (stockSize + requiredSize - 1)){
minCost.setValue(Double.MAX_VALUE);
//picking up the least cost cell
for (int m = 0; m < stockSize; m++)
for (int n = 0; n < requiredSize; n++)
if(!isSet[m][n])
if(cost[m][n] < minCost.getValue()){
minCost.setStock(m);
minCost.setRequired(n);
minCost.setValue(cost[m][n]);
}
i = minCost.getStock();
j = minCost.getRequired();
//allocating stock in the proper manner
min = Math.min(required[j], stock[i]);
feasible.get(k).setRequired(j);
feasible.get(k).setStock(i);
feasible.get(k).setValue(min);
k++;
required[j] -= min;
stock[i] -= min;
//allocating null values in the removed row/column
if(stock[i] == 0)
for(int l = 0; l < requiredSize; l++)
isSet[i][l] = true;
else
for(int l = 0; l < stockSize; l++)
isSet[l][j] = true;
}
return (System.nanoTime() - start) * 1.0e-9;
}
public double getSolution(){
double result = 0;
for(Variable x: feasible){
result += x.getValue() * cost[x.getStock()][x.getRequired()];
}
return result;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the problem size:");
int s = scanner.nextInt();
int r = scanner.nextInt();
double x;
TransportationProblem test = new TransportationProblem(s, r);
System.out.println("Please enter the stocks capacity:");
for (int i = 0; i < test.stockSize; i++){
x = scanner.nextDouble();
test.setStock(x, i);
}
System.out.println("Please enter the requirements:");
for (int i = 0; i < test.requiredSize; i++){
x = scanner.nextDouble();
test.setRequired(x, i);
}
System.out.println("Please enter the transportation costs:");
for (int i = 0; i < test.stockSize; i++)
for (int j = 0; j < test.requiredSize; j++) {
x = scanner.nextDouble();
test.setCost(x, i, j);
}
//test.northWestCorner();
test.leastCostRule();
for(Variable t: test.feasible){
System.out.println(t);
}
System.out.println("Target function: " + test.getSolution());
}
}