Download src/graph.cpp from goethe0101/neurographdb: direct link, hf CLI and curl.
- Browser
- Download file 7.27 kB
-
https://huggingface.co/datasets/goethe0101/neurographdb/resolve/main/src/graph.cpp
- Command line
-
hf download hf://datasets/goethe0101/neurographdb/src/graph.cpp
-
curl -L -o graph.cpp https://huggingface.co/datasets/goethe0101/neurographdb/resolve/main/src/graph.cpp
7.27 kB
| void Graph::add_edge(int32_t src, int32_t dst, float w) { | |
| add_edge_typed(src, dst, w, -1, 1); | |
| } | |
| void Graph::add_edge_typed(int32_t src, int32_t dst, float w, int16_t type, int8_t pol) { | |
| if (src < 0 || src >= n_ || dst < 0 || dst >= n_ || src == dst) return; | |
| auto& e = adj_[src][dst]; | |
| /* ๊ฐ์ ์์ด ์ฌ๋ฌ ๋ฒ ์ค๋ฉด ๊ฐ์ฅ ๊ฐํ ๊ฒ์ ๋จ๊ธด๋ค. | |
| * ํ์ ์ ๊ทธ๋ ํจ๊ป ๊ฐฑ์ ํ๋ค โ ๊ฐ์ฅ ๊ฐํ ๊ทผ๊ฑฐ์ ์ ์ด๋ฅผ ๋ํ๋ก ์ด๋ค. */ | |
| if (w > e.w) { e.w = w; e.type = type; e.pol = pol; } | |
| } | |
| int64_t Graph::n_edges() const { | |
| int64_t c = 0; | |
| for (auto& m : adj_) c += static_cast<int64_t>(m.size()); | |
| return c; | |
| } | |
| float Graph::edge_weight(int32_t src, int32_t dst) const { | |
| if (src < 0 || src >= n_) return 0.0f; | |
| auto it = adj_[src].find(dst); | |
| return it == adj_[src].end() ? 0.0f : it->second.w; | |
| } | |
| std::vector<ScoredDoc> Graph::spread(const std::vector<int32_t>& seeds, | |
| const std::vector<float>& seed_acts, | |
| int max_depth, | |
| float depth_decay, | |
| float min_act, | |
| const std::vector<int16_t>& query_types, | |
| float alpha, | |
| float beta) const { | |
| std::vector<float> act(n_, 0.0f); | |
| std::vector<int> depth(n_, -1); | |
| const bool typed = !query_types.empty(); | |
| /* ๋๋น ์ฐ์ . ๊ฐ์ ๋ ธ๋์ ์ฌ๋ฌ ๊ฒฝ๋ก๋ก ๋๋ฌํ๋ฉด ๊ฐ์ฅ ๋์ ํ์ฑ์ ๋จ๊ธด๋ค. */ | |
| std::queue<int32_t> frontier; | |
| for (size_t i = 0; i < seeds.size(); i++) { | |
| const int32_t s = seeds[i]; | |
| if (s < 0 || s >= n_) continue; | |
| const float a = (i < seed_acts.size()) ? seed_acts[i] : 1.0f; | |
| if (a > act[s]) { act[s] = a; depth[s] = 0; frontier.push(s); } | |
| } | |
| while (!frontier.empty()) { | |
| const int32_t u = frontier.front(); | |
| frontier.pop(); | |
| if (depth[u] >= max_depth) continue; | |
| for (auto& [v, e] : adj_[u]) { | |
| /* ์ ์ด ํธํ โ ๋ฌดํ์ (-1)์ ์ธ์ ๋ ํต๊ณผํ๋ค. ์ง์ ์ ์ด๊ฐ ์์ด๋ ํต๊ณผํ๋ค. | |
| * ๊ทธ๋์ ๋ช ์ ์ธต์ ๋๋ฉด ์๋ ๊ณฑ์ ์ด ์ ๋ถ 1.0์ด ๋์ด ์์ ์ฝ๋์ ๊ฐ์์ง๋ค. */ | |
| float f = 1.0f; | |
| if (typed && e.type >= 0 && | |
| std::find(query_types.begin(), query_types.end(), e.type) == query_types.end()) | |
| f = alpha; | |
| if (e.pol < 0) f *= beta; | |
| const float cand = act[u] * e.w * f * depth_decay; | |
| if (cand < min_act || cand <= act[v]) continue; | |
| act[v] = cand; | |
| depth[v] = depth[u] + 1; | |
| frontier.push(v); | |
| } | |
| } | |
| std::vector<ScoredDoc> out; | |
| out.reserve(64); | |
| for (int32_t i = 0; i < n_; i++) | |
| if (act[i] > 0.0f) out.push_back({i, act[i]}); | |
| std::sort(out.begin(), out.end(), | |
| [](const ScoredDoc& a, const ScoredDoc& b){ return a.score > b.score; }); | |
| return out; | |
| } | |
| namespace { | |
| /* ๋น ์์ ํ ๊ฒฝ๋ก. ๊น์ด๊ฐ 3์ด๋ผ ๋ ธ๋๋ฅผ ๊ทธ๋๋ก ๋ค๊ณ ๋ค๋ ๋ ์ธ๋ค. | |
| * ๋ฐฉ๋ฌธ ๋ ธ๋๋ฅผ ๋ด๋ ์ด์ ๋ ์ํ์ ๋ง๊ธฐ ์ํด์๋ค โ ๊ฐ์ ๋ฌธ๋จ์ ๋ ๋ฒ ์ง๋๋ ๊ฒฝ๋ก๋ | |
| * ์ ์ ๋ณด๋ฅผ ์ฃผ์ง ์์ผ๋ฉด์ ํ๊ท ์ ์๋ง ์ฌ๋ฆฐ๋ค. */ | |
| struct Path { | |
| int32_t last; | |
| float sum; /* ๊ฒฝ๋ก ์ qsim ํฉ */ | |
| float worst; /* ๊ฒฝ๋ก ์ ์ต์ qsim */ | |
| float tail; /* ๋์ฐฉ ๋ ธ๋ qsim */ | |
| int len; | |
| std::vector<int32_t> nodes; | |
| float score(int mode) const { | |
| if (mode == 1) return worst; | |
| if (mode == 2) return tail; | |
| return sum / static_cast<float>(len); | |
| } | |
| }; | |
| } // namespace | |
| std::vector<ScoredDoc> Graph::beam_search(const std::vector<int32_t>& seeds, | |
| const std::vector<float>& qsim, | |
| int max_depth, | |
| int beam_width, | |
| float min_sim, | |
| int score_mode) const { | |
| std::vector<float> best(n_, -1.0f); | |
| for (int32_t s : seeds) { | |
| if (s < 0 || s >= n_ || s >= static_cast<int32_t>(qsim.size())) continue; | |
| /* seed๋ง๋ค ๋ ๋ฆฝ์ ์ผ๋ก ๋น์ ๊ตด๋ฆฐ๋ค. ์ฌ๊ธฐ์ ํฉ์น๋ฉด ํฌ์ seed๊ฐ ๋ฌด์๋ฏธํด์ง๋ค. */ | |
| std::vector<Path> beam; | |
| beam.push_back({s, qsim[s], qsim[s], qsim[s], 1, {s}}); | |
| if (qsim[s] > best[s]) best[s] = qsim[s]; | |
| for (int d = 0; d < max_depth && !beam.empty(); d++) { | |
| std::vector<Path> cand; | |
| cand.reserve(beam.size() * 8); | |
| for (const Path& p : beam) { | |
| auto it = adj_.begin() + p.last; | |
| for (const auto& [v, e] : *it) { | |
| if (qsim[v] < min_sim) continue; | |
| /* ์ํ ์ฐจ๋จ */ | |
| if (std::find(p.nodes.begin(), p.nodes.end(), v) != p.nodes.end()) | |
| continue; | |
| Path q; | |
| q.last = v; | |
| q.sum = p.sum + qsim[v]; | |
| q.worst = std::min(p.worst, qsim[v]); | |
| q.tail = qsim[v]; | |
| q.len = p.len + 1; | |
| q.nodes = p.nodes; | |
| q.nodes.push_back(v); | |
| cand.push_back(std::move(q)); | |
| } | |
| } | |
| if (cand.empty()) break; | |
| const size_t keep = std::min<size_t>(cand.size(), | |
| std::max(1, beam_width)); | |
| std::partial_sort(cand.begin(), cand.begin() + keep, cand.end(), | |
| [score_mode](const Path& a, const Path& b) { | |
| return a.score(score_mode) > b.score(score_mode); | |
| }); | |
| cand.resize(keep); | |
| for (const Path& p : cand) { | |
| const float sc = p.score(score_mode); | |
| if (sc > best[p.last]) best[p.last] = sc; | |
| } | |
| beam.swap(cand); | |
| } | |
| } | |
| std::vector<ScoredDoc> out; | |
| out.reserve(64); | |
| for (int32_t i = 0; i < n_; i++) | |
| if (best[i] >= 0.0f) out.push_back({i, best[i]}); | |
| std::sort(out.begin(), out.end(), | |
| [](const ScoredDoc& a, const ScoredDoc& b) { return a.score > b.score; }); | |
| return out; | |
| } | |
| void Graph::reinforce(const std::vector<int32_t>& coactive, float delta, float w_max) { | |
| for (size_t i = 0; i < coactive.size(); i++) { | |
| for (size_t j = i + 1; j < coactive.size(); j++) { | |
| const int32_t a = coactive[i], b = coactive[j]; | |
| if (a < 0 || a >= n_ || b < 0 || b >= n_ || a == b) continue; | |
| /* ์๋ฐฉํฅ์ผ๋ก ๊ฐํํ๋ค. ์ฐ์์ ๋ฐฉํฅ์ด ์๋ค. */ | |
| EdgeAttr& ab = adj_[a][b]; | |
| EdgeAttr& ba = adj_[b][a]; | |
| ab.w = std::min(w_max, ab.w + delta); | |
| ba.w = std::min(w_max, ba.w + delta); | |
| } | |
| } | |
| } | |
| void Graph::decay(float lambda) { | |
| const float f = 1.0f - lambda; | |
| for (auto& m : adj_) | |
| for (auto& [v, e] : m) e.w *= f; | |
| } | |