Commits (2)
__pycache__
*.csv
*.swp
*.json
......@@ -123,8 +123,13 @@ Most exercises consists in adding a single function in an existing module
(or your own module) and use assemble it in the main executable.
1. Implement a simulated annealing.
You can use num_simulated_annealing or bit_simulated_annealing solver
2. Implement an evolutionary algorithm.
You can use num_genetic or bit_genetic
3. Implement an expected run time empirical cumulative density function.
You can call draw_ert.py with -N option for the number of run and -v option to choose a target value
4. Implement a simple design of experiment to determine the best solver.
You can use try_solver to list the different parameter you want to try and get the best parameter
5. Provide a solver for a competition.
For the competion, symply use my num_greedy
......@@ -2,6 +2,7 @@
from os import system, remove, path
import matplotlib.pyplot as plt
import numpy as np
import json
########################################################################
# Interface
......@@ -18,11 +19,12 @@ if __name__=="__main__":
can.add_argument("-v", "--for-value", metavar="NB", type=int,
help="Print ERT with probabilities to obtain this value")
can.add_argument("-save", "--save-proba", action='store_true', default=False,
help="Disable graphical output and save proba")
arg = can.parse_known_args()
the = arg[0]
print(the)
# Minimum checks.
assert(0 < the.nb_run)
......@@ -55,9 +57,9 @@ if __name__=="__main__":
if line[0] == 1:
if len(attempt) > 0:
data.append(attempt)
current_best = float("inf")
current_best = 0
attempt = {}
if line[1] < current_best:
if line[1] > current_best:
attempt[line[0]] = line[1]
current_best = line[1]
......@@ -68,7 +70,7 @@ if __name__=="__main__":
for attempt in data:
good = False
for t,v in attempt.items():
if t < time and v < value:
if t < time and v > value:
good = True
break
if good:
......@@ -78,7 +80,10 @@ if __name__=="__main__":
probability = np.vectorize(probability, excluded=[0])
# Plot
if the.for_value is not None:
if the.save_proba:
with open('proba.json', 'w') as outfile:
json.dump(data, outfile)
elif the.for_value is not None:
temps = np.linspace(0,temps_max,100)
P = probability(data,temps,the.for_value)
plt.plot(temps,P)
......@@ -94,4 +99,4 @@ if __name__=="__main__":
cbar = fig.colorbar(cs)
plt.show()
remove('run.csv')
#remove('run.csv')
......@@ -86,7 +86,7 @@ if __name__=="__main__":
nb_it = the.iters),
make.iter(iters.save,
filename = the.output_file+".csv",
fmt = "{it} ; {val} ; {sol}\n"),
fmt = "{it} ; {val}\n"),
make.iter(iters.log,
fmt="\r{it} {val}"),
make.iter(iters.history,
......
from os import system
import json
import numpy as np
from scipy.integrate import dblquad
import matplotlib.pyplot as plt
Nb_run = 5
solvers = [
'-m num_greedy -n 45 -i 20 -r 0.08 -t 10000 -w 100 -a 0.01',
'-m bit_greedy -n 45 -i 20 -r 0.08 -t 10000 -w 100 -a 0.01'
]
datas = []
for solver in solvers:
print("python3 draw_ert.py --nb-run " + str(Nb_run) + " --save-proba " + solver)
system("python3 draw_ert.py --nb-run " + str(Nb_run) + " --save-proba " + solver)
with open('proba.json') as json_file:
data = json.load(json_file)
datas.append(data)
temps_max = 0
value_max = 0
value_min = np.inf
for data in datas:
for run in data:
m = max([int(k) for k,v in run.items()])
if m > temps_max:
temps_max = m
m = max([v for k,v in run.items()])
if m > value_max:
value_max = m
m = min([v for k,v in run.items()])
if m < value_min:
value_min = m
def probability(data,time,value):
num = 0
for attempt in data:
good = False
for t,v in attempt.items():
if int(t) < time and v > value:
good = True
break
if good:
num += 1
return num/len(data)
probability = np.vectorize(probability, excluded=[0])
m = 0
v = 0
for i in range(len(solvers)):
volume = dblquad(lambda x,y: probability(datas[i],x,y),value_min,value_max,0,temps_max)[0]
print(solvers[i])
print(volume)
if volume > v:
m = i
v = volume
print("Best solveur:")
print(solvers[m])
\ No newline at end of file