Skip to content

Commit

Permalink
Improvements in interface
Browse files Browse the repository at this point in the history
  • Loading branch information
NoraCodes committed Apr 9, 2018
1 parent e4873c5 commit 7bda179
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions sample-configs/countup-configurable.toml
@@ -0,0 +1,6 @@
mutations_per_generation = 3
initial_program_length = 8
max_runtime = 128
population_size = 32
inputs = [ [1,3], [2,5], [999,6]]
targets = [[1,2,3], [2,3,4,5,6], [999,1000,1001,1002,1003,1004]]
6 changes: 6 additions & 0 deletions sample-configs/sum.toml
@@ -0,0 +1,6 @@
mutations_per_generation = 3
initial_program_length = 8
max_runtime = 128
population_size = 64
inputs = [[1, 2], [1, 2, 3], [5, 5, 22, 10], [2, 6, 1, 1, 0, 1], [8, 1]]
targets = [[3], [6], [42], [11], [9]]
4 changes: 2 additions & 2 deletions src/main.rs
Expand Up @@ -70,14 +70,14 @@ fn main() {

// Report a successful program
println!("Program found after {} generations.", tries);
println!("{}", p.program_as_string());
// Report every input
for i in 0..config.inputs.len() {
let res = sbrain::fixed_evaluate(&(p.program_as_string()), Some(config.inputs[i].clone()), Some(config.max_runtime as u32));
println!("Ran for {} cycles and {} halt\n{:?} -> {} -> {:?}",
println!("Ran for {} cycles and {} halt\n{:?} -> {:?}",
res.cycles,
if res.halted {"did"} else {"did not"},
config.inputs[i],
p.program_as_string(),
res.output
);
}
Expand Down
19 changes: 12 additions & 7 deletions src/mutate.rs
Expand Up @@ -21,23 +21,25 @@ pub fn mutate_program(mut program: Program, cfg: &Configuration) -> Program {
// Select the right set of symbols.
let symbols = if cfg.is_legacy() { BF_SYMBOLS } else { SB_SYMBOLS };

let mutation_type = s.gen_range(0, 3); // HACK! Make enum and match

// Select the type of mutation to perform.
let mutation_type = s.gen_range(0, 3);
match mutation_type {
0 => {
let target_index: usize = s.gen_range(0, program.len());
program[target_index] = *s.choose(symbols).unwrap();
}
1 => { program.insert(s.gen_range(0, program_len), *s.choose(symbols).unwrap()); }
2 => { program.remove(s.gen_range(0, program_len)); }
_ => {}
_ => {unreachable!()}
}
}
program
}

pub fn mutate_population(population: Population, cfg: &Configuration) -> UncostedPopulation {
// Reserve one for the best and one for fresh blood
let empty_slots = population.len() - 2;
// Reserve one for the best and four for fresh blood
let empty_slots = population.len() - 5;
// Create buffer and iterator
let mut new_population = Vec::with_capacity(population.len());
let best_program = population[0].clone().program;
Expand Down Expand Up @@ -66,7 +68,10 @@ pub fn mutate_population(population: Population, cfg: &Configuration) -> Uncoste
).count();

// Now fresh blood
new_population.push(generate_random_program(cfg));
for _ in 0..4 {
new_population.push(generate_random_program(cfg));
}
// Free_mut additional population
if cfg.is_free_mut() { new_population.push(generate_random_program(cfg)); }
new_population
}
Expand All @@ -82,7 +87,7 @@ fn cross_programs(mut a: Program, mut b: Program) -> (Program, Program) {

let mut rand = rand::thread_rng();
// Generate a section to pull and replace
let upper_bound: usize = rand.gen_range(0, (min_length - 1)) + 1;
let upper_bound: usize = rand.gen_range(0, min_length - 1) + 1;
let lower_bound: usize = rand.gen_range(0, upper_bound);

assert!(upper_bound < min_length, "Upper bound is greater than the minimum length");
Expand All @@ -93,4 +98,4 @@ fn cross_programs(mut a: Program, mut b: Program) -> (Program, Program) {
}

(a, b)
}
}

0 comments on commit 7bda179

Please sign in to comment.