-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
1419 lines (1297 loc) · 62.1 KB
/
lib.rs
File metadata and controls
1419 lines (1297 loc) · 62.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::{collections::HashSet, collections::HashMap, iter::Peekable, str::Chars};
use std::cmp::min;
use std::iter::FromIterator;
use rand::{seq::SliceRandom, prelude::ThreadRng};
use rand::thread_rng;
use rayon::prelude::*;
pub type CardSet = HashSet<Card>;
pub type SimulationData = HashMap<Card, Vec<usize>>;
#[derive(Clone,Debug)]
struct FastSimulationData {
num_players: usize,
// First index is the card
// Second index is the player
// index of (card, player) count is
// card * num_players + player
data: Vec<usize>,
}
impl From<&FastSimulationData> for SimulationData {
fn from(data: &FastSimulationData) -> Self {
let mut sim_data = SimulationData::new();
for card in CardUtils::all_cards() {
let card_data = &data.data[(card as usize * data.num_players)..(card as usize * data.num_players + data.num_players)];
sim_data.insert(card, Vec::from(card_data));
}
return sim_data;
}
}
impl FastSimulationData {
fn new(engine: &ClueEngine) -> Self {
let mut data = vec![];
data.resize(engine.player_data.len() * CARD_LAST as usize, 0);
FastSimulationData {
num_players: engine.player_data.len(),
data
}
}
fn num_simulations(self: &FastSimulationData) -> usize {
return (&self.data[0..self.num_players]).iter().sum::<usize>();
}
#[allow(dead_code)]
fn get_card_data(self: &FastSimulationData, card: Card) -> &[usize] {
return &self.data[(card as usize * self.num_players)..(card as usize * self.num_players + self.num_players)];
}
#[allow(dead_code)]
fn get_entry_mut(self: &mut FastSimulationData, card: Card, player_index: usize) -> &mut usize {
return &mut self.data[(card as usize * self.num_players) + player_index];
}
fn increment_entry(self: &mut FastSimulationData, card: Card, player_index: usize) {
self.data[(card as usize * self.num_players) + player_index] += 1;
}
fn accumulate_from(self: &mut FastSimulationData, source: &FastSimulationData) {
for i in 0..self.data.len() {
self.data[i] += source.data[i];
}
}
}
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, FromPrimitive, Hash, Copy, Clone)]
pub enum Card {
// suspects
ProfessorPlum,
ColonelMustard,
MrGreen,
MissScarlet,
DrOrchid,
MrsPeacock,
// weapons
Knife,
Candlestick,
Revolver,
LeadPipe,
Rope,
Wrench,
// rooms
Hall,
Conservatory,
DiningRoom,
Kitchen,
Study,
Library,
Ballroom,
Lounge,
BilliardRoom
}
const CARD_LAST : i32 = (Card::BilliardRoom as i32) + 1;
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Hash, Copy, Clone)]
pub enum CardType {
Suspect = 0,
Weapon = 6,
Room = 12
}
//TODO - document these
#[derive(Ord, PartialOrd, Eq, PartialEq, Debug, Copy, Clone)]
enum UpdateEngineMode {
Minimal,
All
}
impl From<bool> for UpdateEngineMode {
fn from(b: bool) -> Self {
if b { UpdateEngineMode::All } else { UpdateEngineMode::Minimal }
}
}
pub struct CardUtils {
}
impl CardUtils {
// TODO - use TryFrom
pub fn card_from_char(ch: char) -> Result<Card, String> {
let index = ch as i32 - 'A' as i32;
if index < 0 || index >= CARD_LAST {
return Err(format!("Invalid card character '{}'", ch));
}
return FromPrimitive::from_i32(index).ok_or(format!("Invalid card character '{}'", ch));
}
// TODO - use From
pub fn char_from_card(card: Card) -> char {
let index = card as u8 + 'A' as u8;
return index as char;
}
pub fn card_type(card: Card) -> CardType {
let index = card as u8;
if index < CardType::Weapon as u8 {
return CardType::Suspect;
}
if index < CardType::Room as u8 {
return CardType::Weapon;
}
return CardType::Room;
}
pub fn all_cards() -> impl Iterator<Item=Card> {
return (0..CARD_LAST).map(|x| FromPrimitive::from_i32(x).unwrap());
}
pub fn all_card_types() -> impl Iterator<Item=&'static CardType> {
const ALL_CARD_TYPES: [CardType; 3] = [CardType::Suspect, CardType::Weapon, CardType::Room];
return ALL_CARD_TYPES.iter();
}
pub fn cards_of_type(card_type: CardType) -> impl Iterator<Item=Card> {
let int_range = match card_type {
CardType::Suspect => (CardType::Suspect as u8)..(CardType::Weapon as u8),
CardType::Weapon => (CardType::Weapon as u8)..(CardType::Room as u8),
CardType::Room => (CardType::Room as u8)..(CARD_LAST as u8),
};
return int_range.map(|x| FromPrimitive::from_u8(x).unwrap());
}
fn card_set_to_sorted_string(card_set: &CardSet) -> String {
let mut chars = card_set.into_iter().map(|card| CardUtils::char_from_card(*card)).collect::<Vec<char>>();
chars.sort();
return chars.into_iter().collect();
}
}
// https://wduquette.github.io/parsing-strings-into-slices/
/// The Tokenizer type.
#[derive(Clone,Debug)]
struct Tokenizer<'a> {
// The string being parsed.
input: &'a str,
// The starting index of the next character.
index: usize,
// The iterator used to extract characters from the input
chars: Peekable<Chars<'a>>,
}
impl<'a> Tokenizer<'a> {
/// Creates a new tokenizer for the given input.
pub fn new(input: &'a str) -> Self {
Self {
input,
index: 0,
chars: input.chars().peekable(),
}
}
/// Returns the next character and updates the index.
pub fn next(&mut self) -> Option<char> {
let ch = self.chars.next();
if let Some(c) = ch {
self.index += c.len_utf8();
}
ch
}
pub fn next_digit(&mut self) -> Result<u8, ()> {
return Ok(self.next().ok_or(())?.to_digit(10).ok_or(())? as u8);
}
// Returns the remainder of the input starting at the index.
pub fn as_str(&self) -> &str {
&self.input[self.index..]
}
/// Returns the next character without advancing
pub fn peek(&mut self) -> Option<&char> {
return self.chars.peek();
}
}
#[derive(Clone,Debug)]
pub struct PlayerData {
// A set of cards that the player is known to have
pub has_cards: CardSet,
// A set of cards that the player is known not to have
pub not_has_cards: CardSet,
// A list of clauses. Each clause is a set of cards, one of which
// the player is known to have.
pub possible_cards: Vec<CardSet>,
pub is_solution_player: bool,
// None means we don't know how many cards
pub num_cards: Option<u8>
}
impl PlayerData {
pub fn new(num_cards: Option<u8>, is_solution_player: bool) -> PlayerData {
return PlayerData {
has_cards: HashSet::new(),
not_has_cards: HashSet::new(),
possible_cards: vec!(),
is_solution_player,
num_cards
};
}
pub fn write_to_string(self: &PlayerData) -> String {
let mut s = String::from("");
let num_cards_to_write = self.num_cards.unwrap_or(0);
// Always write 0 instead of None for simplicity
s += &num_cards_to_write.to_string();
s += &CardUtils::card_set_to_sorted_string(&self.has_cards);
s += "-";
s += &CardUtils::card_set_to_sorted_string(&self.not_has_cards);
for possible_card_group in (&self.possible_cards).into_iter() {
s += "-";
s += &CardUtils::card_set_to_sorted_string(&possible_card_group);
}
s += ".";
return s;
}
pub fn has_card(self: &PlayerData, card: Card) -> Option<bool> {
if self.has_cards.contains(&card) {
return Some(true);
}
if self.not_has_cards.contains(&card) {
return Some(false);
}
return None;
}
pub fn eliminate_extraneous_clauses(self: &mut PlayerData) {
PlayerData::eliminate_extraneous_clauses_possible_cards(&mut self.possible_cards);
}
pub fn eliminate_extraneous_clauses_possible_cards(possible_cards: &mut Vec<CardSet>) {
let mut need_to_call_again = false;
// This is O(n^2), but hopefully there aren't too many of these
'outer: for i in 0..possible_cards.len() {
for j in (i+1)..possible_cards.len() {
let clause_1 = &possible_cards[i];
let clause_2 = &possible_cards[j];
if clause_1.is_subset(clause_2) {
// clause_2 is extraneous
possible_cards.remove(j);
need_to_call_again = true;
break 'outer;
}
else if clause_1.is_superset(clause_2) {
// clause_1 is extraneous
possible_cards.remove(i);
need_to_call_again = true;
break 'outer;
}
}
}
if need_to_call_again {
// The easiest way to check without messing up the loop is
// to start over, although it's kinda slow. But I don't
// expect there to be tons of extraneous clauses.
PlayerData::eliminate_extraneous_clauses_possible_cards(possible_cards);
}
}
}
#[derive(Debug, Clone)]
pub struct ClueEngine {
pub player_data: Vec<PlayerData>,
}
impl ClueEngine {
pub fn new(number_of_players: u8, number_of_cards_per_player: Option<&Vec<u8>>) -> Result<ClueEngine, String> {
let real_cards_per_player: &Vec<u8>;
let allocated_cards_per_player: Vec<u8>;
if let Some(vec) = number_of_cards_per_player {
real_cards_per_player = vec;
}
else {
allocated_cards_per_player = (0..number_of_players).map(|i| ClueEngine::number_of_player_cards(i, number_of_players)).collect();
real_cards_per_player = &allocated_cards_per_player;
}
if real_cards_per_player.len() != number_of_players as usize {
return Err(format!("Wrong number of cards in number_of_cards_per_player vector! (expected {}, got {})", number_of_players, real_cards_per_player.len()));
}
// There are CARD_LAST - 3 (actually 18) cards among the players.
if real_cards_per_player.iter().sum::<u8>() != CARD_LAST as u8 - 3 {
return Err(format!("Wrong total number of cards in number_of_cards_per_player! (expected {}, got {})", CARD_LAST - 3, real_cards_per_player.iter().sum::<u8>()));
}
let mut player_datas: Vec<PlayerData> = vec!();
for i in 0..(number_of_players + 1) {
let number_of_cards;
if i == number_of_players {
number_of_cards = 3 as u8;
}
else {
number_of_cards = real_cards_per_player[i as usize];
}
let player_data = PlayerData::new(Some(number_of_cards), i == number_of_players);
player_datas.push(player_data);
}
Ok(ClueEngine { player_data: player_datas })
}
pub fn number_of_real_players(self: &Self) -> usize {
// don't include the solution player
return self.player_data.len() - 1;
}
pub fn solution_player(self: &Self) -> &PlayerData {
&self.player_data[self.number_of_real_players()]
}
pub fn solution_player_mut(self: &mut Self) -> &mut PlayerData {
let index = self.number_of_real_players();
&mut self.player_data[index]
}
pub fn number_of_player_cards(player_index: u8, num_players: u8) -> u8 {
if player_index == num_players {
// The case file always has exactly 3 cards
return 3
}
// There are 18 cards among the players.
// There are CARD_LAST - 3 (actually 18) cards among the players.
let mut num_cards = (CARD_LAST - 3) as u8 / num_players; // Integer division
let leftovers = (CARD_LAST - 3) as u8 % num_players;
// Assume the earlier players get the extra cards
if player_index < leftovers {
num_cards += 1;
}
return num_cards as u8;
}
pub fn write_to_string(self: &ClueEngine) -> String {
let mut s = String::from("");
s += &(self.number_of_real_players()).to_string();
for player in self.player_data.iter() {
s += &player.write_to_string();
}
return s;
}
pub fn load_from_string(s: &str) -> Result<ClueEngine, String> {
let mut tokenizer = Tokenizer::new(s);
let number_of_players = tokenizer.next_digit().map_err(|_| String::from("Error - couldn't parse number of players!"))?;
// This can't fail because we're not specifying the number of cards, so they'll get set correctly.
let mut clue_engine = ClueEngine::new(number_of_players, None).unwrap();
for i in 0..(number_of_players+1) {
clue_engine.load_player_from_string(i as usize, &mut tokenizer)?;
}
// Ensure we've consumed all of the input
if tokenizer.peek() == None {
return Ok(clue_engine);
}
else {
return Err(format!("Didn't use all of string; the part that was left is \"{}\"", tokenizer.as_str()));
}
}
// format is (concatenated)
// <number of cards (or 0 if this is unknown)>
// one letter per card in has_cards
// '-'
// one letter per card in not_has_cards
// '-'
// one letter per card in possible_clauses
// (each possible_clause is separated by '-')
// '.'
fn load_player_from_string(self: &mut ClueEngine, player_index: usize, tokenizer: &mut Tokenizer) -> Result<(), String> {
const STRING_ENDED_ERROR: &str = "string ended unexpectedly!";
{
let num_cards = tokenizer.next_digit().map_err(|_| String::from(STRING_ENDED_ERROR))?;
(&mut self.player_data[player_index]).num_cards = if num_cards == 0 { None } else { Some(num_cards)};
}
// Load the list of cards this player has
while *tokenizer.peek().ok_or_else(|| String::from(STRING_ENDED_ERROR))? != '-' {
self.learn_info_on_card(player_index, CardUtils::card_from_char(tokenizer.next().ok_or(String::from(STRING_ENDED_ERROR))?)?, true, true);
}
// advance past the '-'
tokenizer.next();
// Load the list of cards this player doesn't have
{
let mut next_char = *tokenizer.peek().ok_or(String::from(STRING_ENDED_ERROR))?;
while next_char != '-' && next_char != '.' {
self.learn_info_on_card(player_index, CardUtils::card_from_char(tokenizer.next().ok_or(String::from(STRING_ENDED_ERROR))?)?, false, true);
next_char = *tokenizer.peek().ok_or(String::from(STRING_ENDED_ERROR))?;
}
}
// Load the list of clauses as long as it's not done
while tokenizer.next().ok_or(String::from(STRING_ENDED_ERROR))? != '.' {
let mut clause = HashSet::new();
let mut next_char = *tokenizer.peek().ok_or(String::from(STRING_ENDED_ERROR))?;
while next_char != '-' && next_char != '.' {
clause.insert(CardUtils::card_from_char(tokenizer.next().ok_or(String::from(STRING_ENDED_ERROR))?)?);
next_char = *tokenizer.peek().ok_or(String::from(STRING_ENDED_ERROR))?;
}
if !clause.is_empty() {
self.learn_has_one_of_cards(player_index, &clause);
}
}
Ok(())
}
pub fn learn_info_on_card(self: &mut ClueEngine, player_index: usize, card: Card, has_card: bool, update_engine: bool) -> CardSet {
let mut changed_cards = HashSet::new();
let update_mode = UpdateEngineMode::from(update_engine);
self.learn_info_on_card_internal(player_index, card, has_card, update_mode, &mut changed_cards);
return changed_cards;
}
fn learn_info_on_card_internal(self: &mut ClueEngine, player_index: usize, card: Card, has_card: bool, update_engine: UpdateEngineMode, changed_cards: &mut CardSet) {
{
let player = &mut self.player_data[player_index];
if has_card {
player.has_cards.insert(card);
}
else {
player.not_has_cards.insert(card);
}
changed_cards.insert(card);
self.examine_clauses(player_index, Some(card), changed_cards);
}
if update_engine == UpdateEngineMode::All {
self.check_solution(Some(card), changed_cards);
}
if has_card && self.player_data[player_index].is_solution_player {
// We know we have no other cards in this category.
for other_card in CardUtils::cards_of_type(CardUtils::card_type(card)) {
if other_card != card {
self.learn_info_on_card_internal(player_index, other_card, false, update_engine, changed_cards);
}
}
}
}
// Requires that all cards be assigned
// Also requires that we've already checked there's no overlap between
// has_cards and not_has_cards
fn is_consistent_after_all_cards_assigned(self: &mut ClueEngine) -> bool {
let mut cards_seen = CardSet::new();
for player in self.player_data.iter() {
for &card in player.has_cards.iter() {
if !cards_seen.insert(card) {
// Already seen this card in someone else's cards, so not consistent
return false;
}
}
if player.has_cards.len() != player.num_cards.unwrap() as usize {
// wrong number of cards
return false;
}
for clause in player.possible_cards.iter() {
if !clause.intersection(&player.has_cards).any(|_| true) {
// This clause is not satisfied
return false;
}
}
}
return true;
}
pub fn learn_has_one_of_cards(self: &mut ClueEngine, player_index: usize, cards: &CardSet) -> CardSet {
let mut changed_cards = HashSet::new();
self.learn_has_one_of_cards_internal(player_index, cards, &mut changed_cards);
return changed_cards;
}
fn learn_has_one_of_cards_internal(self: &mut ClueEngine, player_index: usize, cards: &CardSet, changed_cards: &mut CardSet) {
let mut clause_helpful = true;
let mut new_clause = HashSet::new();
for card in cards.iter() {
let has_card = self.player_data[player_index].has_card(*card);
match has_card {
Some(true) => {
// We already know player has one of these cards, so this
// clause is worthless.
clause_helpful = false;
},
Some(false) => {
// We know player doesn't have this card, so don't add this card
// to the new clause.
},
None => {
// Don't know; add it to the new clause
new_clause.insert(*card);
}
}
}
if clause_helpful && !new_clause.is_empty() {
if new_clause.len() == 1 {
// We have learned player has this card!
let new_card = *new_clause.iter().next().unwrap();
self.learn_info_on_card_internal(player_index, new_card, true, UpdateEngineMode::All, changed_cards);
} else {
self.player_data[player_index].possible_cards.push(new_clause);
}
self.examine_clauses(player_index, None, changed_cards);
}
}
pub fn learn_suggest(self: &mut ClueEngine, suggesting_player_index: usize, card1: Card, card2: Card, card3: Card, refuting_player_index: Option<usize>, card_shown: Option<Card>) -> CardSet {
let mut changed_cards = HashSet::new();
self.learn_suggest_internal(suggesting_player_index, card1, card2, card3, refuting_player_index, card_shown, &mut changed_cards);
return changed_cards;
}
fn learn_suggest_internal(self: &mut ClueEngine, suggesting_player_index: usize, card1: Card, card2: Card, card3: Card, refuting_player_index: Option<usize>, card_shown: Option<Card>, changed_cards: &mut CardSet) {
let mut current_player_index = suggesting_player_index + 1;
if current_player_index == self.number_of_real_players() as usize {
current_player_index = 0;
}
loop {
if refuting_player_index == Some(current_player_index) {
if let Some(real_card) = card_shown {
self.learn_info_on_card_internal(current_player_index, real_card, true, UpdateEngineMode::All, changed_cards);
} else {
let possible_cards = HashSet::from_iter(vec![card1, card2, card3].iter().map(|x| *x));
self.learn_has_one_of_cards_internal(current_player_index, &possible_cards, changed_cards);
}
self.check_solution(None, changed_cards);
return;
} else if current_player_index == suggesting_player_index {
// No one can refute this. We're done.
self.check_solution(None, changed_cards);
return;
} else {
self.learn_info_on_card_internal(current_player_index, card1, false, UpdateEngineMode::Minimal, changed_cards);
self.learn_info_on_card_internal(current_player_index, card2, false, UpdateEngineMode::Minimal, changed_cards);
self.learn_info_on_card_internal(current_player_index, card3, false, UpdateEngineMode::Minimal, changed_cards);
current_player_index += 1;
if current_player_index == self.number_of_real_players() as usize {
current_player_index = 0;
}
}
}
}
fn examine_clauses(self: &mut ClueEngine, player_index: usize, card: Option<Card>, changed_cards: &mut CardSet) {
self.player_data[player_index].eliminate_extraneous_clauses();
if let Some(real_card) = card {
let player = &mut self.player_data[player_index];
// Iterate over all the clauses, but since we might be removing
// things from the Vec, keep track of the current index manually.
let mut i: usize = 0;
while i < player.possible_cards.len() {
let clause = &mut player.possible_cards[i];
let mut skip_increment = false;
if clause.contains(&real_card) {
if player.has_cards.contains(&real_card) {
// We have this card, so this clause is done
player.possible_cards.remove(i);
// adjust loop counter
skip_increment = true;
}
else if player.not_has_cards.contains(&real_card) {
clause.remove(&real_card);
if clause.len() == 1 {
// We have this card!
let have_card = clause.iter().next().unwrap();
player.has_cards.insert(*have_card);
changed_cards.insert(*have_card);
player.possible_cards.remove(i);
// adjust loop counter
skip_increment = true;
}
}
}
if !skip_increment {
i += 1;
}
}
}
if let Some(number_of_cards) = self.player_data[player_index].num_cards {
if number_of_cards == self.player_data[player_index].has_cards.len() as u8 {
// All cards are accounted for.
for other_card in CardUtils::all_cards() {
if self.player_data[player_index].has_card(other_card) == None {
self.learn_info_on_card(player_index, other_card, false, true);
}
}
}
else if self.player_data[player_index].has_cards.len() + self.player_data[player_index].possible_cards.len() > (number_of_cards as usize) {
// We may be able to figure out something
let num_accounted_for = number_of_cards as isize - self.player_data[player_index].has_cards.len() as isize;
let card_in_any_clause: &CardSet = &self.player_data[player_index].possible_cards.iter().fold(
HashSet::new(),
|mut set, v| {set.extend(v.iter()); set});
for test_card in card_in_any_clause {
// See if we could have this card, by contradiction.
// Assume we don't have this card. Remove it from
// all clauses.
let new_clauses = Self::remove_card_from_clauses(&self.player_data[player_index].possible_cards, *test_card);
// See if it's possible to satisfy the rest of the clauses with one fewer card.
let is_possible = Self::can_satisfy(&new_clauses, num_accounted_for - 1);
if !is_possible {
// We found a contradiction if we don't have this card,
// so we must have this card.
self.learn_info_on_card_internal(player_index, *test_card, true, UpdateEngineMode::All, changed_cards);
}
}
}
}
}
pub fn transpose_clauses(possible_cards: &Vec<CardSet>) -> HashMap<Card, HashSet<usize>> {
let mut transposed_clauses: HashMap<Card, HashSet<usize>> = HashMap::new();
for i in 0..possible_cards.len() {
let clause = &possible_cards[i];
for card in clause.iter() {
if let Some(existing_clauses) = transposed_clauses.get_mut(card) {
existing_clauses.insert(i);
}
else {
let mut new_hash_set = HashSet::new();
new_hash_set.insert(i);
transposed_clauses.insert(*card, new_hash_set);
}
}
}
return transposed_clauses;
}
pub fn remove_card_from_clauses(clauses: &Vec<CardSet>, card: Card) -> Vec<CardSet> {
let mut new_clauses = vec!();
new_clauses.reserve(clauses.len());
for clause in clauses {
let mut new_clause = clause.clone();
new_clause.remove(&card);
new_clauses.push(new_clause);
}
return new_clauses;
}
// Returns whether there's a set of choices that can satisfy all these clauses,
// given we can only use up to num_accounted_for cards.
fn can_satisfy(clauses: &Vec<CardSet>, num_unaccounted_for: isize) -> bool {
if clauses.len() == 0 {
return true;
}
if num_unaccounted_for <= 0 {
return false;
}
// If there are any empty clauses we have a contradiction already.
let smallest_clause = clauses.iter().min_by_key(|x| x.len()).unwrap();
if smallest_clause.len() == 0 {
return false;
}
// See if there's any way we can satisfy these
// Try one card at a time
let card_clauses = ClueEngine::transpose_clauses(clauses);
for test_card in smallest_clause {
// First, remove all clauses containing this card.
let new_clauses = ClueEngine::remove_clauses_with_indices(clauses, card_clauses.get(test_card).unwrap());
// See if it's possible to satisfy the rest of the clauses with one fewer card.
if ClueEngine::can_satisfy(&new_clauses, num_unaccounted_for - 1) {
return true;
}
}
return false;
}
pub fn remove_clauses_with_indices(clauses: &Vec<CardSet>, indices_to_remove: &HashSet<usize>) -> Vec<CardSet> {
let mut new_clauses = vec!();
for i in 0..clauses.len() {
if !indices_to_remove.contains(&i) {
new_clauses.push(clauses[i].clone());
}
}
return new_clauses;
}
// Check if any cards are the solution, and also if any clauses are in common.
fn check_solution(self: &mut Self, card: Option<Card>, changed_cards: &mut CardSet) {
if let Some(real_card) = card {
self.check_for_all_players_but_one_dont_have_this_card(real_card, changed_cards);
}
for card_type in CardUtils::all_card_types() {
let all_cards = CardUtils::cards_of_type(*card_type).collect::<Vec<Card>>();
let mut solution_card: Option<Card> = None;
let mut is_solution = true;
for test_card in all_cards.iter() {
// See if anyone has this card
let card_owned = self.player_data.iter().any(|player| player.has_card(*test_card) == Some(true));
if !card_owned {
// If there's another possibility, we don't know which is
// right.
if solution_card != None {
solution_card = None;
is_solution = false;
} else {
solution_card = Some(*test_card);
}
}
}
if is_solution && solution_card != None {
// There's only one possibility, so this must be it!
let solution = solution_card.unwrap();
if self.solution_player().has_card(solution) == None {
// also check to make sure we don't have another one in this category
// (if this happened, we're inconsistent already, just move on)
if all_cards.iter().all(|c| !self.solution_player().has_cards.contains(c)) {
self.solution_player_mut().has_cards.insert(solution);
changed_cards.insert(solution);
}
}
}
}
// Finally, see if any people share clauses in common.
self.check_for_overlapping_clauses(changed_cards);
}
fn check_for_overlapping_clauses(self: &mut Self, changed_cards: &mut CardSet) {
let mut clause_hash: HashMap<String, Vec<usize>> = HashMap::new();
for idx in 0..self.number_of_real_players() {
let player = &self.player_data[idx as usize];
for clause in player.possible_cards.iter() {
let clause_str = CardUtils::card_set_to_sorted_string(clause);
if clause_hash.contains_key(&clause_str) {
clause_hash.get_mut(&clause_str).unwrap().push(idx);
}
else {
clause_hash.insert(clause_str, vec![idx]);
}
}
}
for (clause, players) in clause_hash.iter() {
// If n people all have an n-length clause, no one else can have
// a card in that clause.
if clause.len() <= players.len() {
let affected_people: HashSet<usize> = HashSet::from_iter(players.iter().map(|x| *x));
for card in clause.chars().map(|ch| CardUtils::card_from_char(ch).unwrap()) {
changed_cards.insert(card);
}
for idx in 0..(self.number_of_real_players() + 1) {
if !affected_people.contains(&idx) {
for card in clause.chars().map(|ch| CardUtils::card_from_char(ch).unwrap()) {
if self.player_data[idx as usize].has_card(card) != Some(false) {
self.learn_info_on_card_internal(idx as usize, card, false, UpdateEngineMode::Minimal, changed_cards);
}
}
}
}
}
}
}
fn check_for_all_players_but_one_dont_have_this_card(self: &mut Self, card: Card, changed_cards: &mut CardSet) {
let mut someone_has_card = false;
let mut number_who_dont_have_card = 0;
let mut player_who_might_have_card = None;
// - Check also for all cards except one in a category are
// accounted for.
for i in 0..self.player_data.len() {
let player = &mut self.player_data[i];
let has_card = player.has_card(card);
match has_card {
Some(true) => {
// Someone has the card, so the solution is not this.
someone_has_card = true;
break;
},
Some(false) => {
number_who_dont_have_card += 1;
},
None => {
// We only look at this if there's only one person who could have this card.
player_who_might_have_card = Some(i);
}
}
}
if !someone_has_card && number_who_dont_have_card == self.number_of_real_players() {
// Every player except one doesn't have this card, so we know the player has it.
self.learn_info_on_card_internal(player_who_might_have_card.unwrap(), card, true, UpdateEngineMode::Minimal, changed_cards);
}
else if someone_has_card {
// Someone has this card, so no one else does. (including solution)
for i in 0..self.player_data.len() {
let player = &self.player_data[i];
if player.has_card(card) == None {
self.learn_info_on_card_internal(i, card, false, UpdateEngineMode::Minimal, changed_cards);
}
}
}
}
pub fn do_simulation(self: &Self, random_solutions: bool) -> (SimulationData, i32) {
let num_simulations: i32 = if random_solutions {100000} else {20000};
const SIMULATION_IN_PARALLEL: bool = true;
const NUM_SIMULATIONS_TO_SPLIT: i32 = 1000;
if self.player_data.iter().any(|player| player.num_cards == None) {
// Can't do simulations if we don't know how many cards everyone has
let mut simulation_data = SimulationData::new();
self.initialize_simulation_data(&mut simulation_data);
return (simulation_data, 0);
}
let mut fast_simulation_data = FastSimulationData::new(self);
// Find a solution to simulate.
// FFV - this iteration could be more generalized
let mut solution_possibilities: HashMap<CardType, Vec<Card>> = HashMap::new();
let solution_cards = &self.player_data[self.number_of_real_players()].has_cards;
let not_solution_cards = &self.player_data[self.number_of_real_players()].not_has_cards;
for card_type in CardUtils::all_card_types() {
let mut already_found_solution_iter = solution_cards.iter().filter(|&card| CardUtils::card_type(*card) == *card_type);
let already_found_solution = already_found_solution_iter.next();
if already_found_solution != None {
// We know what the solution is for this card already
solution_possibilities.insert(*card_type, vec![*(already_found_solution.unwrap())]);
}
else {
// Take all possible cards, except for the ones we know aren't
// solutions
let all_possible_cards = CardUtils::cards_of_type(*card_type).collect::<HashSet<Card>>();
solution_possibilities.insert(*card_type, all_possible_cards.iter().filter_map(|&card| if not_solution_cards.contains(&card) {None } else {Some(card)}).collect());
}
}
let number_of_solutions = solution_possibilities.values().map(|cards| cards.len() as i32).product::<i32>();
let iterations_per_solution = num_simulations / number_of_solutions;
let mut solution_engines: Vec<(ClueEngine, CardSet, i32)> = vec![];
if random_solutions {
let mut available_cards: CardSet = CardUtils::all_cards().collect();
for player in self.player_data.iter() {
for has_card in player.has_cards.iter() {
available_cards.remove(has_card);
}
}
let mut rng = thread_rng();
for _ in 0..num_simulations {
let mut engine_copy = self.clone();
let mut available_card_vec = available_cards.clone();
// TODO - make sure these aren't empty
let card1 = solution_possibilities.get(&CardType::Suspect).unwrap().choose(&mut rng).unwrap();
let card2 = solution_possibilities.get(&CardType::Weapon).unwrap().choose(&mut rng).unwrap();
let card3 = solution_possibilities.get(&CardType::Room).unwrap().choose(&mut rng).unwrap();
available_card_vec.remove(card1);
available_card_vec.remove(card2);
available_card_vec.remove(card3);
let available_card_vec = available_card_vec.iter().collect::<Vec<&Card>>();
// Call the internal versions to avoid a few allocations
let mut ignored_changed_cards = CardSet::new();
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card1, true, UpdateEngineMode::All, &mut ignored_changed_cards);
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card2, true, UpdateEngineMode::All, &mut ignored_changed_cards);
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card3, true, UpdateEngineMode::All, &mut ignored_changed_cards);
if ClueEngine::do_one_simulation(&mut engine_copy, &available_card_vec, &mut rng) {
// Results were consistent, so count them
for player_index in 0..engine_copy.player_data.len() {
for card in engine_copy.player_data[player_index].has_cards.iter() {
fast_simulation_data.increment_entry(*card, player_index);
}
}
}
}
return (SimulationData::from(&fast_simulation_data), num_simulations);
}
else {
for card1 in solution_possibilities.get(&CardType::Suspect).unwrap() {
for card2 in solution_possibilities.get(&CardType::Weapon).unwrap() {
for card3 in solution_possibilities.get(&CardType::Room).unwrap() {
let mut engine_copy = self.clone();
// To avoid solution biasing, we need to gather the available_cards before we put in the solution.
// Otherwise see the test test_simulation_monty_hall_no_player0
// In that case, ProfessorPlum only has two possibilities, and once we pick it (or something else)
// for the solution it automatically goes to the other player.
// But we should be throwing out a lot of those simulations.
let mut available_cards: CardSet = CardUtils::all_cards().collect();
for player in engine_copy.player_data.iter() {
for has_card in player.has_cards.iter() {
available_cards.remove(has_card);
}
}
available_cards.remove(card1);
available_cards.remove(card2);
available_cards.remove(card3);
// Call the internal versions to avoid a few allocations
let mut ignored_changed_cards = CardSet::new();
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card1, true, UpdateEngineMode::All, &mut ignored_changed_cards);
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card2, true, UpdateEngineMode::All, &mut ignored_changed_cards);
engine_copy.learn_info_on_card_internal(engine_copy.number_of_real_players(), *card3, true, UpdateEngineMode::All, &mut ignored_changed_cards);
if SIMULATION_IN_PARALLEL {
// Don't split on just cards, because if there are only a few solution possibilities
// we won't get good parallelism.
let mut temp_iterations_per_solution = iterations_per_solution;
while temp_iterations_per_solution > 0 {
solution_engines.push((engine_copy.clone(), available_cards.clone(), min(NUM_SIMULATIONS_TO_SPLIT, temp_iterations_per_solution)));
temp_iterations_per_solution -= NUM_SIMULATIONS_TO_SPLIT;
}
}
else {
solution_engines.push((engine_copy, available_cards, iterations_per_solution));
}
}
}
}
let simulations_per_iteration: i32 = solution_engines.iter().map(|data| data.2).sum();
let total_number_of_simulations;
if SIMULATION_IN_PARALLEL {
let mut iterations = 0;
const MAX_ITERATIONS: i32 = 100;
while iterations < MAX_ITERATIONS && fast_simulation_data.num_simulations() < 1000 {
iterations += 1;
let results: Vec<FastSimulationData> = solution_engines.par_iter().map(|solution_data| {
let mut local_simulation_data = FastSimulationData::new(self);
let engine = &solution_data.0;
let available_cards = &solution_data.1;
let iterations = solution_data.2;
Self::gather_simulation_data(&mut local_simulation_data, &engine, available_cards, iterations);
local_simulation_data
}).collect();
for result in results {
fast_simulation_data.accumulate_from(&result);
}
}
total_number_of_simulations = iterations * simulations_per_iteration;
}
else {
let mut iterations = 0;
const MAX_ITERATIONS: i32 = 100;
while iterations < MAX_ITERATIONS && fast_simulation_data.num_simulations() < 1000 {
iterations += 1;
for (engine, available_cards, iterations) in &solution_engines {
Self::gather_simulation_data(&mut fast_simulation_data, &engine, &available_cards, *iterations);
}
}
total_number_of_simulations = iterations * simulations_per_iteration;
}
return (SimulationData::from(&fast_simulation_data), total_number_of_simulations);
}
}
// Note that we do at least 20,000 of these, so performance is very important!
fn gather_simulation_data(simulation_data: &mut FastSimulationData, engine: &ClueEngine, available_cards: &CardSet, iterations: i32) {
let available_card_vec = available_cards.iter().collect::<Vec<&Card>>();
let mut rng = thread_rng();
for _ in 0..iterations {
let mut temp_engine = engine.clone();
if ClueEngine::do_one_simulation(&mut temp_engine, &available_card_vec, &mut rng) {
// Results were consistent, so count them
for player_index in 0..temp_engine.player_data.len() {
for card in temp_engine.player_data[player_index].has_cards.iter() {
simulation_data.increment_entry(*card, player_index);
}
}
}
}
}