-
Notifications
You must be signed in to change notification settings - Fork 0
/
Print.java
161 lines (143 loc) · 4.47 KB
/
Print.java
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
package CVRP;
import java.util.ArrayList;
import java.util.List;
import java.lang.String;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Print
{
public static void print() {
print("");
}
public static void print(Object o) {
print(o, true);
}
public static void print(Object o, boolean newLine) {
if (newLine)
System.out.println(o);
else
System.out.print(o);
}
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++)
print(array[i]);
}
public static void printArray(Object[] array) {
for (int i = 0; i < array.length; i++)
print(array[i]);
}
public static void printList(List list)
{
for(int i=0;i<list.size();i++)
{
if(i<(list.size()-1))
{
System.out.print(list.get(i));
System.out.print(",");
}
else
System.out.println(list.get(i));
}
}
public static void printConfig()
{
}
public static void writeResult(String fileName, String loginName, String candidateNum, String name, String description, double lowestCost, List<Integer> bestSolution)
{
try
{
File Text = new File(fileName);
FileOutputStream fos = new FileOutputStream(Text);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter w = new BufferedWriter(osw);
w.write("login "+loginName+" "+candidateNum);
w.newLine();
w.write("name "+name);
w.newLine();
w.write("algorithm "+description);
w.newLine();
w.write("cost "+ String.format("%.3f", lowestCost));
w.newLine();
List<List<Integer>> vehicleRouteList = new ArrayList<List<Integer>>();
List<Integer> depotNodes = new ArrayList<Integer>();
//find the position of the depot node in the list
for(int i=0;i<bestSolution.size();i++)
{
if(bestSolution.get(i)==1)
depotNodes.add(i);
}
for(int i=0;i<depotNodes.size()-1;i++)
{
List<Integer> vehicleRoute = new ArrayList<Integer>();
//System.out.println(depotNodes.get(i));
vehicleRoute.addAll(bestSolution.subList(depotNodes.get(i)+1, depotNodes.get(i+1)));
//System.out.println(vehicleRoute.size());
vehicleRouteList.add(vehicleRoute);
//Print.printList(vehicleRouteList.get(i));
}
for(int i=0;i<vehicleRouteList.size();i++)
{
//System.out.println(vehicleRouteList.get(i));
w.write("1->");
for(int j=0;j<vehicleRouteList.get(i).size();j++)
{
w.write(Integer.toString(vehicleRouteList.get(i).get(j))+"->");
}
w.write("1");
w.newLine();
}
w.close();
}
catch (IOException e)
{
System.err.println("Problem writing to the file "+fileName);
}
}
static FileInputStream inStream; // = new FileInputStream("best-solution.txt");
static DataInputStream dStream;
static BufferedReader reader;
public static ArrayList<Integer> readResult(String fileName)
{
ArrayList<Integer> route = new ArrayList<Integer>();
try
{
inStream = new FileInputStream(fileName);
dStream = new DataInputStream(inStream);
reader = new BufferedReader(new InputStreamReader(inStream));
String line;
String delim = "->";
//route.add(1);
while ((line = reader.readLine()) != null)
{
//route = new ArrayList<Integer>();
if (line.startsWith("login") || line.startsWith("name") || line.startsWith("algorithm") || line.startsWith("cost"))
continue;
String[] tokens = line.split(delim);
for (int i = 0; i < tokens.length-1; i++)
{
//route.add(new Integer(tokens[i]));
route.add(Integer.valueOf(tokens[i]));
}
}
route.add(1);
}
catch (FileNotFoundException e)
{
System.err.println("Error: best-solution.txt not found.");
}
catch (IOException e)
{
System.err.println("Error whilst reading from best-solution.txt. Is it formatted correctly?");
}
return route;
}
}