Skip to content

Commit 1409cb4

Browse files
committed
GA実装中
1 parent d7d97e1 commit 1409cb4

File tree

6 files changed

+64
-22
lines changed

6 files changed

+64
-22
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"args": [],
2323
"cwd": "${workspaceFolder}",
24-
"console": "externalTerminal"
24+
// "console": "externalTerminal"
2525
},
2626
{
2727
"type": "lldb",

src/beemsearch.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl BeemSearch {
211211
let mut eval = 0.0;
212212
let mut firstmove = 0;
213213
SEARCHED_DATA_VEC.with(|vec| {
214-
// vec.borrow_mut()[beem].eval += processdata.before_eval;
215214
eval = vec.borrow()[beem].eval;
216215
firstmove = vec.borrow()[beem].move_value;
217216
});
@@ -325,11 +324,10 @@ impl BeemSearch {
325324
move_flag: bool,
326325
) {
327326
if cfg!(debug_assertions) {
328-
if move_count > 9 {
327+
if move_count > 11 {
329328
panic!("ループしてない?");
330329
}
331330
}
332-
//無駄な回転検索がある
333331
//ハードドロップ
334332
{
335333
let mut new_move_diff = Action::HARD_DROP as i64;
@@ -376,8 +374,8 @@ impl BeemSearch {
376374
}
377375

378376
let cleared_line = Environment::check_and_clear_line(&mut field_clone);
379-
pattern.eval = Evaluation::evaluate(&field_clone, &newmino, cleared_line)
380-
+ before_eval * 1.0;
377+
pattern.eval =
378+
Evaluation::evaluate(&field_clone, &newmino, cleared_line) + before_eval;
381379

382380
VEC_FIELD.with(|value| {
383381
value.borrow_mut().push(field_clone);

src/draw.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ use std::{
1111
};
1212

1313
pub fn print(field: &[bool; Environment::FIELD_HEIGHT * Environment::FIELD_WIDTH], mino: &Mino) {
14-
enable_raw_mode().unwrap();
14+
// enable_raw_mode().unwrap();
1515

1616
let mut stdout = stdout();
1717

18-
execute!(stdout, Hide, DisableBlinking, cursor::MoveTo(0, 0)).unwrap();
18+
execute!(
19+
stdout,
20+
Hide,
21+
DisableBlinking,
22+
cursor::MoveTo(0, 0),
23+
//Clear()
24+
)
25+
.unwrap();
1926
queue!(stdout, cursor::MoveTo(0, 0)).unwrap();
2027

2128
let mut y = Environment::FIELD_HEIGHT as i32 - 1;

src/evaluation.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ impl Evaluation {
119119
+ (hole_count as f64 * -0.3566)
120120
+ (bump as f64 * -0.184);
121121

122-
/* draw::print_debug(field, mino, 0, eval);
123-
println!(
124-
"\nでこぼこ:{}\n消去ライン:{}\n穴:{}\n高さ合計:{}\n",
125-
bump, cleared_line, hole_count, sum_of_height
126-
);
127-
let mut temp = String::new();
128-
getch(true).unwrap();
129-
*/
122+
draw::print_debug(field, mino, 0, eval);
123+
println!(
124+
"\nでこぼこ:{}\n消去ライン:{}\n穴:{}\n高さ合計:{}\n",
125+
bump, cleared_line, hole_count, sum_of_height
126+
);
127+
let mut temp = String::new();
128+
// getch(true).unwrap();
129+
let mut tempstr = String::new();
130+
io::stdin().read_line(&mut tempstr).unwrap();
131+
130132
/*
131133
println!("高さ合計:{}\n消去ライン:{}\n穴:{}\nでこぼこ:{}\n穴に高さ合計2乗:{}\nでこぼこに高さ合計2乗:{}",
132134
(weight[0] * sum_of_height as f64)

src/geneticalgorithm.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use rand::{prelude::ThreadRng, *};
2+
3+
struct Indivisual {
4+
values: [f64; 0],
5+
evaluation: f64,
6+
}
7+
8+
struct GeneticAlgorithm;
9+
10+
impl GeneticAlgorithm {
11+
pub fn bench_mark_test() {
12+
let mut gen_count = 0;
13+
let random = rand::thread_rng();
14+
let mut indivisuals = Vec::new();
15+
16+
for _i in 0..30 {
17+
// let param=
18+
}
19+
}
20+
21+
fn learn() {}
22+
23+
fn bla_alpha_crossover() {}
24+
25+
fn roulette_choise() {}
26+
27+
fn tournament_choise() {}
28+
29+
fn elite_choise() {}
30+
31+
fn get_random(min: f64, max: f64, random: ThreadRng) {
32+
let range = max - min;
33+
let sample = random.gen_range(01);
34+
}
35+
}

src/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod beemsearch;
22
mod draw;
33
mod environment;
44
mod evaluation;
5+
mod geneticalgorithm;
56
mod grobaldata;
67
mod threadpool;
78

@@ -17,17 +18,15 @@ use std::{
1718
thread, time,
1819
};
1920

20-
use thread_id;
2121
use threadpool::ThreadPool;
22-
use winconsole::console::{self, getch};
23-
2422
pub static WEIGHT: OnceCell<[f64; Evaluation::WEIGHT_COUNT as usize]> = OnceCell::new();
2523
pub static THREAD_POOL: OnceCell<Mutex<ThreadPool>> = OnceCell::new();
2624

25+
//デバッグ用でスレッド数変えてる
2726
fn main() {
2827
assert!(
2928
THREAD_POOL
30-
.set(Mutex::new(ThreadPool::new(num_cpus::get())))
29+
.set(Mutex::new(ThreadPool::new(1 /*num_cpus::get() */)))
3130
.is_ok(),
3231
"スレッドプールの初期化失敗"
3332
);
@@ -44,6 +43,7 @@ fn main() {
4443

4544
let mut environment = Environment::new();
4645
environment.init();
46+
geneticalgorithm::bench_mark_test();
4747

4848
// environment.now_mino.mino_kind = 4;
4949
// environment.next = [4, 4, 4, 4, 4];
@@ -57,12 +57,12 @@ fn main() {
5757
let frame_time = time::Duration::from_millis(1000 / 30);
5858
let mut GrobalData = GrobalData::new(num_cpus::get() as u32);
5959

60-
// console::clear().unwrap();
6160
loop {
61+
// console::clear().unwrap();
6262
print(&environment.get_field_ref(), &environment.now_mino);
6363

6464
// getch(true).unwrap();
65-
thread::sleep_ms(500);
65+
thread::sleep_ms(1000);
6666
//io::stdin().read_line(&mut buf).unwrap();
6767

6868
let mut result = environment.search();

0 commit comments

Comments
 (0)