eaf.py 2.83 KB
Newer Older
Julien Lin's avatar
Julien Lin committed
1
2
3
4
5
6
7
8
9
import argparse
from matplotlib import pyplot as plt
import numpy as np
from snp import main as run
import multiprocessing
from joblib import Parallel, delayed

num_cores = multiprocessing.cpu_count()

Julien Lin's avatar
Julien Lin committed
10

Julien Lin's avatar
Julien Lin committed
11
12
13
14
15
def get_args_parse():
    can = argparse.ArgumentParser()

    can.add_argument("--nb-run", type=int, default=10, help="number of runs")

Julien Lin's avatar
Julien Lin committed
16
    can.add_argument(
Julien Lin's avatar
fix eaf    
Julien Lin committed
17
18
19
20
21
        "--quality-threshold",
        type=float,
        default=660,
        nargs="+",
        help="Quality threshold",
Julien Lin's avatar
Julien Lin committed
22
23
24
25
26
    )

    can.add_argument(
        "--not-parallel", type=bool, default=False, help="launch runs sequentialy"
    )
Julien Lin's avatar
Julien Lin committed
27

Julien Lin's avatar
Julien Lin committed
28
29
    can.add_argument("--solver", type=str, default="num_random", help="Solver to use.")

Julien Lin's avatar
fix eaf    
Julien Lin committed
30
31
    can.add_argument("--iters", type=int, default=200)

Julien Lin's avatar
Julien Lin committed
32
33
34
    return can


Julien Lin's avatar
Julien Lin committed
35
def main(eaf_args=None):
Julien Lin's avatar
Julien Lin committed
36
37
38

    can = get_args_parse()

Julien Lin's avatar
Julien Lin committed
39
    the = can.parse_args(eaf_args)
Julien Lin's avatar
Julien Lin committed
40

Julien Lin's avatar
fix eaf    
Julien Lin committed
41
    args = ["--solver", the.solver, "--iters", str(the.iters), "--steady-delta", "500"]
Julien Lin's avatar
Julien Lin committed
42

Julien Lin's avatar
Julien Lin committed
43
44
45
    if the.not_parallel:
        results = []
        for i in range(the.nb_run):
Julien Lin's avatar
Julien Lin committed
46
            results.append(run(args))
Julien Lin's avatar
Julien Lin committed
47
    else:
Julien Lin's avatar
Julien Lin committed
48

Julien Lin's avatar
Julien Lin committed
49
        def f(i):
Julien Lin's avatar
Julien Lin committed
50
51
            return run(args)

Julien Lin's avatar
fix eaf    
Julien Lin committed
52
53
54
        results = Parallel(n_jobs=num_cores - 1)(
            delayed(f)(i) for i in range(the.nb_run)
        )
Julien Lin's avatar
Julien Lin committed
55

Julien Lin's avatar
Julien Lin committed
56
    lens = [len(result[-1]) for result in results]
Julien Lin's avatar
fix eaf    
Julien Lin committed
57
58
59
60
    histories = [
        np.array([val for val, _ in history] + [1] * (the.iters - len(history)))
        for _, _, _, _, _, history in results
    ]
Julien Lin's avatar
Julien Lin committed
61

Julien Lin's avatar
fix eaf    
Julien Lin committed
62
63
64
    histories = np.array([np.maximum.accumulate(history) for history in histories])
    masks = []
    for idx, _ in enumerate(histories):
Julien Lin's avatar
Julien Lin committed
65
66
67
68
69
70
71
72
73
74
75
76
77
        masks.append(
            np.concatenate(
                (
                    np.zeros(
                        lens[idx],
                    ),
                    np.ones(
                        the.iters - lens[idx],
                    ),
                )
            )
        )

Julien Lin's avatar
fix eaf    
Julien Lin committed
78
    histories = np.ma.array(histories, mask=masks).T
Julien Lin's avatar
Julien Lin committed
79

Julien Lin's avatar
fix eaf    
Julien Lin committed
80
    buff = np.empty((1, the.iters))
Julien Lin's avatar
Julien Lin committed
81
    for quality_threshold in the.quality_threshold:
Julien Lin's avatar
fix eaf    
Julien Lin committed
82
        curr_quality = histories >= quality_threshold
Julien Lin's avatar
Julien Lin committed
83
84
        curr_quality = np.sum(curr_quality, axis=1) / the.nb_run
        curr_quality = np.maximum.accumulate(curr_quality)
Julien Lin's avatar
fix eaf    
Julien Lin committed
85
86
87
88
        curr_quality = np.expand_dims(curr_quality, axis=0)
        buff = np.concatenate((buff, curr_quality))

    _, coef, initial, _, _, _ = results[0]
Julien Lin's avatar
Julien Lin committed
89
    t = [i * coef + initial for i in range(histories.shape[0])]
Julien Lin's avatar
Julien Lin committed
90

Julien Lin's avatar
fix eaf    
Julien Lin committed
91
    plt.figure()
Julien Lin's avatar
Julien Lin committed
92

Julien Lin's avatar
Julien Lin committed
93
94
    plt.title(f"{the.solver}, threshold {the.quality_threshold}")
    for i in range(1, buff.shape[0]):
Julien Lin's avatar
Julien Lin committed
95
        plt.step(t, buff[i, ~buff[i].mask][0])
Julien Lin's avatar
Julien Lin committed
96

Julien Lin's avatar
fix eaf    
Julien Lin committed
97
98
99
100
101
102
    plt.legend(
        [
            f"threshold : {the.quality_threshold[i]}"
            for i in range(len(the.quality_threshold))
        ]
    )
Julien Lin's avatar
Julien Lin committed
103
104
105
106

    plt.xlabel("Number of calls to objective function")
    plt.ylabel("Frequency of reaching")
    
Julien Lin's avatar
Julien Lin committed
107
108
109
110
111
    plt.show()


if __name__ == "__main__":
    main()