Skip to content

Commit ca8d805

Browse files
committed
GA
1 parent 41fb476 commit ca8d805

4 files changed

Lines changed: 26 additions & 14 deletions

File tree

‎src/beemsearch.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl BeemSearch {
7171
next_count: i8,
7272
) -> i64 {
7373
let counter = Arc::new(AtomicUsize::new(0));
74-
//let next_count = 4;
74+
let next_count = 4;
7575

7676
//vec![1,2,3,4] -> 1234
7777
let mut next_int = 0;

‎src/environment.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ impl Environment {
480480

481481
if self.field[(x + y) as usize * 10] {
482482
self.dead_flag = true;
483+
self.score -= 1000;
483484
break;
484485
}
485486
}
@@ -654,7 +655,7 @@ impl Environment {
654655
self.field[(x + y * 10) as usize] = true;
655656
}
656657

657-
self.score += 1;
658+
self.score += 2;
658659

659660
let line = Self::check_and_clear_line(&mut self.field);
660661
self.cleared_line += line as isize;
@@ -910,7 +911,7 @@ impl Environment {
910911
result /= 10;
911912
}
912913

913-
if environment.cleared_line == 150 || environment.dead_flag {
914+
if environment.cleared_line >= 150 || environment.dead_flag {
914915
return environment.score as i32;
915916
}
916917
}

‎src/geneticalgorithm.rs‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl GeneticAlgorithm {
112112
result
113113
}
114114

115-
fn learn() {
115+
pub fn learn() {
116116
let mut input = String::new();
117117
let indivisual_count;
118118
let child_count;
@@ -123,27 +123,31 @@ impl GeneticAlgorithm {
123123
println!("1 Yes");
124124
println!("2 No");
125125
io::stdin().read_line(&mut input).unwrap();
126-
match input.as_str() {
126+
match input.trim() {
127127
"1" => {}
128128
"2" => {}
129129
_ => panic!("不明な操作"),
130130
}
131131

132-
print!("世代数を入力:");
132+
input = "".to_string();
133+
println!("世代数を入力");
133134
io::stdin().read_line(&mut input).unwrap();
134-
indivisual_count = input.parse::<i32>().unwrap();
135+
indivisual_count = input.trim().parse::<i32>().unwrap();
135136

136-
print!("子供数を入力:");
137+
input = "".to_string();
138+
println!("子供数を入力");
137139
io::stdin().read_line(&mut input).unwrap();
138-
child_count = input.parse::<i32>().unwrap();
140+
child_count = input.trim().parse::<i32>().unwrap();
139141

142+
input = "".to_string();
140143
println!("初期化値の上限");
141144
io::stdin().read_line(&mut input).unwrap();
142-
random_max = input.parse::<f64>().unwrap();
145+
random_max = input.trim().parse::<f64>().unwrap();
143146

147+
input = "".to_string();
144148
println!("初期化値の下限");
145149
io::stdin().read_line(&mut input).unwrap();
146-
random_min = input.parse::<f64>().unwrap();
150+
random_min = input.trim().parse::<f64>().unwrap();
147151

148152
println!("MGGで学習を開始します\r\n学習結果は./learn/[index].txtとして保存されます");
149153

@@ -168,7 +172,14 @@ impl GeneticAlgorithm {
168172

169173
indivisuals.sort_by(|a, b| b.evaluation.partial_cmp(&a.evaluation).unwrap());
170174

171-
println!("最も高い評価:{}", indivisuals.index(0).evaluation);
175+
println!();
176+
177+
for i in 0..indivisuals.len() {
178+
print!("{}番目の評価:{}\r\n重み", i + 1, indivisuals[i].evaluation);
179+
for j in 0..indivisuals[i].values.len() {
180+
print!("{} ", indivisuals[i].values[j]);
181+
}
182+
}
172183

173184
let mut childs = Vec::new();
174185

‎src/main.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ pub static THREAD_POOL: OnceCell<Mutex<ThreadPool>> = OnceCell::new();
2626

2727
//デバッグ用でスレッド数変えてる
2828
fn main() {
29-
GeneticAlgorithm::bench_mark_test();
30-
3129
assert!(
3230
THREAD_POOL
3331
.set(Mutex::new(ThreadPool::new(num_cpus::get())))
@@ -45,6 +43,8 @@ fn main() {
4543
"err"
4644
);
4745

46+
GeneticAlgorithm::learn();
47+
4848
let mut environment = Environment::new();
4949
environment.init();
5050

0 commit comments

Comments
 (0)