git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
find minimum expression for a function
commitc1eaf41ffdbc0a89ba0b44ffe2085a5a83c40b2c authorIlya Gulkov <ifranked@gmail.com> dateTue 29 Sep 2015, 20:49 (11 years ago) parent7f59cd0
1 file changed+48−12
SolveAvdoshin/BooleanFunctions.cs+48−12
SolveAvdoshin/BooleanFunctions.cs+48−12
@@ -35,24 +35,51 @@ namespace SolveAvdoshin
3535// Console.WriteLine();
3636// }
3737
38− BooleanOperation[] ops = { BooleanOperation.And, BooleanOperation.Or };
38+ BooleanOperation[] ops = { BooleanOperation.Zero, BooleanOperation.NOR, BooleanOperation.NotBImp, BooleanOperation.NotA, BooleanOperation.NotImp, BooleanOperation.NotB, BooleanOperation.Xor, BooleanOperation.NAND, BooleanOperation.And, BooleanOperation.Eq, BooleanOperation.B, BooleanOperation.Imp, BooleanOperation.A, BooleanOperation.BImp, BooleanOperation.Or, BooleanOperation.One };
3939 BooleanVariable[] vars = { BooleanVariable.A, BooleanVariable.B, BooleanVariable.C };
4040
41− foreach(var v in BooleanExpression.AllExpressions(3, ops, vars)) {
42− Console.WriteLine(v.ToString());
43− }
41+// foreach(var v in BooleanExpression.AllExpressions(3, ops, vars)) {
42+// Console.WriteLine(v.ToString());
43+// }
44+
45+// BooleanExpression ex = new OpExpression(BooleanOperation.And, BooleanVariable.A,
46+// new OpExpression(BooleanOperation.And, BooleanVariable.B, BooleanVariable.C));
47+//
48+// Console.WriteLine(ex);
49+//
50+// Console.WriteLine((new BooleanFunction(ex)).ToString("L"));
51+// Console.WriteLine((new BooleanFunction(ex)).Equals(new BooleanFunction(1)));
52+
53+ var ex = new BooleanFunction(26).MininalExpressionInBasis(ops, vars);
54+//
55+ Console.WriteLine(ex.ToString());
4456 }
4557 }
4658
4759 class BooleanFunction // TODO: Make private ?
4860 {
49− int TruthTable;
61+ protected int TruthTable;
5062
5163 public BooleanFunction(int n)
5264 {
5365 TruthTable = n;
5466 }
5567
68+ public BooleanFunction(BooleanExpression ex)
69+ {
70+ TruthTable = 0;
71+
72+ for(int i = 0; i < 8; i++) {
73+ TruthTable *= 2;
74+ TruthTable += ex.Eval((i >> 2) & 1, (i >> 1) & 1, i & 1);
75+ }
76+ }
77+
78+ public bool Equals(BooleanFunction that)
79+ {
80+ return this.TruthTable == that.TruthTable;
81+ }
82+
5683 public bool Eval(bool a, bool b, bool c)
5784 {
5885 return (TruthTable >> (7 - (a ? 4 : 0) - (b ? 2 : 0) - (c ? 1 : 0)) & 1) == 1;
@@ -144,15 +171,19 @@ namespace SolveAvdoshin
144171 return ex;
145172 }
146173
147− public BooleanExpression MininalExpressionInBasis()
174+ public BooleanExpression MininalExpressionInBasis(BooleanOperation[] ops, BooleanVariable[] vars)
148175 {
149− throw new NotImplementedException();
176+ for(int i = 2; i < 7; i++) {
177+ Console.WriteLine("Trying {0}...", i);
150178
151−// for(int i = 2; i < 15; i++) {
152−// foreach(var ex in BooleanExpression.AllExpressions(i)) {
153−//
154−// }
155−// }
179+ foreach(var ex in BooleanExpression.AllExpressions(i, ops, vars)) {
180+ if((new BooleanFunction(ex)).Equals(this)) {
181+ return ex;
182+ }
183+ }
184+ }
185+
186+ throw new Exception("Minimal expression for function " + this.ToString("S") + " couldn't be found");
156187 }
157188 }
158189
@@ -167,6 +198,11 @@ namespace SolveAvdoshin
167198 abstract public void SetIthVar(int i, BooleanVariable value);
168199 abstract public int setIthOp(int i, BooleanOperation value);
169200
201+ public int Eval(int a, int b, int c)
202+ {
203+ return Eval(a == 1, b == 1, c == 1) ? 1 : 0;
204+ }
205+
170206 public static IEnumerable<BooleanExpression> AllTrees(int size)
171207 {
172208 if(size == 0) {