git.iko.soy/ikoHSE/SolveAvdoshin
on master help sign in
Implemented some of the bruteforce tools (AllTrees, SetIthVar, SetIthOp)
commit721b4cd0af8f7ad12517d9f5f133ef369eb2c379 authorIlya Gulkov <ifranked@gmail.com> dateTue 29 Sep 2015, 15:18 (11 years ago) parent2b31b1b
1 file changed+141−49
SolveAvdoshin/BooleanFunctions.cs+141−49
SolveAvdoshin/BooleanFunctions.cs+141−49
@@ -1,4 +1,5 @@
11using System;
2+using System.Collections.Generic;
23
34namespace SolveAvdoshin
45{
@@ -14,9 +15,17 @@ namespace SolveAvdoshin
1415// Console.WriteLine(f.ToString());
1516// Console.WriteLine(f.ToString("L"));
1617
17− var ex = (new BooleanFunction(26)).GetFDNF();
18+// var ex = (new BooleanFunction(26)).GetFDNF();
19+//
20+// Console.WriteLine(ex.ToString());
21+//
22+// PrintDerivatives(new BooleanFunction(26));
1823
19− Console.WriteLine(ex.ToString());
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+ }
2029 }
2130 }
2231
@@ -33,23 +42,55 @@ namespace SolveAvdoshin
3342 {
3443 return (TruthTable >> (7 - (a ? 4 : 0) - (b ? 2 : 0) - (c ? 1 : 0)) & 1) == 1;
3544 }
45+
46+ public int Eval(int a, int b, int c)
47+ {
48+ return TruthTable >> (7 - a * 4 - b * 2 - c) & 1;
49+ }
50+
51+ public static string VarRows()
52+ {
53+ string[] lines = new string[] { "A ", "B ", "C ", };
54+
55+ for(int i = 0; i < 8; i++) {
56+ lines[0] += (i >> 2 & 1) + " ";
57+ lines[1] += (i >> 1 & 1) + " ";
58+ lines[2] += (i & 1) + " ";
59+ }
60+
61+ return String.Join("\n", lines) + "\n";
62+ }
63+
64+ public static string DelimRow()
65+ {
66+ string line = "--";
67+
68+ for(int i = 0; i < 8; i++) {
69+ line += "--";
70+ }
71+
72+ return line + "\n";
73+ }
74+
75+ public string FuncRow()
76+ {
77+ string line = "F ";
78+
79+ for(int i = 0; i < 8; i++) {
80+ line += (Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1) ? 1 : 0) + " ";
81+ }
82+
83+ return line + "\n";
84+ }
3685
3786 public string ToString(string format)
3887 {
3988 if(format == "L") {
40− string[] lines = new string[] { "A ", "B ", "C ", "--", "F ", };
41−
42− for(int i = 0; i < 8; i++) {
43− lines[0] += (i >> 2 & 1) + " ";
44− lines[1] += (i >> 1 & 1) + " ";
45− lines[2] += (i & 1) + " ";
46− lines[3] += "--";
47− lines[4] += (Eval((i >> 2 & 1) == 1, (i >> 1 & 1) == 1, (i & 1) == 1) ? 1 : 0) + " ";
48− }
49−
5089 string result = "";
5190
52− foreach(string line in lines) result += line + "\n";
91+ result += VarRows();
92+ result += DelimRow();
93+ result += FuncRow();
5394
5495 return result;
5596 }
@@ -75,22 +116,12 @@ namespace SolveAvdoshin
75116
76117 BooleanExpression varA, varB, varC;
77118
78− if((i >> 2 & 1) == 1)
79− varA = new VarExpression(BooleanVariable.A);
80− else
81− varA = new NotExpression(BooleanVariable.A);
82−
83− if((i >> 1 & 1) == 1)
84− varB = new VarExpression(BooleanVariable.B);
85− else
86− varB = new NotExpression(BooleanVariable.B);
87−
88− if((i & 1) == 1)
89− varC = new VarExpression(BooleanVariable.C);
90− else
91− varC = new NotExpression(BooleanVariable.C);
119+ varA = new VarExpression(BooleanVariable.A, (i >> 2 & 1) != 1);
120+ varB = new VarExpression(BooleanVariable.B, (i >> 1 & 1) != 1);
121+ varC = new VarExpression(BooleanVariable.C, (i & 1) != 1);
92122
93− cons1 = new OpExpression(BooleanOperation.And, new OpExpression(BooleanOperation.And, varA, varB), varC);
123+ cons1 = new OpExpression(BooleanOperation.And,
124+ new OpExpression(BooleanOperation.And, varA, varB), varC);
94125
95126 ex = ex != null ? new OpExpression(BooleanOperation.Or, cons1, ex) : cons1;
96127 }
@@ -106,30 +137,35 @@ namespace SolveAvdoshin
106137 {
107138 abstract public bool Eval(bool a, bool b, bool c);
108139 abstract new public string ToString();
109− }
110−
111− class NotExpression : BooleanExpression
112− {
113− BooleanExpression Arg;
114−
115− public NotExpression(BooleanExpression arg)
116− {
117− Arg = arg;
118− }
140+ abstract public int CountVariables();
141+ abstract public void SetIthVar(int i, BooleanVariable value);
142+ abstract public int setIthOp(int i, BooleanOperation value);
119143
120− public NotExpression(BooleanVariable variable)
144+ public static IEnumerable<BooleanExpression> AllTrees(int size)
121145 {
122− Arg = new VarExpression(variable);
146+ if(size == 0) {
147+ yield return new VarExpression(BooleanVariable.A);
148+ }
149+ else {
150+ for(int i = 0; i < size; i++) {
151+ foreach(var l in AllTrees(i)) {
152+ foreach(var r in AllTrees(size - 1 - i)) {
153+ yield return new OpExpression(BooleanOperation.And, l, r);
154+ }
155+ }
156+ }
157+ }
123158 }
124159
125− public override string ToString()
160+ public static IEnumerable<BooleanExpression> AllExpressions(int size, BooleanOperation[] ops,
161+ BooleanVariable[] vars)
126162 {
127− return "!" + Arg.ToString();
163+ throw new NotImplementedException();
128164 }
129165
130− public override bool Eval(bool a, bool b, bool c)
166+ public BooleanExpression FindMininalInBasis()
131167 {
132− return !Arg.Eval(a, b, c);
168+ throw new NotImplementedException();
133169 }
134170 }
135171
@@ -215,6 +251,45 @@ namespace SolveAvdoshin
215251 {
216252 return "(" + Left.ToString() + " " + OpSymbols[(int)Op] + " " + Right.ToString() + ")";
217253 }
254+
255+ public override int CountVariables()
256+ {
257+ int res = 0;
258+
259+ res += Left.CountVariables();
260+ res += Right.CountVariables();
261+
262+ return res;
263+ }
264+
265+ public override void SetIthVar(int i, BooleanVariable value)
266+ {
267+ if(i >= Left.CountVariables()) {
268+ Right.SetIthVar(i - Left.CountVariables(), value);
269+ }
270+ else {
271+ Left.SetIthVar(i, value);
272+ }
273+ }
274+
275+ public override int setIthOp(int i, BooleanOperation value)
276+ {
277+ if(i < 0) {
278+ return 0;
279+ }
280+ else {
281+ if(i == 0) {
282+ Op = value;
283+ }
284+
285+ int res = 1;
286+
287+ res += Left.setIthOp(i - res, value);
288+ res += Right.setIthOp(i - res, value);
289+
290+ return res;
291+ }
292+ }
218293 }
219294
220295 class VarExpression : BooleanExpression
@@ -222,21 +297,23 @@ namespace SolveAvdoshin
222297 static readonly string[] VarSymbols = new string[] { "A", "B", "C", };
223298
224299 BooleanVariable Var;
300+ bool Not;
225301
226− public VarExpression(BooleanVariable var)
302+ public VarExpression(BooleanVariable var, bool not = false)
227303 {
228304 Var = var;
305+ Not = not;
229306 }
230307
231308 public override bool Eval(bool a, bool b, bool c)
232309 {
233310 switch(Var) {
234311 case BooleanVariable.A:
235− return a;
312+ return a != Not;
236313 case BooleanVariable.B:
237− return b;
314+ return b != Not;
238315 case BooleanVariable.C:
239− return c;
316+ return c != Not;
240317 default:
241318 throw new Exception("Nigga wat?");
242319 }
@@ -244,7 +321,22 @@ namespace SolveAvdoshin
244321
245322 public override string ToString()
246323 {
247− return VarSymbols[(int)Var];
324+ return (Not ? "!" : "") + VarSymbols[(int)Var];
325+ }
326+
327+ public override int CountVariables()
328+ {
329+ return 1;
330+ }
331+
332+ public override void SetIthVar(int i, BooleanVariable variable)
333+ {
334+ this.Var = variable;
335+ }
336+
337+ public override int setIthOp(int i, BooleanOperation value)
338+ {
339+ return 0;
248340 }
249341 }
250342}