arc_agi/
experiments.rs

1//use std::collections::{BTreeSet, BTreeMap};
2//use crate::cats::*;
3//use crate::rules::*;
4//use crate::data::{dir, Data};
5use std::panic::RefUnwindSafe;
6use crate::examples::*;
7use crate::grid::*;
8//use crate::shape::*;
9use crate::cats::Colour;
10use crate::cats::Transformation;
11//use crate::cats::Transformation::NoTrans;
12
13pub fn check_examples(ex: &Examples, func: &dyn Fn(&Example) -> bool) -> bool {
14    for e in &ex.examples {
15        if !func(e) {
16            return false;
17        }
18    }
19
20    true
21}
22
23pub fn check_input_grids(ex: &Examples, func: &dyn Fn(&Grid) -> bool) -> bool {
24    for e in &ex.examples {
25        if !func(&e.input.grid) {
26            return false;
27        }
28    }
29
30    true
31}
32
33pub fn check_output_grids(ex: &Examples, func: &dyn Fn(&Grid) -> bool) -> bool {
34    for e in &ex.examples {
35        if !func(&e.output.grid) {
36            return false;
37        }
38    }
39
40    true
41}
42
43/*
44pub fn experiment_grid(ex: &Examples, len: usize, func: &dyn Fn(&Grid, &Grid, usize) -> Grid) -> Grid {
45    let mut n = 0;
46
47    for e in &ex.examples {
48        let grid = &e.input.grid;
49        let target = &e.output.grid;
50
51        loop {
52            if n >= len {
53                return Grid::trivial();
54            } else {
55                if func(&grid, &target, n).equals(target) != Colour::Same {
56                    n += 1;
57                } else {
58                    break;
59                }
60            }
61        }
62    }
63
64    func(&ex.test.input.grid, &ex.examples[0].output.grid, n)
65}
66*/
67
68pub fn experiment_grid(ex: &Examples, file: &str, experiment: usize, _trans: Transformation, func: &(dyn Fn(&Grid, &Grid, &mut usize) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
69    let mut n = usize::MAX;
70
71    for (attempts, e) in ex.examples.iter().enumerate() {
72        let grid = &e.input.grid;
73        let target = &e.output.grid;
74
75        if target.size() == 0 {
76            return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
77        }
78
79        loop {
80            if n == 0 {
81                return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
82            } else {
83                let ans = func(grid, target, &mut n);
84                /*
85                let ans = if trans == NoTrans {
86                    ans
87                } else {
88                    ans.inverse_transform(trans)
89                };
90                */
91                if ans.equals(target) != Colour::Same {
92                    if n == usize::MAX {
93                        if attempts > 0 && (experiment < 100000 || attempts > 1) {
94                            println!("{file} {experiment:<4}: {attempts} worked out of {}", ex.examples.len());
95                        }
96                        return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
97                    }
98
99                    n -= 1;
100                } else {
101                    break;
102                }
103            }
104        }
105    }
106
107    // if experiments work then run tests
108    ex.tests.iter().map(|test| {
109        let ans = func(&test.input.grid, &ex.examples[0].output.grid, &mut n);
110
111        /*
112        if trans == NoTrans {
113            ans
114        } else {
115            ans.inverse_transform(trans)
116        }
117        */
118        ans
119    })
120    .collect()
121}
122
123pub fn experiment_example(ex: &Examples, file: &str, experiment: usize, _trans: Transformation, func: &(dyn Fn(&Example) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
124    for (attempts, e) in ex.examples.iter().enumerate() {
125        //if attempts < 1 { continue; }
126        let target = &e.output.grid;
127        let ans = func(e);
128
129        /*
130        let ans = if trans == NoTrans {
131            ans
132        } else {
133            ans.inverse_transform(trans)
134        };
135        */
136//ans.show();
137
138        if ans == Grid::trivial() || ans.equals(target) != Colour::Same {
139            if attempts > 0 && (experiment < 100000 || attempts > 1) {
140                println!("{file} {experiment:<4}: {attempts} worked out of {}", ex.examples.len());
141            }
142            return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
143        }
144    }
145    
146    // if experiments work then run tests
147    ex.tests.iter().map(|test| {
148        let ans = func(test);
149
150        /*
151        if trans == NoTrans {
152            ans
153        } else {
154            ans.inverse_transform(trans)
155        }
156        */
157        ans
158    })
159    .collect()
160}
161
162pub fn experiment_colours(ex: &Examples, func: &(dyn Fn(&Example, Colour) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
163    const COLOURS: &[Colour]  = Colour::base_colours();
164
165    'outer:
166    for colour in COLOURS {
167        for e in ex.examples.iter() {
168            let target = &e.output.grid;
169            let ans = func(e, *colour);
170
171            if ans == Grid::trivial() || ans.equals(target) != Colour::Same {
172                continue 'outer;
173            }
174        }
175        
176        // if experiments work then run tests
177        let ans = ex.tests.iter().map(|test| {
178            let ans = func(test, *colour);
179
180            ans
181        })
182        .collect();
183
184        return ans;
185    }
186
187    ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>()
188}