git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
Function minimisation

also: improved OpExpr.ToString, changed op strings

commit6a8ca00e5242a229895c2d2ac7a730e01c4e4c37 authorIlya Gulkov <ifranked@gmail.com> dateTue 29 Sep 2015, 22:00 (11 years ago) parentc1eaf41
2 files changed+115−61
SolveAvdoshin/BooleanFunctions.cs+109−57 SolveAvdoshin/SolveAvdoshin.cs+6−4
SolveAvdoshin/BooleanFunctions.cs+109−57
@@ -5,54 +5,84 @@ namespace SolveAvdoshin
55{
66 public static class BooleanFunctions
77 {
8− public static void Main()
9− {
10−// Console.WriteLine(ex.ToString());
11−// Console.WriteLine(ex.Eval(true, true, true));
12−
13−// var f = new BooleanFunction(134);
14−//
15−// Console.WriteLine(f.ToString());
16−// Console.WriteLine(f.ToString("L"));
17−
18−// var ex = (new BooleanFunction(26)).GetFDNF();
19−//
20−// Console.WriteLine(ex.ToString());
21−//
22−// PrintDerivatives(new BooleanFunction(26));
23−
24−// foreach(var t in BooleanExpression.AllTrees(3)) {
25−// t.SetIthVar(0, BooleanVariable.B);
26−// t.setIthOp(0, BooleanOperation.Xor);
27−// Console.WriteLine(t.ToString());
28−// }
29−
30−// foreach(var v in Combinatorics.AllNTuples(new BooleanOperation[] { BooleanOperation.And,
31−// BooleanOperation.Or }, 3)) {
32−// foreach(var b in v) {
33−// Console.Write("{0} ", b);
34−// }
35−// Console.WriteLine();
36−// }
37−
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 };
39− BooleanVariable[] vars = { BooleanVariable.A, BooleanVariable.B, BooleanVariable.C };
40−
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());
8+ static readonly BooleanOperation[][] AvdoshinBases = {
9+ new BooleanOperation[] {
10+ BooleanOperation.Zero,
11+ BooleanOperation.NOR,
12+ BooleanOperation.NotCoImp,
13+ BooleanOperation.NotA,
14+ BooleanOperation.NotImp,
15+ BooleanOperation.NotB,
16+ BooleanOperation.Xor,
17+ BooleanOperation.NAND,
18+ BooleanOperation.And,
19+ BooleanOperation.Eq,
20+ BooleanOperation.B,
21+ BooleanOperation.Imp,
22+ BooleanOperation.A,
23+ BooleanOperation.CoImp,
24+ BooleanOperation.Or,
25+ BooleanOperation.One
26+ },
27+ new BooleanOperation[] { BooleanOperation.NOR, },
28+ new BooleanOperation[] { BooleanOperation.NAND, },
29+ new BooleanOperation[] { BooleanOperation.Zero, BooleanOperation.Imp, },
30+ new BooleanOperation[] { BooleanOperation.One, BooleanOperation.CoImp, },
31+ new BooleanOperation[] { BooleanOperation.Imp, BooleanOperation.CoImp, },
32+ new BooleanOperation[] { BooleanOperation.Xor, BooleanOperation.Imp, },
33+ new BooleanOperation[] { BooleanOperation.Eq, BooleanOperation.CoImp, },
34+ new BooleanOperation[] { BooleanOperation.NotA, BooleanOperation.NotB, BooleanOperation.Imp, },
35+ new BooleanOperation[] { BooleanOperation.NotA, BooleanOperation.NotB, BooleanOperation.CoImp, },
36+ new BooleanOperation[] { BooleanOperation.NotA, BooleanOperation.NotB, BooleanOperation.Or, },
37+ new BooleanOperation[] { BooleanOperation.NotA, BooleanOperation.NotB, BooleanOperation.And, },
38+ new BooleanOperation[] { BooleanOperation.Zero, BooleanOperation.Eq, BooleanOperation.And, },
39+ new BooleanOperation[] { BooleanOperation.One, BooleanOperation.Xor, BooleanOperation.Or, },
40+ new BooleanOperation[] { BooleanOperation.Zero, BooleanOperation.Eq, BooleanOperation.Or, },
41+ new BooleanOperation[] { BooleanOperation.One, BooleanOperation.Xor, BooleanOperation.And, },
42+ new BooleanOperation[] { BooleanOperation.Xor, BooleanOperation.Eq, BooleanOperation.Or, },
43+ new BooleanOperation[] { BooleanOperation.Eq, BooleanOperation.Xor, BooleanOperation.And, },
44+ };
45+
46+ static readonly BooleanVariable[] AllVars = {
47+ BooleanVariable.A,
48+ BooleanVariable.B,
49+ BooleanVariable.C,
50+ };
51+
52+ public static void PrintMinimaInAvdoshinBases(int n)
53+ {
54+ BooleanFunction f = new BooleanFunction(n);
55+
56+ Console.WriteLine("\nМинимальные представления функции в базисах (для рисования в винлогике):");
57+
58+ int i = 0;
59+
60+ Console.WriteLine(" ☻ \tБазис\t\tВыражние");
61+ Console.WriteLine();
62+
63+ foreach(var basis in AvdoshinBases) {
64+ Console.Write("{0,2}\t", i);
65+
66+ if(i++ != 0) {
67+ foreach(var op in basis) {
68+ Console.Write(BooleanExpression.PrintOperation(op) + " ");
69+ }
70+ }
71+ else {
72+ Console.Write("ОБЩИЙ");
73+ }
74+
75+ if(i == 5) {
76+ Console.WriteLine();
77+ continue;
78+ }
79+
80+ Console.Write("\t\t");
81+
82+ var ex = f.MininalExpressionInBasis(basis, AllVars);
83+
84+ Console.WriteLine(ex.ToString());
85+ }
5686 }
5787 }
5888
@@ -173,9 +203,7 @@ namespace SolveAvdoshin
173203
174204 public BooleanExpression MininalExpressionInBasis(BooleanOperation[] ops, BooleanVariable[] vars)
175205 {
176− for(int i = 2; i < 7; i++) {
177− Console.WriteLine("Trying {0}...", i);
178−
206+ for(int i = 2; i < 15; i++) {
179207 foreach(var ex in BooleanExpression.AllExpressions(i, ops, vars)) {
180208 if((new BooleanFunction(ex)).Equals(this)) {
181209 return ex;
@@ -187,11 +215,13 @@ namespace SolveAvdoshin
187215 }
188216 }
189217
190− enum BooleanOperation { Zero, NOR, NotBImp, NotA, NotImp, NotB, Xor, NAND, And, Eq, B, Imp, A, BImp, Or, One };
218+ enum BooleanOperation { Zero, NOR, NotCoImp, NotA, NotImp, NotB, Xor, NAND, And, Eq, B, Imp, A, CoImp, Or, One };
191219 enum BooleanVariable { A, B, C };
192220
193221 abstract class BooleanExpression
194222 {
223+ public static readonly string[] OpSymbols = new string[] { "0", "↓", "</=", "(NotA)", "=/>", "(NotB)", "⨁", "|", "&", "==", "(B)", "=>", "(A)", "<=", "|", "1", };
224+
195225 abstract public bool Eval(bool a, bool b, bool c);
196226 abstract new public string ToString();
197227 abstract public int CountVariables();
@@ -238,12 +268,15 @@ namespace SolveAvdoshin
238268 }
239269 }
240270 }
271+
272+ public static string PrintOperation(BooleanOperation op)
273+ {
274+ return OpSymbols[(int)op];
275+ }
241276 }
242277
243278 class OpExpression : BooleanExpression
244279 {
245− static readonly string[] OpSymbols = new string[] { "(Zero)", "(NOR)", "</=", "(NotA)", "=/>", "(NotB)", "⨁", "(NAND)", "&", "==", "(B)", "=>", "(A)", "<=", "|", "(One)", };
246−
247280 BooleanOperation Op;
248281 BooleanExpression Left, Right;
249282
@@ -285,7 +318,7 @@ namespace SolveAvdoshin
285318 return false;
286319 case BooleanOperation.NOR:
287320 return !(aVal || bVal);
288− case BooleanOperation.NotBImp:
321+ case BooleanOperation.NotCoImp:
289322 return !aVal && bVal;
290323 case BooleanOperation.NotA:
291324 return !aVal;
@@ -307,7 +340,7 @@ namespace SolveAvdoshin
307340 return !aVal || bVal;
308341 case BooleanOperation.A:
309342 return aVal;
310− case BooleanOperation.BImp:
343+ case BooleanOperation.CoImp:
311344 return aVal || !bVal;
312345 case BooleanOperation.Or:
313346 return aVal || bVal;
@@ -320,7 +353,26 @@ namespace SolveAvdoshin
320353
321354 public override string ToString()
322355 {
323− return "(" + Left.ToString() + " " + OpSymbols[(int)Op] + " " + Right.ToString() + ")";
356+ switch(Op) {
357+ case BooleanOperation.A:
358+ return Left.ToString();
359+ break;
360+ case BooleanOperation.B:
361+ return Right.ToString();
362+ break;
363+ case BooleanOperation.One:
364+ case BooleanOperation.Zero:
365+ return PrintOperation(Op);
366+ break;
367+ case BooleanOperation.NotA:
368+ return "!" + Left.ToString();
369+ break;
370+ case BooleanOperation.NotB:
371+ return "!" + Right.ToString();
372+ break;
373+ default:
374+ return "(" + Left.ToString() + " " + PrintOperation(Op) + " " + Right.ToString() + ")";
375+ }
324376 }
325377
326378 public override int CountVariables()
SolveAvdoshin/SolveAvdoshin.cs+6−4
@@ -50,7 +50,7 @@ namespace SolveAvdoshin
5050 return coefs;
5151 }
5252
53− public static void Solve(int[] coefs)
53+ public static int Solve(int[] coefs)
5454 {
5555 Console.WriteLine(PrintEquation(coefs));
5656
@@ -58,10 +58,10 @@ namespace SolveAvdoshin
5858
5959 Console.WriteLine("\nОтвет: " + eqAnswer);
6060
61−// BooleanFunction bFun =
61+ return eqAnswer;
6262 }
6363
64− public static void Main1(string[] args)
64+ public static void Main(string[] args)
6565 {
6666 int[] coefs = { -1, -1, -1, -1, -1, -1, -1, -1, -1, };
6767
@@ -73,7 +73,9 @@ namespace SolveAvdoshin
7373 coefs = ConsoleInput();
7474 }
7575
76− Solve(coefs);
76+ int answer = Solve(coefs);
77+
78+ BooleanFunctions.PrintMinimaInAvdoshinBases(answer);
7779 }
7880 catch(FormatException e) {
7981 Console.WriteLine("Osheebka: " + e.Message);