Skip to content

Commit

Permalink
test: ✅ 基于先前的「Websocket服务器测试」探究「无Matriangle自动测试」以及「NARS推理适应性」
Browse files Browse the repository at this point in the history
基于「知识不时颠倒,需要反复修正」的「动态环境」测试NARS的适应性:ONA最强,OpenNARS容易死板
  • Loading branch information
ARCJ137442 committed Apr 8, 2024
1 parent 3632527 commit 3c3f3f7
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/bin/babelnar_cli/runtime_manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ where
for io_result in ReadlineIter::default() {
// 从迭代器中读取一行
let line = io_result?;
let line = line.trim(); // ! 这两句无法合并:临时变量的引用问题

// 尝试获取运行时引用 | 仅有其它地方panic了才会停止
// ! 📝PoisonError无法在线程中传递
Expand All @@ -318,9 +319,9 @@ where
.transform_err(|e| anyhow!("获取NAVM输出缓存时发生错误:{e}"))?;

// 非空⇒解析输入并执行
if !line.trim().is_empty() {
if !line.is_empty() {
if_let_err_eprintln_cli!(
Self::input_line_to_vm(runtime, &line, &config, output_cache)
Self::input_line_to_vm(runtime, line, &config, output_cache)
=> e => [Error] "输入过程中发生错误:{e}"
);
}
Expand Down
95 changes: 95 additions & 0 deletions src/bin/ws_server_test/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,105 @@
use std::{
cell::RefCell,
thread::{self, sleep},
time::Duration,
};
extern crate ws;

fn main() {
/// 单次训练过程
fn train(sender: ws::Sender) -> impl Fn(ws::Message) -> Result<(), ws::Error> {
// 尝试注册操作
let _ = sender.send("REG left".to_string());
let _ = sender.send("REG right".to_string());

// 预先经验
for _ in 0..5 {
// 背景事件
let _ = sender.send("NSE <a --> b>. :|:".to_string());
// 自身操作
let _ = sender.send("NSE <(*, {SELF}) --> ^left>. :|:".to_string());
let _ = sender.send("NSE <(*, {SELF}) --> ^right>. :|:".to_string());
// 一定间隔
let _ = sender.send("CYC 10".to_string());
// 自身状态
let _ = sender.send("NSE <{SELF} --> [good]>. :|:".to_string());
}
// 再间隔一段时间,开始训练
let _ = sender.send("CYC 100".to_string());

let sender2 = sender.clone();
// 生成一个不断发送消息的线程
thread::spawn(move || loop {
let _ = sender2.send("NSE <a --> b>. :|:".to_string());
let _ = sender2.send("CYC 10".to_string());
let _ = sender2.send("NSE <{SELF} --> [good]>! :|:".to_string());
// let _ = sender2.send("NSE <?1 =/> <{SELF} --> [good]>>? :|:".to_string());
thread::sleep(Duration::from_secs_f64(0.03));
});

// * 📝Websocket Handler不能可变,就用RefCell实现内部可变性
let right_side = RefCell::new(false);
let num_good = RefCell::new(0_usize);
let output_steps = RefCell::new(0_usize);
let minimum_fitness_period = RefCell::new(usize::MAX);
const MAX_GOOD: usize = 20;
move |msg: ws::Message| {
// println!("Got message: {}", msg);
let msg = msg.to_string();
// 记录步数
let output_steps = &mut *output_steps.borrow_mut();
*output_steps += 1;
// 操作
if msg.contains("EXE") {
// 左右操作状态
let left = msg.contains(r#"["left","{SELF}"]"#);
let right = msg.contains(r#"["right","{SELF}"]"#);
if !left && !right {
return Ok(());
}
let minimum_fitness_period = &mut *minimum_fitness_period.borrow_mut();
// * 🔬可以尝试「左右颠倒」以观察NARS的适应能力
let num_good = &mut *num_good.borrow_mut();
let right_side = &mut *right_side.borrow_mut();
let lr = if *right_side { "right" } else { "left" };
// 奖励
if left && !*right_side || right && *right_side {
let _ = sender.send("NSE <{SELF} --> [good]>. :|: %1.0; 0.5%".to_string());
println!("good\t{lr}\tfor {num_good}!\t{minimum_fitness_period}");
*num_good += 1;
// 改变模式
if *num_good > MAX_GOOD {
let b = *right_side;
*right_side = !b;
*num_good = 0;
// 一个轮回⇒以「轮回数」记录「适应性」
if b {
*minimum_fitness_period = *minimum_fitness_period.min(output_steps);
*output_steps = 0;
}
}
}
// 惩罚
else {
let _ = sender.send("NSE <{SELF} --> [good]>. :|: %0.0; 0.5%".to_string());
println!("bad\t{lr}\tfor {num_good}!\t{minimum_fitness_period}");
}
}
// out.close(CloseCode::Normal)
Ok(())
}
}

// 循环
loop {
let _ = ws::connect("ws://127.0.0.1:8765", train);
// 连接失败则延迟等待
sleep(Duration::from_secs(1));
}
}

#[test]
fn test_overwhelming_nse() {
loop {
let _ = ws::connect("ws://127.0.0.1:8765", |sender| {
// 生成一个不断发送消息的线程
Expand Down
1 change: 1 addition & 0 deletions src/cin_implements/ona/translators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fn test_hash_operator_id() {
/// * 🚩直接根据选取的「头部」进行匹配
/// 超参数:严格模式
/// * 🚩测试环境下「输出Narsese解析失败」会上报错误
/// TODO: 解决`Input: <(* {SELF}) --> ^left>. :|: occurrenceTime=119 Priority=1.000000 Truth: frequency=1.000000, confidence=0.900000`
pub fn output_translate(content_raw: String) -> Result<Output> {
// 特别处理
if_return! {
Expand Down

0 comments on commit 3c3f3f7

Please sign in to comment.