agentdb 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +38 -0
- package/LICENSE-APACHE +190 -0
- package/LICENSE-MIT +21 -0
- package/README.md +953 -0
- package/bin/agentdb.js +485 -0
- package/bin/plugin-cli-wrapper.mjs +21 -0
- package/dist/cache/query-cache.d.ts +105 -0
- package/dist/cache/query-cache.d.ts.map +1 -0
- package/dist/cache/query-cache.js +224 -0
- package/dist/cache/query-cache.js.map +1 -0
- package/dist/cache/query-cache.mjs +219 -0
- package/dist/cli/cache/query-cache.d.ts +104 -0
- package/dist/cli/cache/query-cache.js +244 -0
- package/dist/cli/cli/db-commands.d.ts +48 -0
- package/dist/cli/cli/db-commands.js +613 -0
- package/dist/cli/commands.d.ts +7 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +113 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/commands.mjs +104 -0
- package/dist/cli/core/backend-interface.d.ts +70 -0
- package/dist/cli/core/backend-interface.js +15 -0
- package/dist/cli/core/native-backend.d.ts +140 -0
- package/dist/cli/core/native-backend.js +432 -0
- package/dist/cli/core/vector-db.d.ts +126 -0
- package/dist/cli/core/vector-db.js +338 -0
- package/dist/cli/core/wasm-backend.d.ts +95 -0
- package/dist/cli/core/wasm-backend.js +418 -0
- package/dist/cli/db-commands.d.ts +49 -0
- package/dist/cli/db-commands.d.ts.map +1 -0
- package/dist/cli/db-commands.js +533 -0
- package/dist/cli/db-commands.js.map +1 -0
- package/dist/cli/db-commands.mjs +522 -0
- package/dist/cli/generator.d.ts +11 -0
- package/dist/cli/generator.d.ts.map +1 -0
- package/dist/cli/generator.js +567 -0
- package/dist/cli/generator.js.map +1 -0
- package/dist/cli/generator.mjs +527 -0
- package/dist/cli/help.d.ts +18 -0
- package/dist/cli/help.d.ts.map +1 -0
- package/dist/cli/help.js +676 -0
- package/dist/cli/help.js.map +1 -0
- package/dist/cli/help.mjs +667 -0
- package/dist/cli/index/hnsw.d.ts +164 -0
- package/dist/cli/index/hnsw.js +558 -0
- package/dist/cli/plugin-cli.d.ts +7 -0
- package/dist/cli/plugin-cli.d.ts.map +1 -0
- package/dist/cli/plugin-cli.js +295 -0
- package/dist/cli/plugin-cli.js.map +1 -0
- package/dist/cli/plugin-cli.mjs +289 -0
- package/dist/cli/quantization/product-quantization.d.ts +108 -0
- package/dist/cli/quantization/product-quantization.js +350 -0
- package/dist/cli/query/query-builder.d.ts +322 -0
- package/dist/cli/query/query-builder.js +600 -0
- package/dist/cli/templates.d.ts +14 -0
- package/dist/cli/templates.d.ts.map +1 -0
- package/dist/cli/templates.js +182 -0
- package/dist/cli/templates.js.map +1 -0
- package/dist/cli/templates.mjs +176 -0
- package/dist/cli/types/index.d.ts +116 -0
- package/dist/cli/types/index.js +5 -0
- package/dist/cli/types.d.ts +91 -0
- package/dist/cli/types.d.ts.map +1 -0
- package/dist/cli/types.js +6 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli/types.mjs +4 -0
- package/dist/cli/wizard/index.d.ts +6 -0
- package/dist/cli/wizard/index.d.ts.map +1 -0
- package/dist/cli/wizard/index.js +138 -0
- package/dist/cli/wizard/index.js.map +1 -0
- package/dist/cli/wizard/index.mjs +131 -0
- package/dist/cli/wizard/prompts.d.ts +11 -0
- package/dist/cli/wizard/prompts.d.ts.map +1 -0
- package/dist/cli/wizard/prompts.js +482 -0
- package/dist/cli/wizard/prompts.js.map +1 -0
- package/dist/cli/wizard/prompts.mjs +470 -0
- package/dist/cli/wizard/validator.d.ts +13 -0
- package/dist/cli/wizard/validator.d.ts.map +1 -0
- package/dist/cli/wizard/validator.js +234 -0
- package/dist/cli/wizard/validator.js.map +1 -0
- package/dist/cli/wizard/validator.mjs +224 -0
- package/dist/core/backend-interface.d.ts +71 -0
- package/dist/core/backend-interface.d.ts.map +1 -0
- package/dist/core/backend-interface.js +16 -0
- package/dist/core/backend-interface.js.map +1 -0
- package/dist/core/backend-interface.mjs +12 -0
- package/dist/core/native-backend.d.ts +141 -0
- package/dist/core/native-backend.d.ts.map +1 -0
- package/dist/core/native-backend.js +457 -0
- package/dist/core/native-backend.js.map +1 -0
- package/dist/core/native-backend.mjs +449 -0
- package/dist/core/vector-db.d.ts +127 -0
- package/dist/core/vector-db.d.ts.map +1 -0
- package/dist/core/vector-db.js +266 -0
- package/dist/core/vector-db.js.map +1 -0
- package/dist/core/vector-db.mjs +261 -0
- package/dist/core/wasm-backend.d.ts +96 -0
- package/dist/core/wasm-backend.d.ts.map +1 -0
- package/dist/core/wasm-backend.js +393 -0
- package/dist/core/wasm-backend.js.map +1 -0
- package/dist/core/wasm-backend.mjs +385 -0
- package/dist/index/hnsw-optimized.d.ts +75 -0
- package/dist/index/hnsw-optimized.d.ts.map +1 -0
- package/dist/index/hnsw-optimized.js +412 -0
- package/dist/index/hnsw-optimized.js.map +1 -0
- package/dist/index/hnsw-optimized.mjs +407 -0
- package/dist/index/hnsw.d.ts +165 -0
- package/dist/index/hnsw.d.ts.map +1 -0
- package/dist/index/hnsw.js +521 -0
- package/dist/index/hnsw.js.map +1 -0
- package/dist/index/hnsw.mjs +516 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +789 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/mcp-server.mjs +784 -0
- package/dist/plugins/base-plugin.d.ts +114 -0
- package/dist/plugins/base-plugin.d.ts.map +1 -0
- package/dist/plugins/base-plugin.js +313 -0
- package/dist/plugins/base-plugin.js.map +1 -0
- package/dist/plugins/base-plugin.mjs +275 -0
- package/dist/plugins/implementations/active-learning.d.ts +135 -0
- package/dist/plugins/implementations/active-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/active-learning.js +372 -0
- package/dist/plugins/implementations/active-learning.js.map +1 -0
- package/dist/plugins/implementations/active-learning.mjs +367 -0
- package/dist/plugins/implementations/actor-critic.d.ts +64 -0
- package/dist/plugins/implementations/actor-critic.d.ts.map +1 -0
- package/dist/plugins/implementations/actor-critic.js +363 -0
- package/dist/plugins/implementations/actor-critic.js.map +1 -0
- package/dist/plugins/implementations/actor-critic.mjs +358 -0
- package/dist/plugins/implementations/adversarial-training.d.ts +133 -0
- package/dist/plugins/implementations/adversarial-training.d.ts.map +1 -0
- package/dist/plugins/implementations/adversarial-training.js +409 -0
- package/dist/plugins/implementations/adversarial-training.js.map +1 -0
- package/dist/plugins/implementations/adversarial-training.mjs +404 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts +132 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.js +354 -0
- package/dist/plugins/implementations/curriculum-learning.js.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.mjs +349 -0
- package/dist/plugins/implementations/decision-transformer.d.ts +77 -0
- package/dist/plugins/implementations/decision-transformer.d.ts.map +1 -0
- package/dist/plugins/implementations/decision-transformer.js +422 -0
- package/dist/plugins/implementations/decision-transformer.js.map +1 -0
- package/dist/plugins/implementations/decision-transformer.mjs +417 -0
- package/dist/plugins/implementations/federated-learning.d.ts +126 -0
- package/dist/plugins/implementations/federated-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/federated-learning.js +436 -0
- package/dist/plugins/implementations/federated-learning.js.map +1 -0
- package/dist/plugins/implementations/federated-learning.mjs +431 -0
- package/dist/plugins/implementations/index.d.ts +30 -0
- package/dist/plugins/implementations/index.d.ts.map +1 -0
- package/dist/plugins/implementations/index.js +45 -0
- package/dist/plugins/implementations/index.js.map +1 -0
- package/dist/plugins/implementations/index.mjs +31 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts +115 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.js +369 -0
- package/dist/plugins/implementations/multi-task-learning.js.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.mjs +364 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts +148 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.js +379 -0
- package/dist/plugins/implementations/neural-architecture-search.js.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.mjs +374 -0
- package/dist/plugins/implementations/q-learning.d.ts +98 -0
- package/dist/plugins/implementations/q-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/q-learning.js +435 -0
- package/dist/plugins/implementations/q-learning.js.map +1 -0
- package/dist/plugins/implementations/q-learning.mjs +430 -0
- package/dist/plugins/implementations/sarsa.d.ts +103 -0
- package/dist/plugins/implementations/sarsa.d.ts.map +1 -0
- package/dist/plugins/implementations/sarsa.js +347 -0
- package/dist/plugins/implementations/sarsa.js.map +1 -0
- package/dist/plugins/implementations/sarsa.mjs +342 -0
- package/dist/plugins/index.d.ts +107 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +179 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/index.mjs +168 -0
- package/dist/plugins/interface.d.ts +439 -0
- package/dist/plugins/interface.d.ts.map +1 -0
- package/dist/plugins/interface.js +12 -0
- package/dist/plugins/interface.js.map +1 -0
- package/dist/plugins/interface.mjs +10 -0
- package/dist/plugins/learning-plugin.interface.d.ts +257 -0
- package/dist/plugins/learning-plugin.interface.d.ts.map +1 -0
- package/dist/plugins/learning-plugin.interface.js +7 -0
- package/dist/plugins/learning-plugin.interface.js.map +1 -0
- package/dist/plugins/learning-plugin.interface.mjs +5 -0
- package/dist/plugins/plugin-exports.d.ts +71 -0
- package/dist/plugins/plugin-exports.d.ts.map +1 -0
- package/dist/plugins/plugin-exports.js +78 -0
- package/dist/plugins/plugin-exports.js.map +1 -0
- package/dist/plugins/plugin-exports.mjs +69 -0
- package/dist/plugins/registry.d.ts +206 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +365 -0
- package/dist/plugins/registry.js.map +1 -0
- package/dist/plugins/registry.mjs +356 -0
- package/dist/plugins/validator.d.ts +63 -0
- package/dist/plugins/validator.d.ts.map +1 -0
- package/dist/plugins/validator.js +464 -0
- package/dist/plugins/validator.js.map +1 -0
- package/dist/plugins/validator.mjs +458 -0
- package/dist/quantization/binary-quantization.d.ts +104 -0
- package/dist/quantization/binary-quantization.d.ts.map +1 -0
- package/dist/quantization/binary-quantization.js +246 -0
- package/dist/quantization/binary-quantization.js.map +1 -0
- package/dist/quantization/binary-quantization.mjs +240 -0
- package/dist/quantization/optimized-pq.d.ts +138 -0
- package/dist/quantization/optimized-pq.d.ts.map +1 -0
- package/dist/quantization/optimized-pq.js +320 -0
- package/dist/quantization/optimized-pq.js.map +1 -0
- package/dist/quantization/optimized-pq.mjs +313 -0
- package/dist/quantization/product-quantization.d.ts +109 -0
- package/dist/quantization/product-quantization.d.ts.map +1 -0
- package/dist/quantization/product-quantization.js +287 -0
- package/dist/quantization/product-quantization.js.map +1 -0
- package/dist/quantization/product-quantization.mjs +282 -0
- package/dist/quantization/scalar-quantization.d.ts +100 -0
- package/dist/quantization/scalar-quantization.d.ts.map +1 -0
- package/dist/quantization/scalar-quantization.js +324 -0
- package/dist/quantization/scalar-quantization.js.map +1 -0
- package/dist/quantization/scalar-quantization.mjs +319 -0
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +9 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/index.mjs +4 -0
- package/dist/query/query-builder.d.ts +323 -0
- package/dist/query/query-builder.d.ts.map +1 -0
- package/dist/query/query-builder.js +524 -0
- package/dist/query/query-builder.js.map +1 -0
- package/dist/query/query-builder.mjs +519 -0
- package/dist/reasoning/context-synthesizer.d.ts +57 -0
- package/dist/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoning/context-synthesizer.js +224 -0
- package/dist/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoning/context-synthesizer.mjs +219 -0
- package/dist/reasoning/experience-curator.d.ts +66 -0
- package/dist/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoning/experience-curator.js +288 -0
- package/dist/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoning/experience-curator.mjs +283 -0
- package/dist/reasoning/memory-optimizer.d.ts +69 -0
- package/dist/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoning/memory-optimizer.js +331 -0
- package/dist/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoning/memory-optimizer.mjs +326 -0
- package/dist/reasoning/pattern-matcher.d.ts +59 -0
- package/dist/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoning/pattern-matcher.js +229 -0
- package/dist/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoning/pattern-matcher.mjs +224 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +118 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js +477 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js.map +1 -0
- package/dist/reasoningbank/adapter/types.d.ts +113 -0
- package/dist/reasoningbank/adapter/types.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/types.js +9 -0
- package/dist/reasoningbank/adapter/types.js.map +1 -0
- package/dist/reasoningbank/cli/commands.d.ts +16 -0
- package/dist/reasoningbank/cli/commands.d.ts.map +1 -0
- package/dist/reasoningbank/cli/commands.js +272 -0
- package/dist/reasoningbank/cli/commands.js.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts +319 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js +301 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js.map +1 -0
- package/dist/reasoningbank/migration/migrate.d.ts +25 -0
- package/dist/reasoningbank/migration/migrate.d.ts.map +1 -0
- package/dist/reasoningbank/migration/migrate.js +178 -0
- package/dist/reasoningbank/migration/migrate.js.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +37 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js +114 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts +39 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.js +98 -0
- package/dist/reasoningbank/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +44 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js +184 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +40 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js +87 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts +77 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.js +165 -0
- package/dist/reasoningbank/sync/quic-sync.js.map +1 -0
- package/dist/sync/conflict.d.ts +78 -0
- package/dist/sync/conflict.d.ts.map +1 -0
- package/dist/sync/conflict.js +202 -0
- package/dist/sync/conflict.js.map +1 -0
- package/dist/sync/conflict.mjs +196 -0
- package/dist/sync/coordinator.d.ts +111 -0
- package/dist/sync/coordinator.d.ts.map +1 -0
- package/dist/sync/coordinator.js +256 -0
- package/dist/sync/coordinator.js.map +1 -0
- package/dist/sync/coordinator.mjs +250 -0
- package/dist/sync/delta.d.ts +81 -0
- package/dist/sync/delta.d.ts.map +1 -0
- package/dist/sync/delta.js +245 -0
- package/dist/sync/delta.js.map +1 -0
- package/dist/sync/delta.mjs +238 -0
- package/dist/sync/index.d.ts +11 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +22 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/index.mjs +9 -0
- package/dist/sync/quic-sync.d.ts +81 -0
- package/dist/sync/quic-sync.d.ts.map +1 -0
- package/dist/sync/quic-sync.js +329 -0
- package/dist/sync/quic-sync.js.map +1 -0
- package/dist/sync/quic-sync.mjs +323 -0
- package/dist/sync/types.d.ts +168 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/sync/types.js +8 -0
- package/dist/sync/types.js.map +1 -0
- package/dist/sync/types.mjs +6 -0
- package/dist/types/index.d.ts +117 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/wasm-loader.d.ts +32 -0
- package/dist/wasm-loader.d.ts.map +1 -0
- package/dist/wasm-loader.js +75 -0
- package/dist/wasm-loader.js.map +1 -0
- package/dist/wasm-loader.mjs +64 -0
- package/examples/adaptive-learning.ts +284 -0
- package/examples/browser/README.md +732 -0
- package/examples/browser/adaptive-recommendations/index.html +427 -0
- package/examples/browser/collaborative-filtering/index.html +310 -0
- package/examples/browser/continual-learning/index.html +736 -0
- package/examples/browser/experience-replay/index.html +616 -0
- package/examples/browser/index.html +369 -0
- package/examples/browser/meta-learning/index.html +789 -0
- package/examples/browser/neuro-symbolic/index.html +692 -0
- package/examples/browser/pattern-learning/index.html +620 -0
- package/examples/browser/quantum-inspired/index.html +728 -0
- package/examples/browser/rag/index.html +624 -0
- package/examples/browser/swarm-intelligence/index.html +811 -0
- package/examples/browser-basic.html +170 -0
- package/examples/hnsw-example.ts +148 -0
- package/examples/node-basic.js +70 -0
- package/examples/quic-sync-example.ts +310 -0
- package/examples/quick-start.js +68 -0
- package/examples/wasm-example.ts +222 -0
- package/package.json +118 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Active Learning Plugin
|
|
3
|
+
*
|
|
4
|
+
* Implements query-based learning where the agent actively selects
|
|
5
|
+
* the most informative samples to learn from, maximizing data efficiency.
|
|
6
|
+
*
|
|
7
|
+
* Key features:
|
|
8
|
+
* - Uncertainty sampling
|
|
9
|
+
* - Query-by-committee
|
|
10
|
+
* - Expected model change
|
|
11
|
+
* - Diversity-based sampling
|
|
12
|
+
* - Budget-aware querying
|
|
13
|
+
*/
|
|
14
|
+
import { BasePlugin } from '../base-plugin.mjs';
|
|
15
|
+
/**
|
|
16
|
+
* Active Learning Plugin Implementation
|
|
17
|
+
*/
|
|
18
|
+
export class ActiveLearningPlugin extends BasePlugin {
|
|
19
|
+
constructor(config) {
|
|
20
|
+
super();
|
|
21
|
+
this.name = 'active-learning';
|
|
22
|
+
this.version = '1.0.0';
|
|
23
|
+
this.experiences = [];
|
|
24
|
+
this.queryStrategy = 'uncertainty';
|
|
25
|
+
this.labelingBudget = 1000;
|
|
26
|
+
this.queriedSamples = new Set();
|
|
27
|
+
this.unlabeledPool = [];
|
|
28
|
+
// Committee for query-by-committee
|
|
29
|
+
this.committee = [];
|
|
30
|
+
this.committeeSize = 5;
|
|
31
|
+
// Diversity tracking
|
|
32
|
+
this.diversityThreshold = 0.3;
|
|
33
|
+
if (config) {
|
|
34
|
+
this.queryStrategy = config.queryStrategy || 'uncertainty';
|
|
35
|
+
this.labelingBudget = config.labelingBudget !== undefined ? config.labelingBudget : 1000;
|
|
36
|
+
this.committeeSize = config.committeeSize || 5;
|
|
37
|
+
}
|
|
38
|
+
if (this.queryStrategy === 'committee') {
|
|
39
|
+
this.initializeCommittee();
|
|
40
|
+
}
|
|
41
|
+
// Mark as initialized for in-memory operation
|
|
42
|
+
this.initialized = true;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Override to skip initialization check for in-memory operation
|
|
46
|
+
*/
|
|
47
|
+
checkInitialized() {
|
|
48
|
+
// No-op for active learning - operates in-memory
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Override selectAction to provide base implementation
|
|
52
|
+
*/
|
|
53
|
+
async selectAction(state, context) {
|
|
54
|
+
// Simple default action selection for active learning
|
|
55
|
+
return {
|
|
56
|
+
id: '0',
|
|
57
|
+
embedding: Array.isArray(state) ? state : [],
|
|
58
|
+
confidence: 0.5,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Override to store experiences in-memory without vectorDB
|
|
63
|
+
*/
|
|
64
|
+
async storeExperience(experience) {
|
|
65
|
+
this.experiences.push(experience);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Override to retrieve from local experiences
|
|
69
|
+
*/
|
|
70
|
+
async retrieveSimilar(state, k) {
|
|
71
|
+
return this.experiences.slice(0, k).map((exp, idx) => ({
|
|
72
|
+
id: exp.id || `exp-${idx}`,
|
|
73
|
+
embedding: exp.state,
|
|
74
|
+
metadata: exp,
|
|
75
|
+
score: 1.0 - (idx * 0.1), // Mock similarity score
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Initialize committee for query-by-committee
|
|
80
|
+
*/
|
|
81
|
+
initializeCommittee() {
|
|
82
|
+
for (let i = 0; i < this.committeeSize; i++) {
|
|
83
|
+
const weights = new Array(256).fill(0).map(() => Math.random() * 0.1 - 0.05);
|
|
84
|
+
this.committee.push({
|
|
85
|
+
id: `member_${i}`,
|
|
86
|
+
weights,
|
|
87
|
+
predictions: new Map(),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Add unlabeled sample to pool
|
|
93
|
+
*/
|
|
94
|
+
async addUnlabeledSample(state, metadata) {
|
|
95
|
+
this.unlabeledPool.push({
|
|
96
|
+
state,
|
|
97
|
+
uncertainty: 0,
|
|
98
|
+
queryValue: 0,
|
|
99
|
+
metadata,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Calculate uncertainty for a sample
|
|
104
|
+
*/
|
|
105
|
+
async calculateUncertainty(sample) {
|
|
106
|
+
switch (this.queryStrategy) {
|
|
107
|
+
case 'uncertainty':
|
|
108
|
+
return this.uncertaintySampling(sample);
|
|
109
|
+
case 'margin':
|
|
110
|
+
return this.marginSampling(sample);
|
|
111
|
+
case 'entropy':
|
|
112
|
+
return this.entropySampling(sample);
|
|
113
|
+
case 'committee':
|
|
114
|
+
return this.queryByCommittee(sample);
|
|
115
|
+
case 'expected_model_change':
|
|
116
|
+
return this.expectedModelChange(sample);
|
|
117
|
+
case 'diverse':
|
|
118
|
+
return this.diversitySampling(sample);
|
|
119
|
+
default:
|
|
120
|
+
return this.uncertaintySampling(sample);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Uncertainty sampling: Select samples with highest prediction uncertainty
|
|
125
|
+
*/
|
|
126
|
+
async uncertaintySampling(sample) {
|
|
127
|
+
const action = await this.selectAction(sample.state);
|
|
128
|
+
const confidence = action.confidence || 0;
|
|
129
|
+
// Uncertainty is 1 - confidence
|
|
130
|
+
return 1 - confidence;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Margin sampling: Select samples where top two predictions are close
|
|
134
|
+
*/
|
|
135
|
+
async marginSampling(sample) {
|
|
136
|
+
// Get top K predictions
|
|
137
|
+
const predictions = await this.getTopPredictions(sample.state, 2);
|
|
138
|
+
if (predictions.length < 2) {
|
|
139
|
+
return 1.0;
|
|
140
|
+
}
|
|
141
|
+
// Margin is the difference between top two
|
|
142
|
+
const margin = Math.abs(predictions[0] - predictions[1]);
|
|
143
|
+
return 1 - margin; // Smaller margin = higher query value
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Entropy sampling: Select samples with highest prediction entropy
|
|
147
|
+
*/
|
|
148
|
+
async entropySampling(sample) {
|
|
149
|
+
const predictions = await this.getTopPredictions(sample.state, 10);
|
|
150
|
+
// Calculate Shannon entropy
|
|
151
|
+
let entropy = 0;
|
|
152
|
+
const sum = predictions.reduce((a, b) => a + b, 0);
|
|
153
|
+
for (const pred of predictions) {
|
|
154
|
+
const prob = pred / sum;
|
|
155
|
+
if (prob > 0) {
|
|
156
|
+
entropy -= prob * Math.log2(prob);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return entropy;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Query-by-committee: Select samples where committee disagrees most
|
|
163
|
+
*/
|
|
164
|
+
async queryByCommittee(sample) {
|
|
165
|
+
const predictions = [];
|
|
166
|
+
// Get prediction from each committee member
|
|
167
|
+
for (const member of this.committee) {
|
|
168
|
+
const pred = this.predictWithWeights(sample.state, member.weights);
|
|
169
|
+
predictions.push(pred);
|
|
170
|
+
}
|
|
171
|
+
// Calculate variance (disagreement)
|
|
172
|
+
const mean = predictions.reduce((a, b) => a + b, 0) / predictions.length;
|
|
173
|
+
const variance = predictions.reduce((sum, p) => sum + Math.pow(p - mean, 2), 0) / predictions.length;
|
|
174
|
+
return Math.sqrt(variance); // Standard deviation
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Expected model change: Select samples that would change model most
|
|
178
|
+
*/
|
|
179
|
+
async expectedModelChange(sample) {
|
|
180
|
+
// Approximate gradient magnitude
|
|
181
|
+
const epsilon = 0.01;
|
|
182
|
+
const baseline = await this.selectAction(sample.state);
|
|
183
|
+
// Perturb state slightly
|
|
184
|
+
const perturbedState = sample.state.map((x) => x + epsilon);
|
|
185
|
+
const perturbed = await this.selectAction(perturbedState);
|
|
186
|
+
// Gradient magnitude
|
|
187
|
+
const gradMag = Math.abs((perturbed.confidence - baseline.confidence) / epsilon);
|
|
188
|
+
return gradMag;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Diversity sampling: Select diverse batch of samples
|
|
192
|
+
*/
|
|
193
|
+
async diversitySampling(sample) {
|
|
194
|
+
if (this.queriedSamples.size === 0) {
|
|
195
|
+
return 1.0;
|
|
196
|
+
}
|
|
197
|
+
// Calculate distance to already queried samples
|
|
198
|
+
const queriedStates = Array.from(this.queriedSamples).map(id => {
|
|
199
|
+
const s = this.unlabeledPool.find(u => this.hashState(u.state) === id);
|
|
200
|
+
return s?.state || sample.state;
|
|
201
|
+
});
|
|
202
|
+
let minDistance = Infinity;
|
|
203
|
+
for (const qState of queriedStates) {
|
|
204
|
+
const dist = this.euclideanDistance(sample.state, qState);
|
|
205
|
+
minDistance = Math.min(minDistance, dist);
|
|
206
|
+
}
|
|
207
|
+
// Higher diversity value for samples far from queried ones
|
|
208
|
+
return minDistance;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Get top K predictions for a state
|
|
212
|
+
*/
|
|
213
|
+
async getTopPredictions(state, k) {
|
|
214
|
+
// Simplified: return random predictions
|
|
215
|
+
const predictions = [];
|
|
216
|
+
for (let i = 0; i < k; i++) {
|
|
217
|
+
predictions.push(Math.random());
|
|
218
|
+
}
|
|
219
|
+
return predictions.sort((a, b) => b - a);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Predict using specific weights
|
|
223
|
+
*/
|
|
224
|
+
predictWithWeights(state, weights) {
|
|
225
|
+
let activation = 0;
|
|
226
|
+
for (let i = 0; i < Math.min(state.length, weights.length); i++) {
|
|
227
|
+
activation += state[i] * weights[i];
|
|
228
|
+
}
|
|
229
|
+
return Math.tanh(activation);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Hash state for tracking
|
|
233
|
+
*/
|
|
234
|
+
hashState(state) {
|
|
235
|
+
return state.slice(0, 10).join(',');
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Euclidean distance between states
|
|
239
|
+
*/
|
|
240
|
+
euclideanDistance(a, b) {
|
|
241
|
+
let sum = 0;
|
|
242
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
243
|
+
sum += Math.pow(a[i] - b[i], 2);
|
|
244
|
+
}
|
|
245
|
+
return Math.sqrt(sum);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Select next batch of samples to query (label)
|
|
249
|
+
*/
|
|
250
|
+
async selectQueryBatch(batchSize) {
|
|
251
|
+
if (this.unlabeledPool.length === 0 || this.labelingBudget <= 0) {
|
|
252
|
+
return [];
|
|
253
|
+
}
|
|
254
|
+
// Calculate query value for each sample
|
|
255
|
+
for (const sample of this.unlabeledPool) {
|
|
256
|
+
sample.uncertainty = await this.calculateUncertainty(sample);
|
|
257
|
+
sample.queryValue = sample.uncertainty;
|
|
258
|
+
}
|
|
259
|
+
// Sort by query value
|
|
260
|
+
const sorted = [...this.unlabeledPool].sort((a, b) => b.queryValue - a.queryValue);
|
|
261
|
+
// Select top samples
|
|
262
|
+
const selected = sorted.slice(0, Math.min(batchSize, this.labelingBudget));
|
|
263
|
+
// Mark as queried
|
|
264
|
+
selected.forEach(s => {
|
|
265
|
+
this.queriedSamples.add(this.hashState(s.state));
|
|
266
|
+
});
|
|
267
|
+
// Update budget
|
|
268
|
+
this.labelingBudget -= selected.length;
|
|
269
|
+
// Remove from unlabeled pool
|
|
270
|
+
this.unlabeledPool = this.unlabeledPool.filter(s => !selected.includes(s));
|
|
271
|
+
return selected;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Train with active learning
|
|
275
|
+
*/
|
|
276
|
+
async train(options) {
|
|
277
|
+
const startTime = Date.now();
|
|
278
|
+
const epochs = options?.epochs || 10;
|
|
279
|
+
const batchSize = options?.batchSize || 16;
|
|
280
|
+
let totalLoss = 0;
|
|
281
|
+
let experiencesProcessed = 0;
|
|
282
|
+
for (let epoch = 0; epoch < epochs; epoch++) {
|
|
283
|
+
// Select informative batch
|
|
284
|
+
const batch = await this.selectQueryBatch(batchSize);
|
|
285
|
+
if (batch.length === 0) {
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
// Simulate labeling and training on queried samples
|
|
289
|
+
for (const sample of batch) {
|
|
290
|
+
// In real scenario, would query oracle for label
|
|
291
|
+
const simulatedLabel = Math.random();
|
|
292
|
+
const exp = {
|
|
293
|
+
state: sample.state,
|
|
294
|
+
action: { id: '0', embedding: sample.state },
|
|
295
|
+
reward: simulatedLabel,
|
|
296
|
+
nextState: sample.state,
|
|
297
|
+
done: false,
|
|
298
|
+
};
|
|
299
|
+
await this.storeExperience(exp);
|
|
300
|
+
// Compute loss
|
|
301
|
+
const prediction = await this.selectAction(sample.state);
|
|
302
|
+
const loss = Math.pow(prediction.confidence - simulatedLabel, 2);
|
|
303
|
+
totalLoss += loss;
|
|
304
|
+
experiencesProcessed++;
|
|
305
|
+
}
|
|
306
|
+
// Update committee if using query-by-committee
|
|
307
|
+
if (this.queryStrategy === 'committee') {
|
|
308
|
+
await this.updateCommittee(batch);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const duration = Date.now() - startTime;
|
|
312
|
+
return {
|
|
313
|
+
loss: totalLoss / Math.max(1, experiencesProcessed),
|
|
314
|
+
experiencesProcessed,
|
|
315
|
+
duration,
|
|
316
|
+
samplesQueried: this.queriedSamples.size,
|
|
317
|
+
remainingBudget: this.labelingBudget,
|
|
318
|
+
unlabeledPoolSize: this.unlabeledPool.length,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Update committee members
|
|
323
|
+
*/
|
|
324
|
+
async updateCommittee(batch) {
|
|
325
|
+
for (const member of this.committee) {
|
|
326
|
+
// Random subset for diversity
|
|
327
|
+
const subset = batch
|
|
328
|
+
.filter(() => Math.random() > 0.5)
|
|
329
|
+
.map(s => ({
|
|
330
|
+
state: s.state,
|
|
331
|
+
action: { id: '0', embedding: s.state },
|
|
332
|
+
reward: Math.random(),
|
|
333
|
+
nextState: s.state,
|
|
334
|
+
done: false,
|
|
335
|
+
}));
|
|
336
|
+
// Update weights (simplified SGD)
|
|
337
|
+
for (const exp of subset) {
|
|
338
|
+
const pred = this.predictWithWeights(exp.state, member.weights);
|
|
339
|
+
const error = pred - exp.reward;
|
|
340
|
+
for (let i = 0; i < member.weights.length; i++) {
|
|
341
|
+
member.weights[i] -= 0.01 * error * exp.state[i % exp.state.length];
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Get active learning statistics
|
|
348
|
+
*/
|
|
349
|
+
getStats() {
|
|
350
|
+
const avgUncertainty = this.unlabeledPool.length > 0
|
|
351
|
+
? this.unlabeledPool.reduce((sum, s) => sum + s.uncertainty, 0) / this.unlabeledPool.length
|
|
352
|
+
: 0;
|
|
353
|
+
return {
|
|
354
|
+
queriedCount: this.queriedSamples.size,
|
|
355
|
+
remainingBudget: this.labelingBudget,
|
|
356
|
+
unlabeledCount: this.unlabeledPool.length,
|
|
357
|
+
averageUncertainty: avgUncertainty,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Reset querying state
|
|
362
|
+
*/
|
|
363
|
+
resetQuery() {
|
|
364
|
+
this.queriedSamples.clear();
|
|
365
|
+
this.unlabeledPool = [];
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actor-Critic Plugin
|
|
3
|
+
*
|
|
4
|
+
* Implements the Actor-Critic algorithm with policy gradient learning.
|
|
5
|
+
* Combines value-based and policy-based methods:
|
|
6
|
+
* - Actor: Policy network that selects actions
|
|
7
|
+
* - Critic: Value network that evaluates actions
|
|
8
|
+
*
|
|
9
|
+
* Key features:
|
|
10
|
+
* - Policy gradient learning (REINFORCE with baseline)
|
|
11
|
+
* - Advantage estimation (GAE - Generalized Advantage Estimation)
|
|
12
|
+
* - Continuous or discrete action spaces
|
|
13
|
+
* - Natural policy gradients
|
|
14
|
+
*/
|
|
15
|
+
import { BasePlugin } from '../base-plugin';
|
|
16
|
+
import { Action, Context, Experience, TrainOptions, TrainingMetrics } from '../learning-plugin.interface';
|
|
17
|
+
/**
|
|
18
|
+
* Actor-Critic Plugin Implementation
|
|
19
|
+
*/
|
|
20
|
+
export declare class ActorCriticPlugin extends BasePlugin {
|
|
21
|
+
name: string;
|
|
22
|
+
version: string;
|
|
23
|
+
private actor;
|
|
24
|
+
private critic;
|
|
25
|
+
private experienceBuffer;
|
|
26
|
+
private gaeLambda;
|
|
27
|
+
/**
|
|
28
|
+
* Initialize Actor-Critic plugin
|
|
29
|
+
*/
|
|
30
|
+
protected onInitialize(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Select action using actor network
|
|
33
|
+
*
|
|
34
|
+
* @param state - Current state vector
|
|
35
|
+
* @param context - Optional context
|
|
36
|
+
* @returns Selected action
|
|
37
|
+
*/
|
|
38
|
+
selectAction(state: number[], context?: Context): Promise<Action>;
|
|
39
|
+
/**
|
|
40
|
+
* Store experience in buffer
|
|
41
|
+
*/
|
|
42
|
+
protected onStoreExperience(experience: Experience): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Train on completed episode
|
|
45
|
+
*/
|
|
46
|
+
private trainOnEpisode;
|
|
47
|
+
/**
|
|
48
|
+
* Compute Generalized Advantage Estimation (GAE)
|
|
49
|
+
*/
|
|
50
|
+
private computeGAE;
|
|
51
|
+
/**
|
|
52
|
+
* Train the actor-critic networks
|
|
53
|
+
*/
|
|
54
|
+
train(options?: TrainOptions): Promise<TrainingMetrics>;
|
|
55
|
+
/**
|
|
56
|
+
* Save actor and critic networks
|
|
57
|
+
*/
|
|
58
|
+
protected onSave(path: string): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Load actor and critic networks
|
|
61
|
+
*/
|
|
62
|
+
protected onLoad(path: string): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=actor-critic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actor-critic.d.ts","sourceRoot":"","sources":["../../../src/plugins/implementations/actor-critic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EACL,MAAM,EACN,OAAO,EACP,UAAU,EACV,YAAY,EACZ,eAAe,EAChB,MAAM,8BAA8B,CAAC;AAqUtC;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,UAAU;IACxC,IAAI,SAAkB;IACtB,OAAO,SAAW;IAEzB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,MAAM,CAAiB;IAC/B,OAAO,CAAC,gBAAgB,CAAoB;IAC5C,OAAO,CAAC,SAAS,CAAgB;IAEjC;;OAEG;cACa,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAa7C;;;;;;OAMG;IACG,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAevE;;OAEG;cACa,iBAAiB,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAUxE;;OAEG;YACW,cAAc;IAyB5B;;OAEG;IACH,OAAO,CAAC,UAAU;IAmBlB;;OAEG;IACG,KAAK,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC;IA0B7D;;OAEG;cACa,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKnD;;OAEG;cACa,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIpD"}
|