Skip to content

Commit f011c5e

Browse files
committed
GA
1 parent b0a62a9 commit f011c5e

File tree

5 files changed

+117
-48
lines changed

5 files changed

+117
-48
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ thread-id = "*"
1616
once_cell = "*"
1717
libm = "*"
1818

19-
2019
[profile.release]
2120
debug = true

src/beemsearch.rs

+67-33
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 = 5;
7575

7676
//vec![1,2,3,4] -> 1234
7777
let mut next_int = 0;
@@ -128,6 +128,7 @@ impl BeemSearch {
128128
Action::NULL,
129129
0,
130130
false,
131+
-1,
131132
);
132133

133134
let mut beem_width = 0;
@@ -323,25 +324,25 @@ impl BeemSearch {
323324
lock_direction: i8,
324325
rotate_count: i32,
325326
move_flag: bool,
327+
count_after_softdrop: i32,
326328
) {
327329
if cfg!(debug_assertions) {
328330
if move_count > 11 {
329331
panic!("ループしてない?");
330332
}
331333
}
334+
if count_after_softdrop >= 3 {
335+
return;
336+
}
332337

333-
//ハードドロップ
334-
{
335-
/*
336-
if move_value == 112 || move_value == 13 {
337-
println!();
338-
} */
339-
340-
let mut new_move_diff = Action::HARD_DROP as i64;
341-
for _i in 0..move_count {
342-
new_move_diff *= 10;
343-
}
338+
let mut new_move_diff = 1;
339+
340+
for _i in 0..move_count {
341+
new_move_diff *= 10;
342+
}
344343

344+
//ハードドロップ・ソフトドロップ
345+
if count_after_softdrop != 0 {
345346
let mut newmino = mino.clone();
346347
let mut temp = 0;
347348

@@ -355,6 +356,23 @@ impl BeemSearch {
355356

356357
newmino.move_pos(0, -temp);
357358

359+
//ソフトドロップ
360+
if count_after_softdrop == -1 {
361+
if temp > 0 {
362+
Self::search(
363+
&mut newmino.clone(),
364+
&field,
365+
move_count + 1,
366+
move_value + new_move_diff * Action::SOFT_DROP as i64,
367+
before_eval,
368+
lock_direction,
369+
rotate_count,
370+
move_flag,
371+
0,
372+
)
373+
}
374+
}
375+
358376
let hash =
359377
Self::get_hash_for_position(newmino.mino_kind, newmino.rotation, &newmino.position);
360378

@@ -363,13 +381,13 @@ impl BeemSearch {
363381
if let Some(result) = searched_data.get_mut(&hash) {
364382
if result.move_count > move_count {
365383
result.move_count = move_count;
366-
result.move_value = move_value + new_move_diff;
384+
result.move_value = move_value + new_move_diff * Action::HARD_DROP as i64;
367385
}
368386
} else {
369387
let mut pattern = SearchedPattern::new();
370388
pattern.position = newmino.position;
371389
pattern.move_count = move_count;
372-
pattern.move_value = move_value + new_move_diff;
390+
pattern.move_value = move_value + new_move_diff * Action::HARD_DROP as i64;
373391

374392
let mut field_clone = field.clone();
375393

@@ -410,20 +428,24 @@ impl BeemSearch {
410428
true,
411429
) {
412430
newmino.move_pos(Vector2::MX1.x, Vector2::MX1.y);
413-
let mut temp = Action::MOVE_LEFT as i64;
414-
for _i in 0..move_count {
415-
temp *= 10;
431+
432+
let softdrop_value;
433+
if count_after_softdrop == -1 {
434+
softdrop_value = -1;
435+
} else {
436+
softdrop_value = count_after_softdrop + 1;
416437
}
417438

418439
Self::search(
419440
&mut newmino,
420441
&field,
421442
move_count + 1,
422-
move_value + temp,
443+
move_value + new_move_diff * Action::MOVE_LEFT as i64,
423444
&before_eval,
424445
Action::MOVE_LEFT,
425446
rotate_count,
426447
true,
448+
softdrop_value,
427449
);
428450
}
429451
}
@@ -443,28 +465,31 @@ impl BeemSearch {
443465
) {
444466
newmino.move_pos(Vector2::X1.x, Vector2::X1.y);
445467

446-
let mut temp = Action::MOVE_RIGHT as i64;
447-
for _i in 0..move_count {
448-
temp *= 10;
468+
let softdrop_value;
469+
if count_after_softdrop == -1 {
470+
softdrop_value = -1;
471+
} else {
472+
softdrop_value = count_after_softdrop + 1;
449473
}
450474

451475
Self::search(
452476
&mut newmino,
453477
&field,
454478
move_count + 1,
455-
move_value + temp,
479+
move_value + new_move_diff * Action::MOVE_RIGHT as i64,
456480
&before_eval,
457481
Action::MOVE_RIGHT,
458482
rotate_count,
459483
true,
484+
softdrop_value,
460485
);
461486
}
462487
}
463488

464489
let mut result = Vector2::ZERO;
465490
//右回転
466491
if !move_flag
467-
&& rotate_count < 3
492+
&& rotate_count < 2
468493
&& Environment::try_rotate(Rotate::RIGHT, &field, mino, &mut result)
469494
{
470495
let mut newmino = mino.clone();
@@ -482,27 +507,30 @@ impl BeemSearch {
482507
newmino.move_pos(result.x, result.y);
483508
Environment::simple_rotate(Rotate::RIGHT, &mut newmino, 0);
484509

485-
let mut temp = Action::ROTATE_RIGHT as i64;
486-
for _i in 0..move_count {
487-
temp *= 10;
510+
let softdrop_value;
511+
if count_after_softdrop == -1 {
512+
softdrop_value = -1;
513+
} else {
514+
softdrop_value = count_after_softdrop + 1;
488515
}
489516

490517
Self::search(
491518
&mut newmino,
492519
&field,
493520
move_count + 1,
494-
move_value + temp,
521+
move_value + new_move_diff * Action::ROTATE_RIGHT as i64,
495522
&before_eval,
496523
lock_direction,
497524
rotate_count + 1,
498525
move_flag,
526+
softdrop_value,
499527
);
500528
}
501529
}
502530

503531
//左回転
504532
if !move_flag
505-
&& rotate_count < 3
533+
&& rotate_count < 2
506534
&& Environment::try_rotate(Rotate::LEFT, &field, mino, &mut result)
507535
{
508536
let mut newmino = mino.clone();
@@ -520,23 +548,28 @@ impl BeemSearch {
520548
newmino.move_pos(result.x, result.y);
521549
Environment::simple_rotate(Rotate::LEFT, &mut newmino, 0);
522550

523-
let mut temp = Action::ROTATE_LEFT as i64;
524-
for _i in 0..move_count {
525-
temp *= 10;
551+
let softdrop_value;
552+
if count_after_softdrop == -1 {
553+
softdrop_value = -1;
554+
} else {
555+
softdrop_value = count_after_softdrop + 1;
526556
}
527557

528558
Self::search(
529559
&mut newmino,
530560
&field,
531561
move_count + 1,
532-
move_value + temp,
562+
move_value + new_move_diff * Action::ROTATE_LEFT as i64,
533563
&before_eval,
534564
lock_direction,
535565
rotate_count + 1,
536566
move_flag,
567+
softdrop_value,
537568
);
538569
}
539570
}
571+
572+
//180回転
540573
}
541574

542575
///過去の位置を記録、参照
@@ -563,8 +596,9 @@ impl BeemSearch {
563596
result
564597
}
565598

599+
#[inline(always)]
566600
///位置情報を回転情報関係なく均一にする
567-
fn get_hash_for_position(kind: i8, rotation: i8, position: &i64) -> i64 {
601+
pub fn get_hash_for_position(kind: i8, rotation: i8, position: &i64) -> i64 {
568602
if rotation == Rotation::ZERO {
569603
return *position;
570604
}

src/environment.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! テトリスシミュレート環境
22
33
use core::panic;
4+
use once_cell::sync::Lazy;
45
use rand::prelude::*;
56

67
use crate::{beemsearch::BeemSearch, evaluation::Evaluation, WEIGHT};
@@ -580,6 +581,7 @@ impl Environment {
580581
Action::HARD_DROP => {
581582
self.set_mino();
582583
}
584+
583585
Action::SOFT_DROP => loop {
584586
if Self::check_valid_pos(&self.field, &self.now_mino, &Vector2::MY1, 0) {
585587
self.now_mino
@@ -588,6 +590,7 @@ impl Environment {
588590
break;
589591
}
590592
},
593+
591594
Action::HOLD => self.hold(),
592595

593596
_ => panic!("不明な型"),
@@ -899,7 +902,12 @@ impl Environment {
899902
let mut environment = Environment::new();
900903
environment.init();
901904

902-
WEIGHT.set(*weight);
905+
// let value = WEIGHT.get().unwrap();
906+
unsafe {
907+
*WEIGHT = *weight;
908+
}
909+
// WEIGHT.set(*weight).unwrap();
910+
903911
//検索
904912
//操作
905913
//終了条件確認(ライン消去数)

src/evaluation.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ impl Evaluation {
2020

2121
pub fn evaluate(field: &[bool], mino: &Mino, cleared_line: i32) -> f64 {
2222
let cleared_value;
23-
let weight = WEIGHT.get().unwrap();
23+
let weight;
24+
25+
unsafe {
26+
weight = *WEIGHT;
27+
}
2428

2529
match cleared_line {
2630
0 => cleared_value = 0.0,

src/main.rs

+36-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use environment::{Action, Environment};
1111
use evaluation::Evaluation;
1212
use geneticalgorithm::GeneticAlgorithm;
1313
use num_cpus;
14+
use once_cell::sync::Lazy;
1415
use once_cell::sync::OnceCell;
1516
use std::time::{Duration, Instant};
1617
use std::{
@@ -21,7 +22,11 @@ use std::{
2122
use threadpool::ThreadPool;
2223

2324
use crate::environment::MinoKind;
24-
pub static WEIGHT: OnceCell<[f64; Evaluation::WEIGHT_COUNT as usize]> = OnceCell::new();
25+
26+
pub static mut WEIGHT: Lazy<[f64; Evaluation::WEIGHT_COUNT as usize]> = Lazy::new(|| {
27+
let m = [0.0; Evaluation::WEIGHT_COUNT as usize];
28+
m
29+
});
2530
pub static THREAD_POOL: OnceCell<Mutex<ThreadPool>> = OnceCell::new();
2631

2732
//デバッグ用でスレッド数変えてる
@@ -33,22 +38,33 @@ fn main() {
3338
"スレッドプールの初期化失敗"
3439
);
3540

36-
assert!(
37-
WEIGHT
38-
.set([
39-
200.1597, 319.1632, -1149.735, 118.6968, 187.1296, -604.2106, -551.1594, -364.9467,
40-
-43.58047,
41-
])
42-
.is_ok(),
43-
"err"
44-
);
41+
unsafe {
42+
*WEIGHT = [
43+
-5628.7173895928445,
44+
-2450.9359534946434,
45+
12769.975461312846,
46+
-22231.72228143262,
47+
-11297.76613876014,
48+
4526.680276187824,
49+
-2796.0433150603876,
50+
492.1132806920541,
51+
3502.358375632634,
52+
];
53+
}
4554

46-
GeneticAlgorithm::learn();
55+
// GeneticAlgorithm::learn();
56+
let mut mino = Environment::create_mino_1(MinoKind::I);
57+
println!(
58+
"{}:{}",
59+
BeemSearch::get_hash_for_position(mino.mino_kind, mino.rotation, &mino.position),
60+
&mino.position
61+
);
62+
let mut buf = String::new();
63+
io::stdin().read_line(&mut buf).unwrap();
4764

4865
let mut environment = Environment::new();
4966
environment.init();
5067

51-
println!("何かキーを入力して検索を開始");
5268
let mut buf = String::new();
5369

5470
let mut timer;
@@ -76,6 +92,14 @@ fn main() {
7692
for _i in 0..count {
7793
environment.user_input((result % 10).try_into().unwrap());
7894
result /= 10;
95+
96+
print(
97+
&environment.get_field_ref(),
98+
&environment.now_mino,
99+
elapsed_time,
100+
);
101+
102+
thread::sleep_ms(500);
79103
}
80104
/*
81105
let key = getch(false).unwrap();

0 commit comments

Comments
 (0)