-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDoublingTestPlot.java
171 lines (153 loc) · 7.31 KB
/
DoublingTestPlot.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
162
163
164
165
166
167
168
169
import java.awt.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class DoublingTestPlot {
private int numberOfTests;
private boolean cached;
public DoublingTestPlot(int numberOfTests, boolean cached){
this.numberOfTests = numberOfTests;
this.cached = cached;
}
public void plot() throws InterruptedException, IOException {
setDrawSettings();
double[][] NAndTime = new double[numberOfTests][2];
int NAndTimeSize = 0;
double max;
if (cached) {
int countDoubles = 0;
File file = new File("cache.txt");
if (file.createNewFile()){
System.out.println("cache file is created");
}
In in = new In("cache.txt");
double[] doubles = in.readAllDoubles();
for (int i = 0; i < doubles.length / 2; i++) {
NAndTime[i][0] = doubles[countDoubles++];
NAndTime[i][1] = doubles[countDoubles++];
}
FileWriter writer = new FileWriter(file);
for (int i = 0; i <= 3; i++) {
double N = 1000.0 * Math.pow(2, i);
if (NAndTime[i][0] == N) {
max = NAndTime[i][1];
StdOut.println(N + " " + NAndTime[i][1]);
NAndTimeSize++;
} else {
double time = DoublingTest.timeTrial((int) N);
StdOut.println(N + " " + time);
max = time;
NAndTime[i][0] = N;
NAndTime[i][1] = time;
writer.append(NAndTime[i][0] + " " + NAndTime[i][1] + " ");
NAndTimeSize++;
}
StdDraw.clear();
drawNormalPlot(NAndTime, NAndTimeSize, max);
drawLogPlot(NAndTime, NAndTimeSize, max);
drawNormalCurve(NAndTime, NAndTimeSize, max);
drawLogCurve(NAndTime, NAndTimeSize, max);
Thread.sleep(1000);
}
writer.close();
} else {
for (int i = 0; i <= 3; i++) {
double N = 1000.0 * Math.pow(2, i);
double time = DoublingTest.timeTrial((int) N);
StdOut.println(N + " " + time);
max = time;
NAndTime[i][0] = N;
NAndTime[i][1] = time;
NAndTimeSize++;
StdDraw.clear();
drawNormalPlot(NAndTime, NAndTimeSize, max);
drawLogPlot(NAndTime, NAndTimeSize, max);
drawNormalCurve(NAndTime, NAndTimeSize, max);
drawLogCurve(NAndTime, NAndTimeSize, max);
Thread.sleep(1000);
}
}
}
private void drawNormalPlot(double[][] NAndTime, int NAndTimeSize, double max) {
// X axis
StdDraw.line(0.0, 0.0, 1.1, 0.0);
// Y axis
StdDraw.line(0.0, 0.0, 0.0, 1.1);
for (int i = 0; i < NAndTimeSize; i++) {
// X axis serifs
StdDraw.line(0.0, NAndTime[i][1] / max, -0.05, NAndTime[i][1] / max);
// Y axis serifs
StdDraw.line(NAndTime[i][0] * 0.0001, 0.0, NAndTime[i][0] * 0.0001, -0.05);
// Y axis digits
StdDraw.text(-0.1, NAndTime[i][1] / max, NAndTime[i][1] + "");
// X axis digits
StdDraw.text(NAndTime[i][0] * 0.0001, -0.1, (int) (NAndTime[i][0] * 0.001) + "K");
}
StdDraw.setPenColor(StdDraw.BOOK_RED);
StdDraw.text(0.0, 1.2, "standard plot");
StdDraw.text(0.5 ,-0.2, "problem size N");
StdDraw.text(1.96, -0.2, "lg N");
StdDraw.text(-0.3, 0.57, "running");
StdDraw.text(-0.3, 0.5, "time");
StdDraw.text(-0.3, 0.43, "T(N)");
StdDraw.text(1.4, 0.5, "lg(T(N))");
StdDraw.text(1.8, 1.2, "log-log plot");
StdDraw.setPenColor(StdDraw.BLACK);
}
private void setDrawSettings() {
StdDraw.setCanvasSize(1000, 500);
StdDraw.setXscale(-0.5, 3.0);
StdDraw.setYscale(-0.5, 1.5);
Font font = new Font("SansSerif", Font.PLAIN, 12);
StdDraw.setFont(font);
}
private void drawNormalCurve(double[][] NAndTime, int NAndTimeSize, double max) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(StdDraw.getPenRadius() + 0.009);
for (int i = 0; i < NAndTimeSize; i++) {
StdDraw.point(NAndTime[i][0] * 0.0001, NAndTime[i][1] / max);
}
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(StdDraw.getPenRadius() - 0.009);
for (int i = 0; i < NAndTimeSize - 1; i++) {
StdDraw.line(NAndTime[i][0] * 0.0001, NAndTime[i][1] / max, NAndTime[i + 1][0] * 0.0001, NAndTime[i + 1][1] / max);
}
}
private void drawLogPlot(double[][] NAndTime, int NAndTimeSize, double max) {
for (int i = 0; i < NAndTimeSize; i++) {
// X axis
if (NAndTimeSize == 1) {
StdDraw.line((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.7, 0.0, (Math.log(NAndTime[0][0] * 100) * 0.1) + 0.7 + 0.1, 0.0);
} else {
StdDraw.line((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.7, 0.0, (Math.log(NAndTime[NAndTimeSize - 1][0] * 100) * 0.1) + 0.7, 0.0);
}
// Y axis
if (NAndTimeSize == 1) {
StdDraw.line((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6, Math.log(NAndTime[0][1] * 100) / Math.log(max * 100), (Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6, 0.1);
} else {
StdDraw.line((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6, Math.log(1.1), (Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6, (Math.log(NAndTime[NAndTimeSize - 1][1] * 100) / Math.log(max * 100)) + 0.1);
}
// Y axis serifs
StdDraw.line((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6, Math.log(NAndTime[i][1] * 100) / Math.log(max * 100), (Math.log(NAndTime[0][0] * 100) * 0.1) + 0.6 - 0.05, Math.log(NAndTime[i][1] * 100) / Math.log(max * 100));
// X axis serifs
StdDraw.line((Math.log(NAndTime[i][0] * 100) * 0.1) + 0.7, 0.0, (Math.log(NAndTime[i][0] * 100) * 0.1) + 0.7, -0.05);
// Y axis digits
StdDraw.text((Math.log(NAndTime[0][0] * 100) * 0.1) + 0.45, Math.log(NAndTime[i][1] * 100) / Math.log(max * 100), NAndTime[i][1] + "");
// X axis digits
StdDraw.text((Math.log(NAndTime[i][0] * 100) * 0.1) + 0.7, -0.1, (int) (NAndTime[i][0] * 0.001) + "K");
}
}
private void drawLogCurve(double[][] NAndTime, int NAndTimeSize, double max) {
for (int i = 0; i < NAndTimeSize; i++) {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.setPenRadius(StdDraw.getPenRadius() + 0.009);
// Print point coordinates
StdDraw.point((Math.log(NAndTime[i][0] * 100) * 0.1) + 0.7, Math.log(NAndTime[i][1] * 100) / Math.log(max * 100));
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(StdDraw.getPenRadius() - 0.009);
}
for (int i = 0; i < NAndTimeSize - 1; i++) {
StdDraw.line((Math.log(NAndTime[i][0] * 100) * 0.1) + 0.7, (Math.log(NAndTime[i][1] * 100) / Math.log(max * 100)), (Math.log(NAndTime[i + 1][0] * 100) * 0.1) + 0.7, (Math.log(NAndTime[i + 1][1] * 100) / (Math.log(max * 100))));
}
}
}