use criterion::{black_box, criterion_group, criterion_main, Criterion}; use new_york_utils::Matrix; use vivalaakam_neat_rs::Genome; fn criterion_benchmark(c: &mut Criterion) { let input_network = r#"{"connections":[{"enabled":false,"from":"input_0","to":"output_0","weight":0.777410022846365},{"enabled":false,"from":"input_1","to":"output_0","weight":0.07753713399775261},{"enabled":false,"from":"input_1","to":"MirEU2","weight":1.0174453950443163},{"enabled":false,"from":"MirEU2","to":"output_0","weight":0.8461961822163597},{"enabled":false,"from":"input_0","to":"P94eHk","weight":1.1343665967199728},{"enabled":false,"from":"P94eHk","to":"output_0","weight":0.8310029652281727},{"enabled":false,"from":"input_1","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"input_1","to":"02pYyn","weight":1.0},{"enabled":false,"from":"02pYyn","to":"MirEU2","weight":0.9325528067613452},{"enabled":true,"from":"input_0","to":"MirEU2","weight":0.6090448879271348},{"enabled":true,"from":"input_0","to":"IqUenQ","weight":0.9361497905793383},{"enabled":false,"from":"IqUenQ","to":"P94eHk","weight":1.1310947520214831},{"enabled":false,"from":"input_1","to":"ahiCHC","weight":1.0},{"enabled":false,"from":"ahiCHC","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"02pYyn","to":"Slgj8n","weight":1.1941097782417684},{"enabled":true,"from":"Slgj8n","to":"MirEU2","weight":1.057863300156034},{"enabled":true,"from":"input_1","to":"XZvBOQ","weight":0.918042033782144},{"enabled":true,"from":"XZvBOQ","to":"MirEU2","weight":0.9512082634576944},{"enabled":true,"from":"input_0","to":"XZvBOQ","weight":0.33788047300376833},{"enabled":false,"from":"input_0","to":"Slgj8n","weight":0.4542064417224311},{"enabled":false,"from":"input_0","to":"ahiCHC","weight":0.5214622405268836},{"enabled":true,"from":"input_1","to":"WWvSiC","weight":1.0},{"enabled":false,"from":"WWvSiC","to":"MirEU2","weight":1.0174453950443163},{"enabled":true,"from":"input_1","to":"IqUenQ","weight":0.6260369178863576},{"enabled":true,"from":"input_0","to":"QsafUL","weight":1.0523744501111378},{"enabled":true,"from":"QsafUL","to":"P94eHk","weight":1.1971337498952423},{"enabled":true,"from":"input_0","to":"WWvSiC","weight":-0.34309157606183815},{"enabled":true,"from":"input_1","to":"lYa8nN","weight":1.0},{"enabled":true,"from":"lYa8nN","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"ahiCHC","to":"jfPSim","weight":1.0210475908070096},{"enabled":false,"from":"jfPSim","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"jfPSim","to":"tSU46s","weight":1.0107278910688346},{"enabled":true,"from":"tSU46s","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"input_1","to":"QsafUL","weight":0.2831421904779922},{"enabled":false,"from":"input_0","to":"jfPSim","weight":0.7652650189865984},{"enabled":true,"from":"input_0","to":"wUNUPQ","weight":0.9727283218544478},{"enabled":true,"from":"wUNUPQ","to":"IqUenQ","weight":0.9361497905793383},{"enabled":true,"from":"input_0","to":"JrkIGm","weight":1.0},{"enabled":false,"from":"JrkIGm","to":"Slgj8n","weight":0.566076300077418},{"enabled":true,"from":"input_1","to":"Slgj8n","weight":-0.07877000442738698},{"enabled":true,"from":"input_0","to":"tSU46s","weight":-0.7327817201998321},{"enabled":true,"from":"input_0","to":"eQ8dDU","weight":1.0},{"enabled":false,"from":"eQ8dDU","to":"XZvBOQ","weight":0.4674471342831743},{"enabled":false,"from":"input_1","to":"JrkIGm","weight":-0.8104069930270376},{"enabled":true,"from":"JrkIGm","to":"wbLCGr","weight":0.8907528991925684},{"enabled":true,"from":"wbLCGr","to":"Slgj8n","weight":0.566076300077418},{"enabled":true,"from":"IqUenQ","to":"gW0yO9","weight":1.0},{"enabled":true,"from":"gW0yO9","to":"P94eHk","weight":1.0811735429411624},{"enabled":false,"from":"input_1","to":"wbLCGr","weight":0.2728714499275764},{"enabled":true,"from":"input_0","to":"aHGh9H","weight":1.0},{"enabled":false,"from":"aHGh9H","to":"P94eHk","weight":1.1343665967199728},{"enabled":true,"from":"aHGh9H","to":"C2ZEJL","weight":1.0},{"enabled":true,"from":"C2ZEJL","to":"P94eHk","weight":1.1274106375104394},{"enabled":true,"from":"input_1","to":"tSU46s","weight":-0.09408256792164488},{"enabled":true,"from":"JrkIGm","to":"ffgG1Z","weight":1.0},{"enabled":true,"from":"ffgG1Z","to":"Slgj8n","weight":0.566076300077418},{"enabled":true,"from":"input_1","to":"3dZfH0","weight":1.0},{"enabled":true,"from":"3dZfH0","to":"wbLCGr","weight":0.2728714499275764},{"enabled":true,"from":"input_1","to":"eQ8dDU","weight":0.7678356216400191},{"enabled":true,"from":"input_1","to":"DFGd3T","weight":1.0},{"enabled":false,"from":"DFGd3T","to":"eQ8dDU","weight":0.7678356216400191},{"enabled":true,"from":"eQ8dDU","to":"agafi5","weight":1.0952695441864138},{"enabled":true,"from":"agafi5","to":"XZvBOQ","weight":0.4674471342831743},{"enabled":false,"from":"ahiCHC","to":"TPQP25","weight":1.0},{"enabled":true,"from":"TPQP25","to":"P94eHk","weight":-0.5756438989027989},{"enabled":true,"from":"input_1","to":"wUNUPQ","weight":0.1542767918456846},{"enabled":true,"from":"input_1","to":"9is8pq","weight":0.9530770869831244},{"enabled":true,"from":"9is8pq","to":"JrkIGm","weight":-0.8830180442301252},{"enabled":true,"from":"input_0","to":"yF3roG","weight":1.0},{"enabled":true,"from":"yF3roG","to":"eQ8dDU","weight":1.0},{"enabled":true,"from":"MirEU2","to":"8uP5Mi","weight":1.0},{"enabled":true,"from":"8uP5Mi","to":"output_0","weight":0.9417694883935641},{"enabled":true,"from":"input_0","to":"lYa8nN","weight":-0.6231198518345669},{"enabled":true,"from":"aHGh9H","to":"yhRJJP","weight":1.0},{"enabled":true,"from":"yhRJJP","to":"P94eHk","weight":1.042047150874489},{"enabled":true,"from":"input_0","to":"02pYyn","weight":0.1395900742213403},{"enabled":true,"from":"input_0","to":"qdJdTN","weight":1.0},{"enabled":true,"from":"qdJdTN","to":"IqUenQ","weight":0.9361497905793383},{"enabled":true,"from":"input_0","to":"TPQP25","weight":0.18577175255000844},{"enabled":true,"from":"input_1","to":"yF3roG","weight":-0.7932921075889703},{"enabled":true,"from":"input_0","to":"pYjnWb","weight":1.0},{"enabled":true,"from":"pYjnWb","to":"IqUenQ","weight":0.9361497905793383}],"nodes":[{"activation":"Tanh","bias":0.0,"enabled":true,"id":"input_0","neuron_type":"Input","position":0},{"activation":"Relu","bias":0.0,"enabled":true,"id":"input_1","neuron_type":"Input","position":1},{"activation":"Sigmoid","bias":-0.317978152777166,"enabled":true,"id":"output_0","neuron_type":"Output","position":32},{"activation":"Gaussian","bias":-0.785080405486027,"enabled":true,"id":"MirEU2","neuron_type":"Hidden","position":30},{"activation":"SoftSign","bias":-0.3770143294632542,"enabled":true,"id":"P94eHk","neuron_type":"Hidden","position":27},{"activation":"Sigmoid","bias":-0.751341631749803,"enabled":true,"id":"02pYyn","neuron_type":"Hidden","position":2},{"activation":"Gaussian","bias":0.9232305020194611,"enabled":true,"id":"IqUenQ","neuron_type":"Hidden","position":20},{"activation":"Selu","bias":-0.5001787044771437,"enabled":true,"id":"ahiCHC","neuron_type":"Hidden","position":3},{"activation":"Selu","bias":-0.22894845745767034,"enabled":true,"id":"Slgj8n","neuron_type":"Hidden","position":28},{"activation":"Relu","bias":-0.8336365161573315,"enabled":true,"id":"XZvBOQ","neuron_type":"Hidden","position":29},{"activation":"Gaussian","bias":-0.5468012769601671,"enabled":true,"id":"WWvSiC","neuron_type":"Hidden","position":4},{"activation":"Identity","bias":-0.20680212768289663,"enabled":true,"id":"QsafUL","neuron_type":"Hidden","position":5},{"activation":"Sinusoid","bias":0.7686660073815766,"enabled":true,"id":"lYa8nN","neuron_type":"Hidden","position":6},{"activation":"Step","bias":0.3662435195241853,"enabled":false,"id":"jfPSim","neuron_type":"Hidden","position":7},{"activation":"Sigmoid","bias":0.7992889987484726,"enabled":true,"id":"tSU46s","neuron_type":"Hidden","position":8},{"activation":"Step","bias":0.3778579283487924,"enabled":true,"id":"wUNUPQ","neuron_type":"Hidden","position":9},{"activation":"Step","bias":-0.14089474065747654,"enabled":true,"id":"JrkIGm","neuron_type":"Hidden","position":21},{"activation":"Selu","bias":0.09303157761513584,"enabled":true,"id":"eQ8dDU","neuron_type":"Hidden","position":22},{"activation":"Selu","bias":-0.37155450481699387,"enabled":true,"id":"wbLCGr","neuron_type":"Hidden","position":23},{"activation":"Sinusoid","bias":0.7136956261914382,"enabled":true,"id":"gW0yO9","neuron_type":"Hidden","position":24},{"activation":"Selu","bias":0.8020018145544465,"enabled":true,"id":"aHGh9H","neuron_type":"Hidden","position":10},{"activation":"Gaussian","bias":0.34751944254711165,"enabled":true,"id":"C2ZEJL","neuron_type":"Hidden","position":11},{"activation":"Tanh","bias":-0.9377337219786626,"enabled":true,"id":"ffgG1Z","neuron_type":"Hidden","position":25},{"activation":"Identity","bias":0.5561285951589379,"enabled":true,"id":"3dZfH0","neuron_type":"Hidden","position":12},{"activation":"Selu","bias":0.22904511993181487,"enabled":true,"id":"DFGd3T","neuron_type":"Hidden","position":13},{"activation":"Tanh","bias":0.8356364137538534,"enabled":true,"id":"agafi5","neuron_type":"Hidden","position":26},{"activation":"Sigmoid","bias":-0.03788136152627053,"enabled":true,"id":"TPQP25","neuron_type":"Hidden","position":14},{"activation":"SoftSign","bias":-0.05942713419548218,"enabled":true,"id":"9is8pq","neuron_type":"Hidden","position":15},{"activation":"Gaussian","bias":-0.41189734142615775,"enabled":true,"id":"yF3roG","neuron_type":"Hidden","position":16},{"activation":"Selu","bias":-0.15749512467252424,"enabled":true,"id":"8uP5Mi","neuron_type":"Hidden","position":31},{"activation":"Sigmoid","bias":0.6968361899952709,"enabled":true,"id":"yhRJJP","neuron_type":"Hidden","position":17},{"activation":"Sigmoid","bias":-0.45395906959530286,"enabled":true,"id":"qdJdTN","neuron_type":"Hidden","position":18},{"activation":"Relu","bias":-0.16532195876945988,"enabled":true,"id":"pYjnWb","neuron_type":"Hidden","position":19}]}"#; let genome: Genome = input_network.into(); let network = genome.get_network(); let inputs_vec = vec![ vec![0f64, 0f64], vec![0f64, 1f64], vec![1f64, 0f64], vec![1f64, 1f64], ]; let mut inputs_matrix = Matrix::new(2, 4); inputs_matrix .set_data(vec![0f64, 0f64, 0f64, 1f64, 1f64, 0f64, 1f64, 1f64]) .expect("TODO: panic message"); let outputs = vec![0f64, 1f64, 1f64, 0f64]; c.bench_function("activate", |b| { b.iter(|| { let mut distance: f64 = 0f64; for i in 0..4 { let output = network.activate(inputs_vec[i].to_vec()); distance += (outputs[i] - output[0]).powi(2); } }) }); c.bench_function("activate_matrix", |b| { b.iter(|| { let mut distance: f64 = 0f64; let output = network.activate_matrix(&inputs_matrix); for i in 0..4 { distance += (outputs[i] - output.get(0, i).unwrap_or_default()).powi(2); } }) }); } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);