private static object InferIntercept(this LineSymbol inputLineSymbol, string label) { var line = inputLineSymbol.Shape as Line; Debug.Assert(line != null); if (line.Intercept != null) { var goal = new EqGoal(new Var(label), line.Intercept); goal.Traces.AddRange(inputLineSymbol.Traces); TraceInstructionalDesign.FromLineToIntercept(inputLineSymbol, goal); return(goal); } if (inputLineSymbol.CachedSymbols.Count != 0) { var goalList = new List <object>(); foreach (var lss in inputLineSymbol.CachedSymbols) { var cachedLss = lss as LineSymbol; Debug.Assert(cachedLss != null); var cachedLs = cachedLss.Shape as Line; Debug.Assert(cachedLs != null); var goal = new EqGoal(new Var(label), cachedLs.Intercept); goal.Traces.AddRange(cachedLss.Traces); TraceInstructionalDesign.FromLineToIntercept(cachedLss, goal); goalList.Add(goal); } return(goalList); } return(null); }
//forward solving private static object InferDistance(this LineSegmentSymbol inputLineSymbol, string label) { var lineSeg = inputLineSymbol.Shape as LineSegment; Debug.Assert(lineSeg != null); if (label != null && lineSeg.Distance != null) { var goal = new EqGoal(new Var(label), lineSeg.Distance); TraceInstructionalDesign.FromLineSegmentToDistance(inputLineSymbol); goal.Traces.AddRange(inputLineSymbol.Traces); return(goal); } if (label != null && inputLineSymbol.CachedSymbols.Count != 0) { var goalList = new List <object>(); foreach (var lss in inputLineSymbol.CachedSymbols) { var cachedLss = lss as LineSegmentSymbol; Debug.Assert(cachedLss != null); var cachedLs = cachedLss.Shape as LineSegment; Debug.Assert(cachedLs != null); var goal = new EqGoal(new Var(label), cachedLs.Distance); //goal.Traces.AddRange(cachedLss.Traces); TraceInstructionalDesign.FromLineSegmentToDistance(cachedLss); goal.Traces.AddRange(cachedLss.Traces); goalList.Add(goal); } return(goalList); } return(null); }
//forward solving private static EqGoal InferSlope(this LineSymbol inputLineSymbol, string label) { var line = inputLineSymbol.Shape as Line; Debug.Assert(line != null); var goal = new EqGoal(new Var(label), line.Slope); goal.Traces.AddRange(inputLineSymbol.Traces); TraceInstructionalDesign.FromLineToSlope(inputLineSymbol, goal); return(goal); }
/// <summary> /// TODO /// </summary> /// <param name="inputLineSymbol"></param> /// <param name="label"></param> /// <returns></returns> private static LineSymbol InferGeneralForm(this LineSymbol inputLineSymbol, string label) { var line = inputLineSymbol.Shape as Line; Debug.Assert(line != null); var ls = new LineSymbol(line); ls.OutputType = LineType.GeneralForm; ls.Traces.AddRange(inputLineSymbol.Traces); TraceInstructionalDesign.FromOneFormToAnother(inputLineSymbol, ls); return(ls); }
private static bool ConstraintCheck(GoalNode goalNode1, GoalNode goalNode2, object constraint, out object output) { Debug.Assert(constraint != null); var st = constraint as ShapeType?; var goal1 = goalNode1.Goal as EqGoal; var goal2 = goalNode2.Goal as EqGoal; Debug.Assert(goal1 != null); Debug.Assert(goal2 != null); output = null; if (st != null) { switch (st.Value) { case ShapeType.Line: output = LineBinaryRelation.Unify(goal1, goal2); break; } return(output != null); } var label = constraint as string; if (label != null) { //TODO if (LineAcronym.EqualGeneralFormLabels(label)) { output = LineBinaryRelation.Unify(goal1, goal2); var ls = output as LineSymbol; if (ls != null) { ls.OutputType = LineType.GeneralForm; TraceInstructionalDesign.FromLineSlopeIntercetpToLineGeneralForm(ls); return(true); } return(false); } if (LineAcronym.EqualSlopeInterceptFormLabels(label)) { output = LineBinaryRelation.Unify(goal1, goal2); var ls = output as LineSymbol; if (ls != null) { ls.OutputType = LineType.SlopeIntercept; return(true); } return(false); } } return(false); }
public void Problem16() { var pt1 = new Point(-1, 2); var ps1 = new PointSymbol(pt1); var pt2 = new Point(5, 8); var ps2 = new PointSymbol(pt2); var pt3 = new Point(2, 4); var midPoint = new PointSymbol(pt3); TraceInstructionalDesign.FromPointsToMidPoint(ps1, ps2, midPoint); }
/// <summary> /// ax+by+c=0 =========> y = -(a/b)x-(c/b) /// </summary> /// <param name="inputLineSymbol"></param> /// <param name="label"></param> /// <returns></returns> private static LineSymbol InferSlopeInterceptForm(this LineSymbol inputLineSymbol) { //TODO var line = inputLineSymbol.Shape as Line; Debug.Assert(line != null); var ls = new LineSymbol(line); ls.OutputType = LineType.SlopeIntercept; ls.Traces.AddRange(inputLineSymbol.Traces); TraceInstructionalDesign.FromOneFormToAnother(inputLineSymbol, ls); return(ls); }
public void Problem01() { /* const string input1 = "A(2,0)"; * const string input2 = "B(5,4)";*/ var pt1 = new Point(2, 0); var ps1 = new PointSymbol(pt1); var pt2 = new Point(5, 4); var ps2 = new PointSymbol(pt2); var d = new Var("d"); var eqGoal = new EqGoal(d, 5); var ls = new LineSegment(pt1, pt2); var lss = new LineSegmentSymbol(ls); TraceInstructionalDesign.FromPointsToLineSegment(lss); TraceInstructionalDesign.FromLineSegmentToDistance(lss); }
/// <summary> /// construct a line through a point and a goal, /// e.g A(1,2) ^ S = 2=> Conjunctive Norm Form /// </summary> /// <param name="pt1"></param> /// <param name="goal"></param> /// <returns></returns> public static LineSymbol Unify(PointSymbol pt, EqGoal goal) { var variable1 = goal.Lhs as Var; Debug.Assert(variable1 != null); if (LineAcronym.EqualSlopeLabels(variable1.ToString())) { double dValue; bool result = LogicSharp.IsDouble(goal.Rhs, out dValue); if (result) { if (!pt.Shape.Concrete) { return(null); } var line = LineGenerationRule.GenerateLine((Point)pt.Shape, dValue, null); if (pt.Traces.Count != 0) { line.Traces.AddRange(pt.Traces); } if (goal.Traces.Count != 0) { line.Traces.AddRange(goal.Traces); } TraceInstructionalDesign.FromPointSlopeToLine(pt, goal, line); line.OutputType = LineType.SlopeIntercept; return(line); } else { var line = new Line(null); //ghost line var ls = new LineSymbol(line); ls.OutputType = LineType.SlopeIntercept; return(ls); } } return(null); }
public static object Unify(PointSymbol pt1, PointSymbol pt2, EqGoal goal = null) { //point identify check if (pt1.Equals(pt2)) { return(null); } //Mid-point build process if (pt1.Shape.Concrete && pt2.Shape.Concrete) { var point1 = pt1.Shape as Point; var point2 = pt2.Shape as Point; Debug.Assert(point1 != null); Debug.Assert(point2 != null); var midPoint = PointGenerationRule.GenerateMidPoint(point1, point2); TraceInstructionalDesign.FromPointsToMidPoint(pt1, pt2, midPoint); return(midPoint); } return(null); }
public static LineSegmentSymbol GenerateLineSegment(Point pt1, Point pt2) { if (pt1.Equals(pt2)) { return(null); } Debug.Assert(pt1.Concrete); Debug.Assert(pt2.Concrete); var ls = new LineSegment(pt1, pt2); var lss = new LineSegmentSymbol(ls); if (pt1.Traces.Count != 0) { lss.Traces.AddRange(pt1.Traces); } if (pt2.Traces.Count != 0) { lss.Traces.AddRange(pt2.Traces); } TraceInstructionalDesign.FromPointsToLineSegment(lss); return(lss); }
//backward solving private static object InferSlope(this LineSymbol inputLineSymbol, double value) { return(TraceInstructionalDesign.FromLineToSlope(inputLineSymbol, value)); }
/// <summary> /// construct a line through two points /// </summary> /// <param name="pt1"></param> /// <param name="pt2"></param> /// <returns></returns> public static object Unify(PointSymbol pt1, PointSymbol pt2, EqGoal goal = null) { //point identify check if (pt1.Equals(pt2)) { return(null); } //Line build process if (pt1.Shape.Concrete && pt2.Shape.Concrete) { var point1 = pt1.Shape as Point; var point2 = pt2.Shape as Point; Debug.Assert(point1 != null); Debug.Assert(point2 != null); var winPt1 = new System.Windows.Point((double)point1.XCoordinate, (double)point1.YCoordinate); var winPt2 = new System.Windows.Point((double)point2.XCoordinate, (double)point2.YCoordinate); var lineSymbol = LineGenerationRule.GenerateLine(point1, point2); if (lineSymbol == null) { return(null); } var line = lineSymbol.Shape as Line; Debug.Assert(line != null); line.Rel1 = winPt1; line.Rel2 = winPt2; TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, lineSymbol); return(lineSymbol); } else { //lazy evaluation //Constraint solving on Graph var line = new Line(null); //ghost line line.Rel1 = pt1.Shape; line.Rel2 = pt2.Shape; var ls = new LineSymbol(line); #region Reification Purpose if (pt1.Shape.Concrete && !pt2.Shape.Concrete) { var point1 = pt1.Shape as Point; if (pt2.CachedSymbols.Count != 0) { foreach (ShapeSymbol ss in pt2.CachedSymbols) { var ps = ss as PointSymbol; Debug.Assert(ps != null); Debug.Assert(ps.Shape.Concrete); var cachePoint = ps.Shape as Point; Debug.Assert(cachePoint != null); var gline = LineGenerationRule.GenerateLine(point1, cachePoint); gline.Traces.AddRange(ps.Traces); TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline); ls.CachedSymbols.Add(gline); } } } if (!pt1.Shape.Concrete && pt2.Shape.Concrete) { var point2 = pt2.Shape as Point; if (pt1.CachedSymbols.Count != 0) { foreach (ShapeSymbol ss in pt1.CachedSymbols) { var ps = ss as PointSymbol; Debug.Assert(ps != null); Debug.Assert(ps.Shape.Concrete); var cachePoint = ps.Shape as Point; Debug.Assert(cachePoint != null); var gline = LineGenerationRule.GenerateLine(cachePoint, point2); gline.Traces.AddRange(ps.Traces); TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline); ls.CachedSymbols.Add(gline); } } } if (!pt1.Shape.Concrete && !pt2.Shape.Concrete) { foreach (ShapeSymbol ss1 in pt1.CachedSymbols) { foreach (ShapeSymbol ss2 in pt2.CachedSymbols) { var ps1 = ss1 as PointSymbol; Debug.Assert(ps1 != null); Debug.Assert(ps1.Shape.Concrete); var cachePoint1 = ps1.Shape as Point; Debug.Assert(cachePoint1 != null); var ps2 = ss2 as PointSymbol; Debug.Assert(ps2 != null); Debug.Assert(ps2.Shape.Concrete); var cachePoint2 = ps2.Shape as Point; Debug.Assert(cachePoint2 != null); var gline = LineGenerationRule.GenerateLine(cachePoint1, cachePoint2); gline.Traces.AddRange(ps1.Traces); gline.Traces.AddRange(ps2.Traces); TraceInstructionalDesign.FromPointPointToLine(pt1, pt2, gline); ls.CachedSymbols.Add(gline); } } } #endregion if (goal != null) { return(ls.Unify(goal)); } return(ls); } }
/// <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); }
//backward solving private static object InferDistance(this LineSegmentSymbol inputLineSymbol, double value) { return(TraceInstructionalDesign.FromLineSegmentToDistance(inputLineSymbol, value)); }
public static bool IsLineEquation(this Equation eq, out LineSymbol ls, bool allowEval = true) { Debug.Assert(eq != null); Debug.Assert(eq.Rhs != null); ls = null; Line line; /* bool matched = SatisfySpecialForm(eq, out line); * if (matched) * { * ls = new LineSymbol(line); * line.Label = eq.EqLabel; * if (eq.Traces.Count == 1) * { * var strategy = "Generate a line by manipulating algebraic equation."; * var newTrace = new Tuple<object, object>(strategy, eq.Traces[0].Item2); * ls.Traces.Add(newTrace); * //ls.ImportTrace(eq); * } * TraceInstructionalDesign.LineSlopeIntercepToGraph(ls); * return true; * } * * matched = SatisfyLineSlopeInterceptForm(eq, out line); * if (matched) * { * ls = new LineSymbol(line); * line.Label = eq.EqLabel; * if (eq.Traces.Count == 1) * { * var strategy = "Generate a line by manipulating algebraic equation."; * var newTrace = new Tuple<object, object>(strategy, eq.Traces[0].Item2); * ls.Traces.Add(newTrace); * //ls.ImportTrace(eq); * } * TraceInstructionalDesign.LineSlopeIntercepToGraph(ls); * return true; * }*/ /* if (!allowEval) * { * matched = SatisfyLineGeneralForm(eq, out line); * if (matched) * { * ls = new LineSymbol(line); * line.Label = eq.EqLabel; * return true; * } * matched = SatisfyLineSlopeInterceptForm(eq, out line); * if (matched) * { * ls = new LineSymbol(line); * line.Label = eq.EqLabel; * return true; * } * return false; * }*/ object obj; bool? result = eq.Eval(out obj, true, true); // without transitive equational rule. if (result != null) { return(false); } if (eq.CachedEntities.Count != 1) { return(false); } var outputEq = eq.CachedEntities.ToList()[0] as Equation; if (outputEq == null) { return(false); } bool matched = SatisfySpecialForm(outputEq, out line); if (matched) { ls = new LineSymbol(line); line.Label = eq.EqLabel; if (outputEq.Traces.Count == 1) { var strategy = "Generate a line by manipulating algebraic equation."; var newTrace = new Tuple <object, object>(strategy, outputEq.Traces[0].Item2); ls.Traces.Add(newTrace); //ls.ImportTrace(eq); } TraceInstructionalDesign.LineSlopeIntercepToGraph(ls); return(true); } //Equation Semantic Unification //general form of line equation ax+by+c=0 //point-slope form of line equation y = mx + b matched = SatisfyLineGeneralForm(outputEq, out line); if (matched) { ls = new LineSymbol(line); line.Label = eq.EqLabel; if (outputEq.Traces.Count == 1) { var strategy = "Generate a line by manipulating algebraic equation."; var newTrace = new Tuple <object, object>(strategy, outputEq.Traces[0].Item2); ls.Traces.Add(newTrace); //ls.ImportTrace(eq); } //Instruction Design TraceInstructionalDesign.FromLineGeneralFormToSlopeIntercept(ls); TraceInstructionalDesign.LineSlopeIntercepToGraph(ls); /* List<Tuple<object, object>> trace = eq.CloneTrace(); * ls.Traces.AddRange(trace); * List<Tuple<object, object>> lst = ls.Traces.Intersect(trace).ToList(); * if (lst.Count == 0) ls.Traces.AddRange(trace);*/ return(true); } matched = SatisfyLineSlopeInterceptForm(outputEq, out line); if (matched) { ls = new LineSymbol(line); line.Label = eq.EqLabel; if (outputEq.Traces.Count == 1) { var strategy = "Generate a line by manipulating algebraic equation."; var newTrace = new Tuple <object, object>(strategy, outputEq.Traces[0].Item2); ls.Traces.Add(newTrace); //ls.ImportTrace(eq); } TraceInstructionalDesign.LineSlopeIntercepToGraph(ls); /* List<Tuple<object,object>> trace = eq.CloneTrace(); * ls = new LineSymbol(line); * line.Label = eq.EqLabel; * ls.Traces.AddRange(trace); * List<Tuple<object, object>> lst = ls.Traces.Intersect(trace).ToList(); * if (lst.Count == 0) ls.Traces.AddRange(trace);*/ return(true); } eq.ClearTrace(); //eq.CachedEntities.Clear(); return(false); }