Output of specific solution

This commit is contained in:
Arne Keller 2021-03-15 19:17:03 +01:00
parent d130341176
commit 24139814cc
3 changed files with 31 additions and 17 deletions

View File

@ -1,3 +1,5 @@
use std::env::args;
use svg_fmt::*; use svg_fmt::*;
use kv::*; use kv::*;
@ -14,6 +16,8 @@ const COLORS: &'static [Color] = &[
]; ];
fn main() { fn main() {
let args = args().collect::<Vec<_>>();
let vars = [A, B, C]; let vars = [A, B, C];
let function = vec![0, 0, 1, 1, 1, 1, 1, 0].into_iter().map(Into::into).collect(); let function = vec![0, 0, 1, 1, 1, 1, 1, 0].into_iter().map(Into::into).collect();
let groups = find_groups(&function, &vars, One); let groups = find_groups(&function, &vars, One);
@ -22,25 +26,33 @@ fn main() {
eprintln!("{}", print_implicant(x)); eprintln!("{}", print_implicant(x));
} }
eprintln!("prime:"); eprintln!("prime:");
let prime = find_prime(&function, &vars, One, &groups); let (prime, other) = find_prime(&function, &vars, One, &groups);
for x in &prime { for x in &prime {
eprintln!("{}", print_implicant(x)); eprintln!("{}", print_implicant(x));
} }
eprintln!("solutions:"); eprintln!("solutions:");
let solutions = all_solutions(function.clone(), &vars, One, &prime, &groups); let solutions = all_solutions(function.clone(), &vars, One, &prime, &other);
for sol in solutions { for sol in &solutions {
for x in &prime { for x in &prime {
eprint!("{} | ", print_implicant(x)); eprint!("{} | ", print_implicant(x));
} }
for x in &sol { for x in sol {
eprint!("{} | ", print_implicant(x)); eprint!("{} | ", print_implicant(x));
} }
eprintln!(); eprintln!();
} }
let block_masks = groups.iter() let block_masks: Vec<_> = if let Some(Ok(index)) = args.get(1).map(|x| x.parse::<usize>()) {
.map(block_to_mask) prime.iter()
.collect::<Vec<_>>(); .map(block_to_mask)
.chain(solutions[index].iter().map(block_to_mask))
.collect()
} else {
prime.iter()
.map(block_to_mask)
.chain(other.iter().map(block_to_mask))
.collect()
};
let (grid, w, h) = grid(3); let (grid, w, h) = grid(3);
println!("{}", BeginSvg { w: w * SIZE_FACTOR + 3, h: h * SIZE_FACTOR + 3 }); println!("{}", BeginSvg { w: w * SIZE_FACTOR + 3, h: h * SIZE_FACTOR + 3 });

View File

@ -1,4 +1,4 @@
use std::fmt::{self, Display}; use std::fmt;
use itertools::Itertools; use itertools::Itertools;
@ -99,7 +99,7 @@ pub fn find_groups(func: &FunctionSpec, vars: &[Variable], typ: Output) -> Vec<V
groups groups
} }
pub fn find_prime<'a>(func: &FunctionSpec, vars: &[Variable], typ: Output, blocks: &'a [Block]) -> Vec<&'a [(Variable, Output)]> { pub fn find_prime<'a>(func: &FunctionSpec, vars: &[Variable], typ: Output, blocks: &'a [Block]) -> (Vec<&'a [(Variable, Output)]>, Vec<&'a [(Variable, Output)]>) {
assert_eq!(func.len(), 2usize.pow(vars.len() as u32)); assert_eq!(func.len(), 2usize.pow(vars.len() as u32));
let block_masks = blocks.iter() let block_masks = blocks.iter()
@ -125,10 +125,12 @@ pub fn find_prime<'a>(func: &FunctionSpec, vars: &[Variable], typ: Output, block
prime[i] = true; prime[i] = true;
} }
} }
prime.into_iter().enumerate().filter(|&(_, prime)| prime).map(|(i, _)| &*blocks[i]).collect() let a = prime.iter().enumerate().filter(|&(_, &prime)| prime).map(|(i, _)| &*blocks[i]).collect();
let b = prime.iter().enumerate().filter(|&(_, &prime)| !prime).map(|(i, _)| &*blocks[i]).collect();
(a, b)
} }
pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, prime: &[BlockRef], other: &[Block]) -> Vec<Vec<Block>> { pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, prime: &[BlockRef], other: &[BlockRef]) -> Vec<Vec<Block>> {
assert_eq!(func.len(), 2usize.pow(vars.len() as u32)); assert_eq!(func.len(), 2usize.pow(vars.len() as u32));
// first mark all inputs covered by prime blocks as Any // first mark all inputs covered by prime blocks as Any
@ -153,7 +155,7 @@ pub fn all_solutions(mut func: FunctionSpec, vars: &[Variable], typ: Output, pri
all_recursive(func, vars, typ, other, &other_masks) all_recursive(func, vars, typ, other, &other_masks)
} }
fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[Block], other_masks: &[(Variable, Variable)]) -> Vec<Vec<Block>> { fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[BlockRef], other_masks: &[(Variable, Variable)]) -> Vec<Vec<Block>> {
let mut all = Vec::new(); let mut all = Vec::new();
for i in 0..other.len() { for i in 0..other.len() {
let block = &other[i]; let block = &other[i];
@ -173,10 +175,10 @@ fn all_recursive(func: FunctionSpec, vars: &[Variable], typ: Output, other: &[Bl
} }
let extensions = all_recursive(func, vars, typ, other, other_masks); let extensions = all_recursive(func, vars, typ, other, other_masks);
if extensions.is_empty() { if extensions.is_empty() {
all.push(vec![block.clone()]); all.push(vec![block.to_vec()]);
} }
for mut extension in extensions { for mut extension in extensions {
extension.push(block.clone()); extension.push(block.to_vec());
all.push(extension); all.push(extension);
} }
} }
@ -253,7 +255,7 @@ fn test_3var() {
vec![(A, One), (C, Zero)], vec![(A, One), (C, Zero)],
vec![(B, Zero), (C, Zero)], vec![(B, Zero), (C, Zero)],
]); ]);
let prime = find_prime(&fun, &mut [A, B, C], Zero, &groups); let prime = find_prime(&fun, &mut [A, B, C], Zero, &groups).0;
assert_eq!(prime, vec![ assert_eq!(prime, vec![
vec![(A, Zero), (B, Zero)], vec![(A, Zero), (B, Zero)],
vec![(A, One), (C, Zero)], vec![(A, One), (C, Zero)],

View File

@ -8,12 +8,12 @@ fn main() {
println!("{}", print_implicant(x)); println!("{}", print_implicant(x));
} }
println!("prime:"); println!("prime:");
let prime = find_prime(&function, &[A, B, C], One, &groups); let (prime, other) = find_prime(&function, &[A, B, C], One, &groups);
for x in &prime { for x in &prime {
println!("{}", print_implicant(x)); println!("{}", print_implicant(x));
} }
println!("solutions:"); println!("solutions:");
let solutions = all_solutions(function, &[A, B, C], One, &prime, &groups); let solutions = all_solutions(function, &[A, B, C], One, &prime, &other);
for sol in solutions { for sol in solutions {
for x in &prime { for x in &prime {
print!("{} | ", print_implicant(x)); print!("{} | ", print_implicant(x));