TraceMaker
C++ API of the engine
Loading...
Searching...
No Matches
knowledge_base.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3// Persistent knowledge base (design doc 06 §4, failure-memory tier T3): what was learned on earlier runs.
4//
5// * per board-feature bucket: how often each router variant was run and how often it produced the best result
6// (Thompson-sampling bandit over portfolio variants);
7// * per board (content hash): connections that failed before, so a re-run routes them first.
8//
9// SQLite file, default ~/.local/share/tracemaker/kb.sqlite. Every call is best-effort: a missing or locked
10// database never stops routing.
11#include <cstdint>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include "model/board.hpp"
17
18namespace tmk::learn {
19
21 std::uint64_t hash = 0; // content hash of the board file
22 int layers = 0, nets = 0, pads = 0, connections = 0;
23 double area_cm2 = 0;
24 std::string bucket() const; // coarse class used to share statistics between similar boards
25};
26
27BoardFeatures features_of(const model::Board& b, const std::string& file_text, int connections);
28
30 std::string net, pad_a, pad_b; // "REF.NUM"
31 int fails = 0;
32};
33
35 public:
36 explicit KnowledgeBase(std::string path = default_path());
38 bool ok() const;
39 static std::string default_path();
40
41 // Bandit: returns `k` variant indices out of `n`, the historically best first, the rest by Thompson sampling
42 // on Beta(1 + wins, 1 + plays - wins) for this feature bucket (seeded, deterministic).
43 std::vector<int> choose_variants(const BoardFeatures& f, int n, int k, std::uint64_t seed) const;
44 void record_run(const BoardFeatures& f, const std::string& board_name, const std::vector<int>& variants_run, int best_variant,
45 int routed, int connections, double seconds);
46
47 std::vector<FailedConnection> failed_connections(std::uint64_t board_hash) const;
48 void record_failures(std::uint64_t board_hash, const std::vector<FailedConnection>& failed);
49
50 private:
51 struct Impl;
52 std::unique_ptr<Impl> impl_;
53};
54
55} // namespace tmk::learn
The board model (design doc 02 §3): every object TraceMaker reads from a .kicad_pcb,...
static std::string default_path()
KnowledgeBase(std::string path=default_path())
std::vector< FailedConnection > failed_connections(std::uint64_t board_hash) const
void record_run(const BoardFeatures &f, const std::string &board_name, const std::vector< int > &variants_run, int best_variant, int routed, int connections, double seconds)
void record_failures(std::uint64_t board_hash, const std::vector< FailedConnection > &failed)
std::vector< int > choose_variants(const BoardFeatures &f, int n, int k, std::uint64_t seed) const
Bandit: returns k variant indices out of n, the historically best first, the rest by Thompson samplin...
BoardFeatures features_of(const model::Board &b, const std::string &file_text, int connections)
std::uint64_t hash
content hash of the board file
std::string bucket() const
coarse class used to share statistics between similar boards