private static bool SatisfyGeneralForm(Equation equation,
                                               out Circle circle)
        {
            circle = null;
            var term = equation.Lhs as Term;

            if (term != null && term.Op.Method.Name.Equals("Add"))
            {
                var lst = term.Args as List <object>;
                if (lst != null && lst.Count == 3)
                {
                    bool isNum = LogicSharp.IsNumeric(lst[2]);

                    if (isNum)
                    {
                        double dNum;
                        LogicSharp.IsDouble(lst[2], out dNum);
                        dNum *= -1;
                        object coeffX, coeffY;
                        bool   xTerm1 = IsXSquareTerm(lst[0], out coeffX);
                        bool   yTerm1 = IsYSquareTerm(lst[1], out coeffY);
                        if (xTerm1 && yTerm1)
                        {
                            var pt = new Point(coeffX, coeffY);
                            circle = new Circle(pt, Math.Pow(dNum, 0.5));
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
 public bool AddYCoord(object y)
 {
     if (LogicSharp.IsNumeric(y))
     {
         Properties.Add(YCoordinate, y);
         return(true);
     }
     return(false);
 }
Ejemplo n.º 3
0
 public bool AddXCoord(object x)
 {
     if (LogicSharp.IsNumeric(x))
     {
         Properties.Add(XCoordinate, x);
         return(true);
     }
     return(false);
 }
        private static bool SatisfySpecialForm(Equation equation, out Circle circle)
        {
            circle = null;

            bool isRhsNum = LogicSharp.IsNumeric(equation.Rhs);

            if (!isRhsNum)
            {
                return(false);
            }
            double dNum;

            LogicSharp.IsDouble(equation.Rhs, out dNum);
            var term = equation.Lhs as Term;

            if (term == null)
            {
                return(false);
            }
            if (!term.Op.Method.Name.Equals("Add"))
            {
                return(false);
            }
            var lst = term.Args as List <object>;

            if (lst == null || lst.Count != 2)
            {
                return(false);
            }

            object coeffX, coeffY;
            bool   xTerm = IsXSquareTerm(lst[0], out coeffX);
            bool   yTerm = IsYSquareTerm(lst[1], out coeffY);

            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            xTerm = false;
            yTerm = false;
            xTerm = IsXSquareTerm(lst[1], out coeffX);
            yTerm = IsYSquareTerm(lst[0], out coeffY);
            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            return(false);
        }
Ejemplo n.º 5
0
        public bool Reify(EqGoal goal)
        {
            var line = this.Shape as Line;

            Debug.Assert(line != null);
            EqGoal tempGoal = goal;
            bool   cond1    = Var.IsVar(tempGoal.Lhs) && LogicSharp.IsNumeric(tempGoal.Rhs);
            bool   cond2    = Var.IsVar(tempGoal.Rhs) && LogicSharp.IsNumeric(tempGoal.Lhs);

            Debug.Assert(cond1 || cond2);

            if (line.Concrete)
            {
                return(false);
            }

            object aResult = EvalGoal(line.A, tempGoal);
            object bResult = EvalGoal(line.B, tempGoal);
            object cResult = EvalGoal(line.C, tempGoal);

            //Atomic operation
            if (aResult != null && !line.A.Equals(aResult))
            {
                CacheA(aResult, goal);
                RaiseReify(null);
                return(true);
            }

            if (bResult != null && !line.B.Equals(bResult))
            {
                CacheB(bResult, goal);
                RaiseReify(null);
                return(true);
            }

            if (cResult != null && !line.C.Equals(cResult))
            {
                CacheC(cResult, goal);
                RaiseReify(null);
                return(true);
            }

            return(false);
        }
        private static bool IsYWithConstTerm(object obj, out object coeff)
        {
            coeff = null;
            var term = obj as Term;

            if (term == null)
            {
                return(false);
            }

            if (term.Op.Method.Name.Equals("Add") ||
                term.Op.Method.Name.Equals("Substract"))
            {
                var lst = term.Args as List <object>;
                if (lst == null)
                {
                    return(false);
                }
                bool isYTerm = IsYTerm(lst[0], out coeff);
                if (coeff == null || !coeff.ToString().Equals("1"))
                {
                    return(false);
                }
                if (!isYTerm)
                {
                    return(false);
                }
                bool isRhsNum = LogicSharp.IsNumeric(lst[1]);
                if (!isRhsNum)
                {
                    return(false);
                }
                double dNum;
                LogicSharp.IsDouble(lst[1], out dNum);
                if (term.Op.Method.Name.Equals("Add"))
                {
                    dNum *= -1;
                }
                coeff = dNum;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 7
0
        private static bool SatisfySpecialForm(Equation equation, out Line line)
        {
            line = null;

            bool isRhsNum = LogicSharp.IsNumeric(equation.Rhs);

            if (!isRhsNum)
            {
                return(false);
            }

            double dNum;

            LogicSharp.IsDouble(equation.Rhs, out dNum);

            double rhs = -1 * dNum;
            object xCoeff;
            bool   result = IsXTerm(equation.Lhs, out xCoeff);

            if (result)
            {
                line = new Line(null, xCoeff, null, rhs);
                return(true);
            }
            object yCoeff;

            result = IsYTerm(equation.Lhs, out yCoeff);
            if (result)
            {
                line = new Line(null, null, yCoeff, rhs);
                return(true);
            }
            result = IsXYTerm(equation.Lhs, out xCoeff, out yCoeff);
            if (result)
            {
                line = new Line(null, xCoeff, yCoeff, rhs);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This method quarantees that the substitution term is unique toward each variable.
        ///
        /// dict: {{x:3},{x:4}} is not allowed
        /// dict: {{x:3}, {y:4}} is allowed
        /// </summary>
        /// <param name="point"></param>
        /// <param name="dict"></param>
        /// <param name="ps"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public bool Reify(EqGoal goal)
        {
            var point = Shape as Point;

            Debug.Assert(point != null);

            //EqGoal tempGoal = goal.GetLatestDerivedGoal();
            EqGoal tempGoal = goal;
            bool   cond1    = Var.IsVar(tempGoal.Lhs) && LogicSharp.IsNumeric(tempGoal.Rhs);
            bool   cond2    = Var.IsVar(tempGoal.Rhs) && LogicSharp.IsNumeric(tempGoal.Lhs);

            Debug.Assert(cond1 || cond2);

            if (point.Concrete)
            {
                return(false);
            }

            object xResult = EvalGoal(point.XCoordinate, tempGoal);
            object yResult = EvalGoal(point.YCoordinate, tempGoal);

            //Atomic operation
            if (!point.XCoordinate.Equals(xResult))
            {
                GenerateXCacheSymbol(xResult, goal);
                RaiseReify(null);
                return(true);
            }
            else if (!point.YCoordinate.Equals(yResult))
            {
                GenerateYCacheSymbol(yResult, goal);
                RaiseReify(null);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// construct a line through two goals
        /// e.g  m=2, k=3 => conjunctive norm form
        /// </summary>
        /// <param name="goal1"></param>
        /// <param name="goal2"></param>
        /// <returns></returns>
        public static LineSymbol Unify(EqGoal goal1, EqGoal goal2)
        {
            var variable1 = goal1.Lhs as Var;
            var variable2 = goal2.Lhs as Var;

            Debug.Assert(variable1 != null);
            Debug.Assert(variable2 != null);

            var dict = new Dictionary <string, object>();

            string slopeKey     = "slope";
            string interceptKey = "intercept";

            if (LineAcronym.EqualSlopeLabels(variable1.ToString()))
            //if (variable1.ToString().Equals(LineAcronym.Slope1))
            {
                dict.Add(slopeKey, goal1.Rhs);
            }
            if (LineAcronym.EqualInterceptLabels(variable1.ToString()))
            //if (variable1.ToString().Equals(LineAcronym.Intercept1))
            {
                dict.Add(interceptKey, goal1.Rhs);
            }
            if (LineAcronym.EqualSlopeLabels(variable2.ToString()))
            //if (variable2.ToString().Equals(LineAcronym.Slope1))
            {
                if (dict.ContainsKey(slopeKey))
                {
                    return(null);
                }
                dict.Add(slopeKey, goal2.Rhs);
            }
            if (LineAcronym.EqualInterceptLabels(variable2.ToString()))
            //if (variable2.ToString().Equals(LineAcronym.Intercept1))
            {
                if (dict.ContainsKey(interceptKey))
                {
                    return(null);
                }
                dict.Add(interceptKey, goal2.Rhs);
            }

            if (dict.Count == 2 &&
                dict[slopeKey] != null &&
                dict[interceptKey] != null)
            {
                if (LogicSharp.IsNumeric(dict[slopeKey]) &&
                    LogicSharp.IsNumeric(dict[interceptKey]))
                {
                    double dSlope, dIntercept;
                    LogicSharp.IsDouble(dict[slopeKey], out dSlope);
                    LogicSharp.IsDouble(dict[interceptKey], out dIntercept);
                    var line = LineGenerationRule.GenerateLine(dSlope, dIntercept);
                    var ls   = new LineSymbol(line)
                    {
                        OutputType = LineType.SlopeIntercept
                    };

                    TraceInstructionalDesign.FromSlopeInterceptToLineSlopeIntercept(goal1, goal2, ls);
                    return(ls);
                }
                else
                {
                    //lazy evaluation
                    //Constraint solving on Graph
                    var line = new Line(null); //ghost line
                    var ls   = new LineSymbol(line);
                    ls.OutputType = LineType.SlopeIntercept;
                    return(ls);
                }
            }
            return(null);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The purpose of parse string is to re-format the str
        /// x, 2x, -2x, ax, -ax,2ax, 3y,34y, y25
        /// </summary>
        /// <param name="str"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object TransformString(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }

            bool   isNeg = false;
            string parseStr;

            if (charArr[0].Equals('-'))
            {
                isNeg    = true;
                parseStr = str.Substring(1, str.Length - 1);
            }
            else
            {
                parseStr = str;
            }

            //TODO tackle decimal number
            string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            //string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=(\\d+\\.\\d+))(?=\\D)");
            var lst = new List <object>();

            for (var i = 0; i < strs.Length; i++)
            {
                if (strs[i].Equals("."))
                {
                    throw new Exception("Cannot be decimal input");
                }

                if (i == 0)
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            iNum = isNeg ? iNum * -1 : iNum;
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            dNum = isNeg ? dNum * -1 : dNum;
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        if (isNeg)
                        {
                            lst.Add(-1);
                        }
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
                else
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
            }
            if (lst.Count == 1)
            {
                return(lst[0]);
            }
            return(new Term(Expression.Multiply, lst));
        }
Ejemplo n.º 11
0
        public static object TransformString2(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }
            bool isNeg = charArr[0].Equals('-');

            var lst = new List <object>();
            int j   = -1;

            for (int i = charArr.Count() - 1; i >= 0; i--)
            {
                if (Char.IsLetter(charArr[i]))
                {
                    lst.Insert(0, new Var(charArr[i]));
                }
                else
                {
                    j = i;
                    break;
                }
            }
            if (isNeg)
            {
                if (j == 0)
                {
                    var term1 = new Term(Expression.Multiply, new List <object>()
                    {
                        -1, lst[0]
                    });
                    lst[0] = term1;

                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var  subStr = str.Substring(1, j);
                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, -1 * iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, -1 * dNum);
                }
            }
            else
            {
                if (j == -1)
                {
                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var subStr = str.Substring(0, j + 1);

                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, dNum);
                }
            }
            return(new Term(Expression.Multiply, lst));
        }