Skip to content

Commit

Permalink
Added expression evaluation.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkonowrocki committed May 12, 2019
1 parent ad2d778 commit fdf0701
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@ project.lock.json

#####
# End of core ignore list, below put you custom 'per project' settings (patterns or path)
#####
#####
/GUI
/MultiAgentLanguageGUI/packages
42 changes: 42 additions & 0 deletions ModelsTests/CommonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Ninject;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Action = MultiAgentLanguageModels.Action;
Expand Down Expand Up @@ -62,5 +63,46 @@ public void GeneratingLogicExpression2()
var notSigmaExpression = new LogicExpression(notSigma);
Assert.AreEqual(@"[(\sigma)]", notSigmaExpression.ToProlog());
}

[Test]
public void LogicExpressionCheck()
{
var sigma = new Fluent("sigma");
var notSigma = new Not(sigma);
var notSigmaExpression = new LogicExpression(notSigma);
var actual = notSigmaExpression.EvaluateLogicExpression();
var expected = new List<List<Tuple<string, bool>>>()
{
new List<Tuple<string, bool>>()
{
new Tuple<string, bool>("sigma", false)
}
};
Assert.AreEqual(expected, actual);
}

[Test]
public void LogicExpressionCheck2()
{
var sigma = new Fluent("sigma");
var pi = new Fluent("pi");
var beta = new Fluent("beta");
var notSigma = new Not(sigma);
var ifPiBeta = new If(pi, beta);
var iffPiBetaSigma = new Iff(ifPiBeta, notSigma);

var iffExpression = new LogicExpression(iffPiBetaSigma);
var actual = iffExpression.EvaluateLogicExpression().ToListOfStrings();
var expected = new List<string>()
{
@"[beta, pi, \sigma]",
@"[beta, \pi, \sigma]",
@"[\beta, \pi, \sigma]",
@"[\beta, pi, sigma]"
};
var check1 = expected.TrueForAll(x => actual.Contains(x));
var check2 = actual.TrueForAll(x => expected.Contains(x));
Assert.True(check1 && check2);
}
}
}
9 changes: 9 additions & 0 deletions MultiAgentLanguageModels/Fluent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ public class Fluent : LogicElement
{
public string Name { get; set; }

public bool Value { get; set; }

public Fluent(string name)
{
Name = name;
Left = null;
Right = null;
}

public override string ToString()
{
return Name;
}

public override bool GetValue()
{
return Value;
}
}
}
31 changes: 31 additions & 0 deletions MultiAgentLanguageModels/LogicElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public abstract class LogicElement
{
public LogicElement Left { get; set; }
public LogicElement Right { get; set; }
public abstract bool GetValue();
}

public class Not : LogicElement
Expand All @@ -13,6 +14,12 @@ public Not(LogicElement logicElement)
Left = logicElement;
Right = null;
}

public override bool GetValue()
{
return !Left.GetValue();
}

public override string ToString()
{
return $"(\\{Left.ToString()})";
Expand All @@ -26,6 +33,12 @@ public Or(LogicElement left, LogicElement right)
Left = left;
Right = right;
}

public override bool GetValue()
{
return Left.GetValue() || Right.GetValue();
}

public override string ToString()
{
return $"({Left.ToString()}+{Right.ToString()})";
Expand All @@ -39,6 +52,12 @@ public And(LogicElement left, LogicElement right)
Left = left;
Right = right;
}

public override bool GetValue()
{
return Left.GetValue() && Right.GetValue();
}

public override string ToString()
{
return $"({Left.ToString()}*{Right.ToString()})";
Expand All @@ -52,6 +71,12 @@ public If(LogicElement left, LogicElement right)
Left = left;
Right = right;
}

public override bool GetValue()
{
return (!Left.GetValue()) || Right.GetValue();
}

public override string ToString()
{
Not notLeft = new Not(Left);
Expand All @@ -67,6 +92,12 @@ public Iff(LogicElement left, LogicElement right)
Left = left;
Right = right;
}

public override bool GetValue()
{
return ((!Left.GetValue()) || Right.GetValue()) && ((!Right.GetValue()) || Left.GetValue());
}

public override string ToString()
{
If leftToRight = new If(Left, Right);
Expand Down
76 changes: 73 additions & 3 deletions MultiAgentLanguageModels/LogicExpression.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,65 @@
namespace MultiAgentLanguageModels
using System;
using System.Collections.Generic;
using System.Linq;

namespace MultiAgentLanguageModels
{
public class LogicExpression : IProlog
{

private List<Fluent> Fluents
{
get
{
List<Fluent> fluents = new List<Fluent>();
List<LogicElement> temp = new List<LogicElement>();
temp.Add(Element);
LogicElement logicElement;
while (temp.Count != 0)
{
logicElement = temp.First();
temp.RemoveAt(0);
if (logicElement.GetType() == typeof(Fluent))
{
fluents.Add(logicElement as Fluent);
}
else
{
if (logicElement.Left != null)
{
temp.Add(logicElement.Left);
}
if (logicElement.Right != null)
{
temp.Add(logicElement.Right);
}
}
}
return fluents.OrderBy(x => x.Name).ToList();
}
}

public LogicElement Element { get; }

private readonly bool empty = false;

private static LogicExpression _empty;

public string StringExpression { get => Element.ToString(); }

public static LogicExpression Empty {
get {
if (_empty == null)
_empty = new LogicExpression();
return _empty;
} }
}
}

private LogicExpression()
{
empty = true;
}

public LogicExpression(LogicElement element)
{
Element = element;
Expand All @@ -28,5 +69,34 @@ public string ToProlog()
{
return empty ? "[]" : $"[{Element.ToString()}]";
}

public List<List<Tuple<string, bool>>> EvaluateLogicExpression()
{
List<List<Tuple<string, bool>>> results = new List<List<Tuple<string, bool>>>();
var fluents = Fluents;
for(int i=0; i < Math.Pow(2, fluents.Count); i++)
{
var binary = Convert.ToString(i, 2).PadLeft(fluents.Count, '0').Select(x => x == '1' ? true : false).ToArray();
for(int j = 0; j < fluents.Count; j++)
{
fluents[j].Value = binary[j];
}
if (Element.GetValue())
{
results.Add(
fluents.Select(x => new Tuple<string, bool>(x.Name, x.Value)).ToList()
);
}
}
return results;
}
}

public static class EvaluationExt
{
public static List<string> ToListOfStrings(this List<List<Tuple<string, bool>>> tuples)
{
return tuples.Select(x => $"[{x.Select(t => t.Item2 ? t.Item1 : $"\\{t.Item1}").Aggregate((a, b) => a + ", " + b)}]").ToList();
}
}
}

0 comments on commit fdf0701

Please sign in to comment.