Skip to content

Commit 68185f2

Browse files
author
fnieri
committed
Refactored code and moved out matplot code
1 parent b970f23 commit 68185f2

18 files changed

+773
-55
lines changed

analyse_generation.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import time
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
from generate_model import *
5+
6+
7+
count = 0
8+
instances = [_ for _ in range(1, 11 )]
9+
solveDensity1 = []
10+
solveDensity2 = []
11+
solveDensity3 = []
12+
13+
for i in range(1, 15):
14+
with open(f"results{i}.txt", "r") as file:
15+
solveDensity1tmp = []
16+
solveDensity2tmp = []
17+
solveDensity3tmp = []
18+
19+
for line in file:
20+
21+
instance, generate_time, solve_time= line.strip().split(",")
22+
timesToGenerate.append(float(generate_time))
23+
timesToSolve.append(float(solve_time))
24+
#arcs.append(int(arc))d
25+
if count == 0:
26+
solveDensity1tmp.append(float(solve_time))
27+
elif count == 1:
28+
solveDensity2tmp.append(float(solve_time))
29+
else:
30+
solveDensity3tmp.append(float(solve_time))
31+
count += 1
32+
if count % 3 == 0:
33+
count = 0
34+
solveDensity1.append(solveDensity1tmp)
35+
solveDensity2.append(solveDensity2tmp)
36+
solveDensity3.append(solveDensity3tmp)
37+
solveDensity1 = np.array(solveDensity1)
38+
solveDensity2 = np.array(solveDensity2)
39+
solveDensity3 = np.array(solveDensity3)
40+
#plt.plot(instances, timesToGenerate, marker='o', linestyle='-',label="Time to generate instance")
41+
#plt.plot(instances, timesToSolve, marker='o', linestyle='-', label="Time to solve instance")
42+
#plt.xlabel("Instance number")
43+
#plt.ylabel("Time")
44+
mean_values = np.mean(solveDensity1, axis=0)
45+
std_values = np.std(solveDensity1, axis=0)
46+
47+
mean_values1 = np.mean(solveDensity2, axis=0)
48+
std_values1 = np.std(solveDensity2, axis=0)
49+
50+
mean_values2 = np.mean(solveDensity3, axis=0)
51+
std_values2 = np.std(solveDensity3, axis=0)
52+
t = np.arange(0, 15, 1)
53+
# Plot the mean line
54+
plt.plot(mean_values, color='green', label='Mean for instances of density 1')
55+
# Plot the shaded area representing the standard deviation
56+
plt.fill_between(t, mean_values - std_values, mean_values + std_values, color='red',
57+
alpha=0.3, label='Standard Deviation for instances of density 1')
58+
plt.plot(mean_values1, color='red', label='Mean for instances of density 2')
59+
# Plot the shaded area representing the standard deviation
60+
plt.fill_between(t, mean_values1 - std_values1, mean_values1 + std_values1, color='blue',
61+
alpha=0.3, label='Standard Deviation for instances of density 2')
62+
plt.plot(mean_values2, color='blue', label='Mean for instances of density 3')
63+
# Plot the shaded area representing the standard deviation
64+
plt.fill_between(t, mean_values2 - std_values2, mean_values2 + std_values2, color='green',
65+
alpha=0.3, label='Standard Deviation for instances of density 3')
66+
67+
# Set labels and title
68+
plt.xlabel('Number of instance')
69+
plt.ylabel('Time to solve')
70+
plt.title('Mean time to solve instance with standard deviation')
71+
72+
# Display the legend
73+
plt.legend()
74+
75+
76+
plt.show()

generate_model.py

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22
import sys
3-
from collections import defaultdict
43
import time
5-
import matplotlib.pyplot as plt
64
NODES = "nodes"
75
SOURCE = "source"
86
SINK = "sink"
@@ -16,8 +14,6 @@
1614
timesToGenerate = []
1715
timesToSolve = []
1816
instances = []
19-
arcs = []
20-
2117

2218
class ModelGenerator:
2319
def __init__(self, filename: str):
@@ -73,7 +69,7 @@ def addArc(self, source: str, destination: str, flow: str):
7369
"""
7470
Add an arc from the source to the 2
7571
"""
76-
if source != destination and source != self.sink and destination != self.source:
72+
if source != destination: #and source != self.sink and destination != self.source:
7773
self.bounds.append(f"0 <= x_{source}_{destination} <= {flow}")
7874
self.subjectTo[int(source)] += f" + x_{source}_{destination}"
7975
self.subjectTo[int(destination)] += f" - x_{source}_{destination}"
@@ -112,7 +108,7 @@ def generateModel(self):
112108
def solveModel(self):
113109
self.generateModel()
114110
startTime = time.time()
115-
os.system(f"glpsol --lp {self.modelName}.lp -o {self.modelName}.sol")
111+
os.system(f"glpsol --lp {self.modelName}.lp -o {self.modelName}.sol > /dev/null")
116112
endTime = time.time()
117113
timesToSolve.append(endTime - startTime)
118114

@@ -165,61 +161,32 @@ def isOptimal(self):
165161
self.findSTCut()
166162
return self.sink not in self.visited
167163

168-
def main():
164+
165+
def main(filename: str):
166+
model_gen = ModelGenerator(filename)
167+
model_gen.solveModel()
168+
graph = Graph(model_gen.nodes, model_gen.source, model_gen.sink, model_gen.modelName)
169+
170+
171+
if __name__ == "__main__":
172+
try:
173+
filename = sys.argv[1]
174+
# main(filename)
175+
except IndexError:
176+
print("Enter a filename")
169177
instance = 0
170178
for nodes in range(100, 1600, 100):
171179
for density in range(1, 4):
172180
generate_model = f"Instances/inst-{nodes}-0.{density}.txt"
173181
generator = ModelGenerator(generate_model)
174182
generator.solveModel()
175-
graph = Graph(generator.nodes, generator.source, generator.sink, generator.modelName)
176-
print(graph.isOptimal())
177-
178-
#def main(filename: str):
179-
# model_gen = ModelGenerator(filename)
180-
# model_gen.solveModel()
181-
# graph = Graph(model_gen.nodes, model_gen.source, model_gen.sink, model_gen.modelName)
182-
#print(graph.isOptimal())
183+
# graph = Graph(generator.nodes, generator.source, generator.sink, generator.modelName)
184+
# print(graph.isOptimal())
185+
instances.append(instance)
186+
instance += 1
187+
with open("results14.txt", "w") as result_file:
188+
for instance, generate_time, solve_time in zip(instances, timesToGenerate, timesToSolve):
189+
result_file.write(f"{instance}, {generate_time}, {solve_time}\n")
183190

184-
if __name__ == "__main__":
185-
try:
186-
filename = sys.argv[1]
187-
#main(filename)
188-
main()
189-
except IndexError:
190-
print("Enter a filename")
191-
"""
192-
with open("results.txt", "r") as file:
193-
for line in file:
194-
instance, generate_time, solve_time, arc = line.strip().split(",")
195-
instances.append(int(instance))
196-
timesToGenerate.append(float(generate_time))
197-
timesToSolve.append(float(solve_time))
198-
arcs.append(int(arc))
199-
200-
plt.plot(instances, timesToGenerate, marker='o', linestyle='-',label="Time to generate instance")
201-
plt.plot(instances, timesToSolve, marker='o', linestyle='-', label="Time to solve instance")
202-
plt.xlabel("Instance number")
203-
plt.ylabel("Time")
204-
# Adjust the x-axis tick spacing
205-
plt.xticks(instances[::10]) # Set the x-axis ticks at every 10th instance
206-
plt.xticks(rotation=45, ha='right') # Rotate the x-axis labels by 45 degrees and align them to the right
207-
plt.scatter(instances, timesToGenerate, color='red')
208-
plt.scatter(instances, timesToSolve, color='blue')
209-
210-
plt.legend()
211-
plt.show()
212-
213-
for j in range(len(arcs)-1):
214-
for i in range(len(arcs)-1):
215-
if arcs[i] > arcs[i+1]:
216-
arcs[i], arcs[i+1] = arcs[i+1], arcs[i]
217-
timesToSolve[i], timesToSolve[i+1] = timesToSolve[i+1], timesToSolve[i]
218-
timesToGenerate[i], timesToGenerate[i+1] = timesToGenerate[i+1], timesToGenerate[i]
219-
plt.plot(arcs, timesToSolve)
220-
plt.xlabel("Number of arcs")
221-
plt.ylabel("Time to solve")
222-
plt.show()
223-
"""
224191

225192

minimum_cut.py

Whitespace-only changes.

results0.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
0, 0.0020956993103027344, 0.013704776763916016
2+
0, 0.008342266082763672, 0.02321314811706543
3+
0, 0.014306068420410156, 0.03284502029418945
4+
0, 0.015241622924804688, 0.045037269592285156
5+
0, 0.031789302825927734, 0.06821417808532715
6+
0, 0.04496884346008301, 0.1046600341796875
7+
0, 0.01851963996887207, 0.07089853286743164
8+
0, 0.053299903869628906, 0.15282368659973145
9+
0, 0.07927632331848145, 0.23514509201049805
10+
0, 0.057166337966918945, 0.10442495346069336
11+
0, 0.07078957557678223, 0.2992992401123047
12+
0, 0.1027219295501709, 0.5418307781219482
13+
0, 0.05078005790710449, 0.17583250999450684
14+
0, 0.11251497268676758, 0.5554444789886475
15+
0, 0.16904330253601074, 0.9185295104980469
16+
0, 0.07109260559082031, 0.3460366725921631
17+
0, 0.15367341041564941, 0.8601117134094238
18+
0, 0.23962759971618652, 1.5588691234588623
19+
0, 0.0978546142578125, 0.4771759510040283
20+
0, 0.21620631217956543, 1.5462980270385742
21+
0, 0.362567663192749, 2.4656050205230713
22+
0, 0.12975597381591797, 0.7761869430541992
23+
0, 0.27866220474243164, 2.1118245124816895
24+
0, 0.48675107955932617, 4.013125896453857
25+
0, 0.17588329315185547, 1.1169764995574951
26+
0, 0.41699838638305664, 2.9746413230895996
27+
0, 0.6136248111724854, 5.143832206726074
28+
0, 0.2230219841003418, 1.5704264640808105
29+
0, 0.4704103469848633, 3.9226431846618652
30+
0, 0.7905941009521484, 7.231858968734741
31+
0, 0.2581501007080078, 1.405287742614746
32+
0, 0.5807061195373535, 4.676584959030151
33+
0, 1.0100584030151367, 9.878384828567505
34+
0, 0.31455492973327637, 1.912799596786499
35+
0, 0.7616591453552246, 7.605779409408569
36+
0, 1.2772817611694336, 11.73460578918457
37+
0, 0.37662315368652344, 3.8534955978393555
38+
0, 0.8865969181060791, 8.34248423576355
39+
0, 1.5448710918426514, 14.647265672683716
40+
0, 0.4527468681335449, 3.091582775115967
41+
0, 1.0533394813537598, 10.61623477935791
42+
0, 1.8634376525878906, 18.773932933807373
43+
0, 0.5352470874786377, 5.302542686462402
44+
0, 1.2766056060791016, 12.917662858963013
45+
0, 2.2229702472686768, 23.45106291770935

results1.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
0, 0.002016305923461914, 0.014663457870483398
2+
0, 0.009421586990356445, 0.022015810012817383
3+
0, 0.014834165573120117, 0.0291135311126709
4+
0, 0.015236139297485352, 0.0300447940826416
5+
0, 0.01975703239440918, 0.05937314033508301
6+
0, 0.024647951126098633, 0.0873880386352539
7+
0, 0.02371835708618164, 0.06298685073852539
8+
0, 0.043706417083740234, 0.14528465270996094
9+
0, 0.05858278274536133, 0.23894500732421875
10+
0, 0.03435325622558594, 0.10364985466003418
11+
0, 0.06807899475097656, 0.29592394828796387
12+
0, 0.09673714637756348, 0.5363826751708984
13+
0, 0.04659104347229004, 0.1762552261352539
14+
0, 0.11314105987548828, 0.5543336868286133
15+
0, 0.15712237358093262, 0.9412624835968018
16+
0, 0.07021141052246094, 0.33469343185424805
17+
0, 0.1551375389099121, 0.8768930435180664
18+
0, 0.246964693069458, 1.5420963764190674
19+
0, 0.09592413902282715, 0.4849362373352051
20+
0, 0.20752453804016113, 1.534580945968628
21+
0, 0.3340904712677002, 2.4680397510528564
22+
0, 0.1381053924560547, 0.77970290184021
23+
0, 0.2794053554534912, 2.019005298614502
24+
0, 0.4561920166015625, 3.9142181873321533
25+
0, 0.16580724716186523, 1.0335636138916016
26+
0, 0.36572790145874023, 2.9665074348449707
27+
0, 0.6065404415130615, 5.116588830947876
28+
0, 0.2033379077911377, 1.5460264682769775
29+
0, 0.467022180557251, 3.8758914470672607
30+
0, 0.776242733001709, 7.199347496032715
31+
0, 0.26485633850097656, 1.4158685207366943
32+
0, 0.601677417755127, 4.729222774505615
33+
0, 1.0046560764312744, 9.875152587890625
34+
0, 0.3226180076599121, 1.915661096572876
35+
0, 0.7356350421905518, 6.503028154373169
36+
0, 1.2464349269866943, 11.833600759506226
37+
0, 0.38566112518310547, 4.479432106018066
38+
0, 0.9795017242431641, 8.771806240081787
39+
0, 1.6507415771484375, 15.440086126327515
40+
0, 0.4693300724029541, 3.242736339569092
41+
0, 1.0666203498840332, 10.897948741912842
42+
0, 1.951451301574707, 23.181598663330078
43+
0, 0.7962324619293213, 7.949961185455322
44+
0, 1.8484976291656494, 16.285173654556274
45+
0, 3.2128922939300537, 29.331610679626465

results10.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
0, 0.012818098068237305, 0.019315242767333984
2+
1, 0.00907444953918457, 0.01869821548461914
3+
2, 0.01294565200805664, 0.02750229835510254
4+
3, 0.016394853591918945, 0.02851700782775879
5+
4, 0.023014068603515625, 0.06387042999267578
6+
5, 0.029115676879882812, 0.09184074401855469
7+
6, 0.02298569679260254, 0.05844569206237793
8+
7, 0.041597843170166016, 0.15499496459960938
9+
8, 0.07422757148742676, 0.23647809028625488
10+
9, 0.05288338661193848, 0.10351085662841797
11+
10, 0.07686352729797363, 0.3009450435638428
12+
11, 0.1134328842163086, 0.5432910919189453
13+
12, 0.0636148452758789, 0.1739330291748047
14+
13, 0.10860586166381836, 0.565706729888916
15+
14, 0.17905211448669434, 0.9239521026611328
16+
15, 0.08097004890441895, 0.33347249031066895
17+
16, 0.16515803337097168, 0.8636152744293213
18+
17, 0.25286173820495605, 1.5761456489562988
19+
18, 0.11247920989990234, 0.48567843437194824
20+
19, 0.22503352165222168, 1.5528223514556885
21+
20, 0.36699390411376953, 2.473513603210449
22+
21, 0.13156867027282715, 0.7792236804962158
23+
22, 0.2858314514160156, 2.0628273487091064
24+
23, 0.4775242805480957, 3.9347047805786133
25+
24, 0.20172548294067383, 1.045464038848877
26+
25, 0.3718528747558594, 2.957460641860962
27+
26, 0.6772289276123047, 5.225487947463989
28+
27, 0.2189488410949707, 1.590648889541626
29+
28, 0.4819493293762207, 3.8670589923858643
30+
29, 0.8115334510803223, 7.276402950286865
31+
30, 0.2567579746246338, 1.380441665649414
32+
31, 0.5936927795410156, 4.76715612411499
33+
32, 2.173457384109497, 10.385305643081665
34+
33, 0.32201147079467773, 1.8978006839752197
35+
34, 0.7836143970489502, 6.749059438705444
36+
35, 1.375976800918579, 13.871328830718994
37+
36, 0.3875291347503662, 4.248737573623657
38+
37, 0.9668564796447754, 8.348936557769775
39+
38, 1.5466926097869873, 14.636460065841675
40+
39, 0.4581122398376465, 3.1224682331085205
41+
40, 1.0793824195861816, 10.794933557510376
42+
41, 1.8680346012115479, 19.699820041656494
43+
42, 0.5597021579742432, 5.562687635421753
44+
43, 1.4082388877868652, 13.0486741065979
45+
44, 2.360111713409424, 23.8929922580719

results11.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
0, 0.011633157730102539, 0.01934075355529785
2+
1, 0.009612083435058594, 0.017809629440307617
3+
2, 0.013591289520263672, 0.024611711502075195
4+
3, 0.014622926712036133, 0.0336604118347168
5+
4, 0.023777246475219727, 0.05592608451843262
6+
5, 0.03209185600280762, 0.08871579170227051
7+
6, 0.02311539649963379, 0.056908369064331055
8+
7, 0.0397181510925293, 0.14590835571289062
9+
8, 0.06783080101013184, 0.2353811264038086
10+
9, 0.06052279472351074, 0.10563039779663086
11+
10, 0.07788491249084473, 0.2859976291656494
12+
11, 0.11618590354919434, 0.5419228076934814
13+
12, 0.049759626388549805, 0.1773052215576172
14+
13, 0.11577820777893066, 0.5482356548309326
15+
14, 0.17203354835510254, 0.9360122680664062
16+
15, 0.08922863006591797, 0.33127689361572266
17+
16, 0.16093969345092773, 0.8787550926208496
18+
17, 0.24955415725708008, 1.5632004737854004
19+
18, 0.10869574546813965, 0.4835622310638428
20+
19, 0.21755552291870117, 1.5476715564727783
21+
20, 0.34929466247558594, 2.467033624649048
22+
21, 0.14263606071472168, 0.7679116725921631
23+
22, 0.28635430335998535, 2.0411407947540283
24+
23, 0.46971631050109863, 3.93168568611145
25+
24, 0.17890644073486328, 1.0384845733642578
26+
25, 0.3787710666656494, 2.9731285572052
27+
26, 0.6243529319763184, 5.1940529346466064
28+
27, 0.20714831352233887, 1.5582966804504395
29+
28, 0.4768686294555664, 3.895355701446533
30+
29, 0.7921774387359619, 7.234867334365845
31+
30, 0.29755592346191406, 1.3978052139282227
32+
31, 0.5860283374786377, 4.7148988246917725
33+
32, 0.9989526271820068, 9.857431173324585
34+
33, 0.31925439834594727, 1.9258503913879395
35+
34, 0.7193968296051025, 6.508935213088989
36+
35, 1.2725257873535156, 11.838297605514526
37+
36, 0.3763289451599121, 3.84029221534729
38+
37, 0.8699963092803955, 8.392340660095215
39+
38, 1.529719352722168, 14.659706830978394
40+
39, 0.44361233711242676, 3.1108617782592773
41+
40, 1.079890251159668, 10.659740686416626
42+
41, 1.8634381294250488, 18.899206399917603
43+
42, 0.531609058380127, 5.301243543624878
44+
43, 1.249814510345459, 12.720186233520508
45+
44, 2.225860357284546, 23.538081407546997

0 commit comments

Comments
 (0)