summaryrefslogtreecommitdiff
path: root/src/research.py
blob: f0374cf9ef946d71e53ffa4fbe906c9a8a4d121f (plain)
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
import os
from plotter import Plotter


class Research:
    def __init__(self, model, name):
        self.model = model
        self.name = name
        self.plotter = Plotter()
        self.path = os.path.join('data', name)
        os.makedirs(self.path, exist_ok=True)

        self.model_path = os.path.join(self.path, 'model.npy')
        self.solution_path = os.path.join(self.path, 'solution.npy')

    def load(self):
        if os.path.exists(self.solution_path):
            self.model.load(self.solution_path)
            return 1

        if os.path.exists(self.model_path):
            self.model.load(self.model_path)

        return 0

    def solve(self, precision=10 ** -4, preview=True, save_plot=False, save_model=False):
        error = 1
        iteration = 0
        while error > precision:
            iteration += 1
            self.model.iterate()
            error = self.model.avg_error()
            print(f'Iteration {iteration}, avg error: {error}')

            if iteration % 10 == 0 or iteration == 1:
                if preview or save_plot:
                    self.plotter.plot(self.model)
                if preview:
                    self.plotter.show()

            if iteration % 50 == 0:
                if save_model:
                    self.model.save(self.model_path)

        self.model.save(self.solution_path)
        self.inspect()

    def inspect(self):
        self.plotter.plot(self.model, density=1)
        while True:
            self.plotter.show()