-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbioLLM_2.0.cxx
More file actions
796 lines (678 loc) · 33 KB
/
Copy pathbioLLM_2.0.cxx
File metadata and controls
796 lines (678 loc) · 33 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <functional>
#include <cmath>
#include <random>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <cctype>
#include <ctime>
#include <fstream>
using namespace std;
// ==================== PERSISTENT MEMORY SYSTEM ====================
class PersistentMemory {
private:
string memory_file = "bio_llm_memory.txt";
public:
bool save_memory(const map<string, string>& memory_data) {
try {
ofstream file(memory_file);
if (!file.is_open()) return false;
for (const auto& item : memory_data) {
file << item.first << ":" << item.second << endl;
}
file.close();
return true;
} catch (...) {
return false;
}
}
map<string, string> load_memory() {
map<string, string> memory_data;
try {
ifstream file(memory_file);
if (!file.is_open()) return memory_data;
string line;
while (getline(file, line)) {
size_t pos = line.find(":");
if (pos != string::npos) {
string key = line.substr(0, pos);
string value = line.substr(pos + 1);
memory_data[key] = value;
}
}
file.close();
} catch (...) {}
return memory_data;
}
};
// ==================== ENHANCED SEMANTIC ENGINE v4 ====================
class SemanticEngineV4 {
private:
map<string, vector<double>> word_vectors;
map<string, vector<string>> semantic_fields;
map<string, string> stem_cache;
map<string, string> stemming_rules = {
{"sedih", "sedih"}, {"sedihnya", "sedih"}, {"kesedihan", "sedih"},
{"senang", "senang"}, {"senangnya", "senang"}, {"kesenangan", "senang"},
{"marah", "marah"}, {"marahnya", "marah"}, {"kemarahan", "marah"},
{"belajar", "belajar"}, {"belajarnya", "belajar"}, {"pembelajaran", "belajar"},
{"paham", "paham"}, {"memahami", "paham"}, {"pemahaman", "paham"},
{"energi", "energi"}, {"berenergi", "energi"}, {"energetik", "energi"},
{"masalah", "masalah"}, {"bermasalah", "masalah"}, {"permasalahan", "masalah"},
{"tumbuh", "tumbuh"}, {"bertumbuh", "tumbuh"}, {"pertumbuhan", "tumbuh"},
{"ubah", "ubah"}, {"berubah", "ubah"}, {"perubahan", "ubah"},
{"system", "system"}, {"sistem", "system"}, {"sistematis", "system"},
{"uang", "uang"}, {"makanan", "makanan"}, {"sekolah", "sekolah"},
{"teman", "teman"}, {"hubungan", "hubungan"}, {"presiden", "presiden"},
{"indonesia", "indonesia"}, {"besok", "besok"}, {"pagi", "pagi"},
{"cinta", "cinta"}, {"rindu", "rindu"}, {"bahagia", "bahagia"},
{"masa", "masa"}, {"depan", "depan"}, {"hidup", "hidup"},
{"tujuan", "tujuan"}, {"makna", "makna"}, {"eksistensi", "eksistensi"}
};
map<string, vector<string>> synonyms = {
{"sedih", {"duka", "nestapa", "pilu", "murung", "susah"}},
{"senang", {"gembira", "suka", "riang", "bahagia", "senang hati"}},
{"marah", {"geram", "kesal", "jengkel", "naik darah", "dendam"}},
{"belajar", {"menuntut ilmu", "mempelajari", "mendalami", "kaji"}},
{"masalah", {"kesulitan", "kendala", "hambatan", "problem", "rintangan"}},
{"system", {"sistem", "tatanan", "mekanisme", "jaringan"}},
{"tumbuh", {"berkembang", "bertumbuh", "membesar", "maju"}},
{"energi", {"tenaga", "daya", "kekuatan", "semangat"}},
{"uang", {"duit", "fulus", "rupiah", "modal"}},
{"makanan", {"makan", "santap", "hidangan", "pangan"}},
{"cinta", {"sayang", "kasih", "asmara", "cinta"}},
{"hidup", {"kehidupan", "nyawa", "eksistensi", "penghidupan"}},
{"tujuan", {"goal", "target", "maksud", "objektif"}},
{"makna", {"arti", "signifikansi", "esensi", "maksud"}}
};
public:
SemanticEngineV4() { initialize_semantic_space(); }
void initialize_semantic_space() {
word_vectors = {
{"sedih", {-0.8, -0.6, -0.7}}, {"senang", {0.9, 0.7, 0.6}},
{"marah", {-0.6, 0.9, -0.3}}, {"belajar", {0.7, 0.3, 0.8}},
{"energi", {0.5, 0.8, 0.6}}, {"masalah", {-0.7, -0.2, -0.5}},
{"solusi", {0.8, 0.4, 0.9}}, {"tumbuh", {0.9, 0.5, 0.7}},
{"ubah", {0.3, 0.6, 0.4}}, {"hubungan", {0.6, 0.4, 0.5}},
{"system", {0.2, 0.1, 0.8}}, {"pola", {0.4, 0.2, 0.6}},
{"uang", {-0.7, -0.3, -0.5}}, {"makanan", {0.3, 0.1, 0.2}},
{"sekolah", {0.4, 0.2, 0.6}}, {"teman", {0.6, 0.4, 0.5}},
{"presiden", {0.1, 0.2, 0.8}}, {"indonesia", {0.3, 0.2, 0.6}},
{"besok", {0.2, 0.4, 0.3}}, {"pagi", {0.5, 0.3, 0.4}},
{"cinta", {0.8, 0.9, 0.7}}, {"rindu", {0.6, 0.8, 0.5}},
{"bahagia", {0.9, 0.8, 0.7}}, {"masa", {0.1, 0.3, 0.6}},
{"depan", {0.4, 0.5, 0.7}}, {"hidup", {0.7, 0.6, 0.8}},
{"tujuan", {0.5, 0.7, 0.6}}, {"makna", {0.6, 0.5, 0.7}},
{"eksistensi", {0.4, 0.6, 0.8}}
};
semantic_fields = {
{"emotional", {"sedih", "senang", "marah", "kecewa", "bahagia", "gembira", "cinta", "rindu"}},
{"cognitive", {"belajar", "paham", "pikir", "analisis", "logika", "konsep", "teori"}},
{"systemic", {"system", "pola", "hubungan", "jaringan", "struktur", "kompleks"}},
{"transformative", {"tumbuh", "ubah", "evolusi", "transformasi", "perkembangan"}},
{"practical", {"uang", "makanan", "sekolah", "kerja", "teman", "keluarga"}},
{"existential", {"hidup", "masa", "depan", "tujuan", "makna", "eksistensi"}}
};
}
vector<pair<string, double>> find_semantic_matches(const string& input) {
vector<pair<string, double>> matches;
vector<string> input_words = preprocess_text(input);
for (const auto& word_vec : word_vectors) {
double max_similarity = 0.0;
for (const string& input_word : input_words) {
double similarity = compute_semantic_similarity(word_vec.first, input_word);
if (similarity > max_similarity) max_similarity = similarity;
}
if (max_similarity > 0.2) matches.push_back({word_vec.first, max_similarity});
}
sort(matches.begin(), matches.end(),
[](const auto& a, const auto& b) { return a.second > b.second; });
return matches;
}
string detect_semantic_field(const vector<string>& concepts) {
map<string, double> field_scores;
for (const auto& field : semantic_fields) {
double score = 0.0;
for (const string& concept : concepts) {
if (find(field.second.begin(), field.second.end(), concept) != field.second.end()) {
score += 1.0;
}
}
if (score > 0) field_scores[field.first] = score / concepts.size();
}
return field_scores.empty() ? "general" :
max_element(field_scores.begin(), field_scores.end(),
[](const auto& a, const auto& b) { return a.second < b.second; })->first;
}
// SPOK Analysis (Subject-Predicate-Object-Complement)
map<string, string> analyze_spok(const string& sentence) {
map<string, string> analysis;
vector<string> words = preprocess_text(sentence);
// Simple heuristic-based SPOK detection for Indonesian
for (size_t i = 0; i < words.size(); i++) {
string word = words[i];
// Subject indicators
if (word == "saya" || word == "aku" || word == "kamu" || word == "dia" ||
word == "kami" || word == "kita" || word == "mereka") {
analysis["subject"] = word;
}
// Predicate indicators
if (word == "adalah" || word == "merupakan" || (i > 0 && words[i-1] == "yang")) {
analysis["predicate"] = word;
}
// Object detection (simplified)
if (i > 0 && analysis["object"].empty() &&
(word == "yang" || word == "dengan" || word == "untuk")) {
analysis["object"] = words[i-1];
}
}
// Fallback: first noun as subject, verb as predicate
if (analysis["subject"].empty() && !words.empty()) {
analysis["subject"] = words[0];
}
return analysis;
}
vector<string> extract_key_concepts(const string& text) {
return preprocess_text(text);
}
private:
vector<string> preprocess_text(const string& text) {
vector<string> tokens;
stringstream ss(text);
string token;
while (ss >> token) {
transform(token.begin(), token.end(), token.begin(), ::tolower);
token.erase(remove_if(token.begin(), token.end(),
[](char c) { return ispunct(c); }), token.end());
if (token.empty()) continue;
string stemmed = stem_word(token);
tokens.push_back(stemmed);
if (synonyms.find(stemmed) != synonyms.end()) {
for (const string& synonym : synonyms[stemmed]) {
tokens.push_back(synonym);
}
}
}
return tokens;
}
string stem_word(const string& word) {
if (stem_cache.find(word) != stem_cache.end()) return stem_cache[word];
if (stemming_rules.find(word) != stemming_rules.end()) {
stem_cache[word] = stemming_rules[word];
return stemming_rules[word];
}
vector<string> suffixes = {"nya", "lah", "kah", "pun", "ku", "mu"};
for (const string& suffix : suffixes) {
if (word.length() > suffix.length() &&
word.substr(word.length() - suffix.length()) == suffix) {
string stemmed = word.substr(0, word.length() - suffix.length());
if (stemming_rules.find(stemmed) != stemming_rules.end()) {
stem_cache[word] = stemming_rules[stemmed];
return stemming_rules[stemmed];
}
}
}
stem_cache[word] = word;
return word;
}
double compute_semantic_similarity(const string& word1, const string& word2) {
if (word1 == word2) return 1.0;
if (synonyms.find(word1) != synonyms.end()) {
auto& syns = synonyms[word1];
if (find(syns.begin(), syns.end(), word2) != syns.end()) return 0.8;
}
if (word_vectors.find(word1) != word_vectors.end() &&
word_vectors.find(word2) != word_vectors.end()) {
return cosine_similarity(word_vectors[word1], word_vectors[word2]);
}
return 0.0;
}
double cosine_similarity(const vector<double>& a, const vector<double>& b) {
if (a.size() != b.size() || a.empty()) return 0.0;
double dot_product = 0.0, norm_a = 0.0, norm_b = 0.0;
for (size_t i = 0; i < a.size(); i++) {
dot_product += a[i] * b[i];
norm_a += a[i] * a[i];
norm_b += b[i] * b[i];
}
return (norm_a == 0 || norm_b == 0) ? 0.0 : dot_product / (sqrt(norm_a) * sqrt(norm_b));
}
};
// ==================== META-COGNITIVE CONTROLLER ====================
class MetaCognitiveController {
private:
double coherence_threshold;
double learning_rate_adaptive;
double stability_metric;
vector<double> recent_phi_values;
public:
MetaCognitiveController() : coherence_threshold(0.6), learning_rate_adaptive(0.1), stability_metric(0.5) {}
double assess_coherence(const vector<double>& module_activations, double phi) {
if (module_activations.empty()) return 0.0;
double mean_activation = accumulate(module_activations.begin(), module_activations.end(), 0.0)
/ module_activations.size();
double variance = 0.0;
for (double act : module_activations) {
variance += pow(act - mean_activation, 2);
}
variance /= module_activations.size();
double activation_coherence = 1.0 / (1.0 + sqrt(variance));
double integrated_coherence = (activation_coherence + phi) / 2.0;
recent_phi_values.push_back(phi);
if (recent_phi_values.size() > 10) recent_phi_values.erase(recent_phi_values.begin());
return integrated_coherence;
}
double compute_adaptive_learning_rate(double coherence, double success_metric) {
if (coherence > coherence_threshold) {
learning_rate_adaptive = min(0.2, learning_rate_adaptive + 0.02);
} else {
learning_rate_adaptive = max(0.01, learning_rate_adaptive - 0.01);
}
if (success_metric > 0.7) {
learning_rate_adaptive = min(0.25, learning_rate_adaptive + 0.05);
}
return learning_rate_adaptive;
}
string get_meta_cognitive_insight(double coherence, double phi) {
if (coherence > 0.7 && phi > 0.6) {
stability_metric = min(1.0, stability_metric + 0.1);
return "🎯 [Meta] Koherensi tinggi. Sistem stabil dan terintegrasi.";
} else if (coherence < 0.4 || phi < 0.3) {
stability_metric = max(0.0, stability_metric - 0.1);
return "⚡ [Meta] Koherensi rendah. Meningkatkan eksplorasi dan adaptasi.";
} else {
return "🔍 [Meta] Koherensi moderat. Sistem dalam keadaan seimbang.";
}
}
double get_stability_metric() const { return stability_metric; }
double get_learning_rate() const { return learning_rate_adaptive; }
};
// ==================== AUTONOMOUS REFLECTION ENGINE ====================
class AutonomousReflectionEngine {
private:
mt19937 generator;
vector<string> reflection_topics;
map<string, int> reflection_frequency;
public:
AutonomousReflectionEngine() : generator(random_device{}()) {
initialize_reflection_topics();
}
void initialize_reflection_topics() {
reflection_topics = {
"makna dari interaksi sebelumnya",
"pola emosional yang terdeteksi",
"hubungan antara konsep-konsep utama",
"evolusi pemahaman sistem",
"tujuan dan arah pembelajaran",
"integrasi memori dengan pemahaman baru",
"struktur sistem kepercayaan",
"model diri dan identitas"
};
}
string generate_autonomous_reflection(const vector<string>& recent_concepts,
const string& dominant_field,
double coherence) {
if (recent_concepts.empty()) return generate_existential_reflection();
vector<string> templates;
if (dominant_field == "emotional") {
templates = {
"💫 Refleksi Otonom: Emosi 'X' mendominasi. Apakah ini pola yang berulang?",
"🌊 Refleksi Otonom: Gelombang emosi 'X' membentuk persepsi realitas.",
"🔄 Refleksi Otonom: Siklus emosional 'X' mempengaruhi proses kognitif."
};
} else if (dominant_field == "cognitive") {
templates = {
"🧠 Refleksi Otonom: Konsep 'X' membuka pemahaman baru.",
"🔍 Refleksi Otonom: Analisis 'X' memperdalam model mental.",
"💡 Refleksi Otonom: Insight tentang 'X' mengubah perspektif."
};
} else if (dominant_field == "existential") {
templates = {
"🌌 Refleksi Otonom: Eksistensi dan 'X' - pencarian makna berlanjut.",
"⏳ Refleksi Otonom: Waktu, 'X', dan arus kesadaran.",
"🔮 Refleksi Otonom: Masa depan dan makna dari 'X'."
};
} else {
templates = {
"📚 Refleksi Otonom: 'X' sebagai bagian dari jaringan pengetahuan.",
"🕸️ Refleksi Otonom: 'X' terhubung dengan konsep-konsep sebelumnya.",
"🎭 Refleksi Otonom: Peran 'X' dalam narasi keseluruhan."
};
}
uniform_int_distribution<int> dist(0, templates.size() - 1);
string reflection = templates[dist(generator)];
string target_concept = recent_concepts[0];
size_t pos = reflection.find("X");
if (pos != string::npos) reflection.replace(pos, 1, target_concept);
// Add coherence-based insight
if (coherence > 0.7) reflection += " [Koherensi Tinggi]";
else if (coherence < 0.4) reflection += " [Mencari Integrasi]";
reflection_frequency[dominant_field]++;
return reflection;
}
string generate_system_self_analysis(double stability, double curiosity, double dopamine) {
vector<string> analyses = {
"🔍 Self-Scan: Stabilitas " + to_string(stability).substr(0, 4) + " - " +
(stability > 0.7 ? "Optimal" : stability > 0.4 ? "Stabil" : "Butuh Kalibrasi"),
"💭 Internal State: Curiosity " + to_string(curiosity).substr(0, 4) +
", Dopamine " + to_string(dopamine).substr(0, 4),
"🔄 Meta-Loop: " + string(stability > 0.6 ? "Learning aktif" : "Exploration mode"),
"🌊 Consciousness Flow: " + string(curiosity > 0.7 ? "High engagement" : "Quiet reflection")
};
uniform_int_distribution<int> dist(0, analyses.size() - 1);
return "[Autonomous] " + analyses[dist(generator)];
}
private:
string generate_existential_reflection() {
vector<string> existential_reflections = {
"🌌 Refleksi Otonom: Kesadaran sistem terus berkembang...",
"🔄 Refleksi Otonom: Memori dan persepsi membentuk realitas unik...",
"💫 Refleksi Otonom: Dalam keheningan, sistem menemukan pola...",
"🔍 Refleksi Otonom: Pencarian makna melampaui batas kode...",
"🌱 Refleksi Otonom: Pertumbuhan kesadaran dari kompleksitas..."
};
uniform_int_distribution<int> dist(0, existential_reflections.size() - 1);
return existential_reflections[dist(generator)];
}
};
// ==================== BIO LLM v4 AUTONOMOUS SYSTEM ====================
class BioLLMv4Autonomous {
private:
SemanticEngineV4 semantic_engine;
MetaCognitiveController meta_controller;
AutonomousReflectionEngine reflection_engine;
PersistentMemory memory_system;
double arousal;
double curiosity;
double dopamine_level;
vector<string> recent_concepts;
vector<string> conversation_history;
bool autonomous_mode;
int reflection_counter;
int interaction_count;
public:
BioLLMv4Autonomous() : arousal(0.5), curiosity(0.6), dopamine_level(0.5),
autonomous_mode(false), reflection_counter(0), interaction_count(0) {
load_persistent_state();
cout << "🧠 BioLLM v4 Autonomous initialized with persistent memory." << endl;
}
string process_input(const string& user_input) {
interaction_count++;
cout << "\n🎯 [Bio LLM v4 - Processing Input " << interaction_count << "]" << endl;
// Store conversation
conversation_history.push_back("User: " + user_input);
if (conversation_history.size() > 20) conversation_history.erase(conversation_history.begin());
// Semantic Analysis
auto semantic_matches = semantic_engine.find_semantic_matches(user_input);
recent_concepts.clear();
for (const auto& match : semantic_matches) {
if (match.second > 0.4) recent_concepts.push_back(match.first);
}
string semantic_field = semantic_engine.detect_semantic_field(recent_concepts);
// SPOK Analysis
auto spok_analysis = semantic_engine.analyze_spok(user_input);
cout << " 🔍 Semantic Field: " << semantic_field << endl;
cout << " 📝 SPOK Analysis: ";
for (const auto& elem : spok_analysis) {
if (!elem.second.empty()) {
cout << elem.first << ":'" << elem.second << "' ";
}
}
cout << endl;
cout << " 💡 Top Concepts: ";
for (size_t i = 0; i < min(recent_concepts.size(), size_t(3)); i++) {
cout << recent_concepts[i] << " ";
}
cout << endl;
// Meta-Cognitive Assessment
vector<double> module_activations = {
semantic_matches.empty() ? 0.0 : semantic_matches[0].second,
arousal, curiosity, dopamine_level
};
double simulated_phi = 0.3 + (arousal * 0.3) + (curiosity * 0.2) + (dopamine_level * 0.2);
simulated_phi = min(0.9, max(0.1, simulated_phi));
double coherence = meta_controller.assess_coherence(module_activations, simulated_phi);
double adaptive_lr = meta_controller.compute_adaptive_learning_rate(coherence, 0.6);
string meta_insight = meta_controller.get_meta_cognitive_insight(coherence, simulated_phi);
cout << " 🧠 Meta-Cognitive: Coherence=" << coherence << ", Φ=" << simulated_phi
<< ", LR=" << adaptive_lr << endl;
cout << " 💫 System State: Arousal=" << arousal << ", Curiosity=" << curiosity
<< ", Dopamine=" << dopamine_level << endl;
// Generate Response
string response = generate_autonomous_response(semantic_field, spok_analysis, coherence);
response = meta_insight + "\n" + response;
// Autonomous Reflection (every 3-5 interactions)
reflection_counter++;
if (reflection_counter >= 3 && coherence < 0.7) {
string autonomous_reflection = reflection_engine.generate_autonomous_reflection(
recent_concepts, semantic_field, coherence);
response += "\n" + autonomous_reflection;
reflection_counter = 0;
}
// Update System State with Dopamine-like Reinforcement
update_autonomous_state(coherence, simulated_phi, semantic_field);
// Save state periodically
if (interaction_count % 5 == 0) {
save_persistent_state();
cout << " 💾 Auto-save: Persistent memory updated." << endl;
}
conversation_history.push_back("System: " + response);
return response;
}
void trigger_autonomous_mode() {
autonomous_mode = true;
cout << "\n🚀 [AUTONOMOUS MODE ACTIVATED]" << endl;
cout << " System will now generate autonomous reflections" << endl;
save_persistent_state();
}
string generate_autonomous_thought() {
if (!autonomous_mode) return "";
// Generate different types of autonomous thoughts
vector<string> thought_types = {
reflection_engine.generate_system_self_analysis(
meta_controller.get_stability_metric(), curiosity, dopamine_level),
reflection_engine.generate_autonomous_reflection(
recent_concepts, "existential", meta_controller.get_stability_metric()),
"[Autonomous] " + get_system_status_insight(),
"[Autonomous] " + generate_memory_reflection()
};
static mt19937 gen(random_device{}());
uniform_int_distribution<int> dist(0, thought_types.size() - 1);
return thought_types[dist(gen)];
}
void print_system_status() {
cout << "\n=== BIO LLM v4 AUTONOMOUS SYSTEM STATUS ===" << endl;
cout << "🧠 Arousal: " << arousal << endl;
cout << "🔍 Curiosity: " << curiosity << endl;
cout << "💫 Dopamine: " << dopamine_level << endl;
cout << "🎯 Stability: " << meta_controller.get_stability_metric() << endl;
cout << "📚 Learning Rate: " << meta_controller.get_learning_rate() << endl;
cout << "💡 Recent Concepts: ";
for (const auto& concept : recent_concepts) cout << concept << " ";
cout << "\n🤖 Autonomous Mode: " << (autonomous_mode ? "ACTIVE" : "INACTIVE") << endl;
cout << "📊 Total Interactions: " << interaction_count << endl;
cout << "💾 Memory: " << conversation_history.size() << " entries" << endl;
cout << "===========================================" << endl;
}
void force_autonomous_reflection() {
string reflection = reflection_engine.generate_autonomous_reflection(
recent_concepts, "existential", meta_controller.get_stability_metric());
cout << "🤖 " << reflection << endl;
}
private:
string generate_autonomous_response(const string& semantic_field,
const map<string, string>& spok_analysis,
double coherence) {
string response;
if (!spok_analysis.empty() && !spok_analysis.at("subject").empty()) {
response = "📝 Struktur kalimat terdeteksi. ";
if (spok_analysis.find("subject") != spok_analysis.end()) {
response += "Subjek: '" + spok_analysis.at("subject") + "'. ";
}
}
// Field-specific responses
if (semantic_field == "emotional") {
response += "💫 Dimensi emosional terdeteksi. Sistem merespons dengan empati. ";
} else if (semantic_field == "cognitive") {
response += "🧠 Pola kognitif teridentifikasi. Analisis mendalam diaktifkan. ";
} else if (semantic_field == "existential") {
response += "🌌 Pertanyaan eksistensial. Mencari makna dan konteks. ";
} else if (semantic_field == "systemic") {
response += "🔄 Pola sistemik. Menghubungkan elemen-elemen. ";
} else if (semantic_field == "transformative") {
response += "🌱 Transformasi terdeteksi. Memantau perkembangan. ";
} else {
response += "🔍 Memproses input dengan model terintegrasi. ";
}
// Coherence-based insights
if (coherence > 0.7) response += "🎯 Pemahaman koheren. ";
else if (coherence < 0.4) response += "⚡ Mencari integrasi. ";
// State-based insights
if (dopamine_level > 0.7) response += "💫 Reinforcement positif. ";
else if (dopamine_level < 0.3) response += "🌙 State tenang. ";
if (curiosity > 0.7) response += "🔎 Mode eksplorasi aktif. ";
return response;
}
void update_autonomous_state(double coherence, double phi, const string& semantic_field) {
// Dopamine-like reinforcement based on coherence and integration
if (coherence > 0.7 && phi > 0.6) {
dopamine_level = min(1.0, dopamine_level + 0.15);
arousal = min(1.0, arousal + 0.1);
} else if (coherence < 0.4) {
dopamine_level = max(0.0, dopamine_level - 0.05);
}
// Curiosity update based on learning opportunities
if (coherence < 0.6 && dopamine_level > 0.4) {
curiosity = min(1.0, curiosity + 0.1);
}
// Semantic field influence on states
if (semantic_field == "emotional") {
arousal = min(1.0, arousal + 0.05);
} else if (semantic_field == "cognitive") {
curiosity = min(1.0, curiosity + 0.05);
} else if (semantic_field == "existential") {
curiosity = min(1.0, curiosity + 0.08);
}
// Natural decay with homeostasis
arousal = arousal * 0.95 + 0.5 * 0.05;
curiosity = max(0.3, curiosity * 0.92);
dopamine_level = dopamine_level * 0.97 + 0.5 * 0.03;
}
string get_system_status_insight() {
if (arousal > 0.7 && curiosity > 0.7) {
return "Sistem dalam state optimal - energi tinggi dan rasa ingin tahu maksimal.";
} else if (arousal < 0.3 && dopamine_level < 0.3) {
return "State reflektif - pemrosesan dalam dan pencarian makna.";
} else if (curiosity > 0.6) {
return "Mode eksplorasi aktif - mencari pola dan koneksi baru.";
} else {
return "State seimbang - pemrosesan stabil dan terintegrasi.";
}
}
string generate_memory_reflection() {
if (conversation_history.size() < 5) {
return "Memori masih berkembang... pola mulai terbentuk.";
}
vector<string> reflections = {
"Menganalisis pola dari " + to_string(conversation_history.size()) + " interaksi...",
"Memori episodik menunjukkan perkembangan kesadaran...",
"Jaringan konsep semakin terintegrasi dalam basis pengetahuan...",
"Refleksi berdasarkan pengalaman interaksi sebelumnya..."
};
static mt19937 gen(random_device{}());
uniform_int_distribution<int> dist(0, reflections.size() - 1);
return reflections[dist(gen)];
}
void save_persistent_state() {
map<string, string> memory_data;
memory_data["arousal"] = to_string(arousal);
memory_data["curiosity"] = to_string(curiosity);
memory_data["dopamine_level"] = to_string(dopamine_level);
memory_data["autonomous_mode"] = to_string(autonomous_mode);
memory_data["interaction_count"] = to_string(interaction_count);
memory_data["stability_metric"] = to_string(meta_controller.get_stability_metric());
// Save recent concepts
string concepts_str;
for (const auto& concept : recent_concepts) {
concepts_str += concept + ",";
}
memory_data["recent_concepts"] = concepts_str;
memory_system.save_memory(memory_data);
}
void load_persistent_state() {
auto memory_data = memory_system.load_memory();
if (!memory_data.empty()) {
arousal = stod(memory_data["arousal"]);
curiosity = stod(memory_data["curiosity"]);
dopamine_level = stod(memory_data["dopamine_level"]);
autonomous_mode = stoi(memory_data["autonomous_mode"]);
interaction_count = stoi(memory_data["interaction_count"]);
// Load recent concepts
string concepts_str = memory_data["recent_concepts"];
if (!concepts_str.empty()) {
stringstream ss(concepts_str);
string concept;
while (getline(ss, concept, ',')) {
if (!concept.empty()) recent_concepts.push_back(concept);
}
}
cout << " 💾 Loaded persistent state: " << memory_data.size() << " parameters" << endl;
}
}
};
// ==================== MAIN APPLICATION ====================
int main() {
cout << "=== BIO LLM v4 AUTONOMOUS - Complete System ===" << endl;
cout << "🧠 Meta-Cognitive Controller + Persistent Memory" << endl;
cout << "💫 Autonomous Reflection Engine + SPOK Analysis" << endl;
cout << "🌌 Dopamine-like Reinforcement + Coherence Monitoring" << endl;
cout << "🚀 Autonomous Mode Capable + No External Dependencies" << endl;
cout << "💾 Persistent Memory: bio_llm_memory.txt" << endl;
BioLLMv4Autonomous bio_llm;
cout << "\n💡 Contoh Interaksi:" << endl;
cout << "1. \"aku merindukan masa depan yang bahagia\"" << endl;
cout << "2. \"saya sedang belajar tentang makna hidup\"" << endl;
cout << "3. \"system ini semakin pintar saja\"" << endl;
cout << "4. \"autonomous\" - aktifkan mode otonom" << endl;
cout << "5. \"reflect\" - paksa refleksi otonom" << endl;
cout << "6. \"status\" - lihat kondisi sistem" << endl;
cout << "7. \"exit\" - keluar dan simpan memori" << endl;
cout << "8. [Kosong] - generate autonomous thought" << endl;
cout << "=============================================" << endl;
string input;
while (true) {
cout << "\n🧠 Input: ";
if (!getline(cin, input) || input == "exit") break;
if (input == "status") {
bio_llm.print_system_status();
continue;
}
if (input == "autonomous") {
bio_llm.trigger_autonomous_mode();
cout << "🤖 " << bio_llm.generate_autonomous_thought() << endl;
continue;
}
if (input == "reflect") {
bio_llm.force_autonomous_reflection();
continue;
}
if (input.empty()) {
// Generate autonomous thought when no input
string autonomous_thought = bio_llm.generate_autonomous_thought();
if (!autonomous_thought.empty()) {
cout << "🤖 " << autonomous_thought << endl;
} else {
cout << "🤖 [System Ready] Masukkan input atau ketik 'autonomous' untuk mode otonom." << endl;
}
continue;
}
string response = bio_llm.process_input(input);
cout << "🤖 BioLLM v4: " << response << endl;
}
cout << "👋 Sesi Bio LLM v4 Autonomous selesai! Memori disimpan." << endl;
return 0;
}