Пример #1
0
        public static bool AcquireCongruences(KnownMeasurementsAggregator known, List <GroundedClause> clauses)
        {
            bool addedKnown = false;

            foreach (GeometryTutorLib.ConcreteAST.GroundedClause clause in clauses)
            {
                GeometryTutorLib.ConcreteAST.CongruentSegments cs = clause as GeometryTutorLib.ConcreteAST.CongruentSegments;
                if (cs != null && !cs.IsReflexive())
                {
                    double length1 = known.GetSegmentLength(cs.cs1);
                    double length2 = known.GetSegmentLength(cs.cs2);

                    if (length1 >= 0 && length2 < 0)
                    {
                        if (known.AddSegmentLength(cs.cs2, length1))
                        {
                            addedKnown = true;
                        }
                    }
                    if (length1 <= 0 && length2 > 0)
                    {
                        if (known.AddSegmentLength(cs.cs1, length2))
                        {
                            addedKnown = true;
                        }
                    }
                    // else: both known
                }

                GeometryTutorLib.ConcreteAST.CongruentAngles cas = clause as GeometryTutorLib.ConcreteAST.CongruentAngles;
                if (cas != null && !cas.IsReflexive())
                {
                    double measure1 = known.GetAngleMeasure(cas.ca1);
                    double measure2 = known.GetAngleMeasure(cas.ca2);

                    if (measure1 >= 0 && measure2 < 0)
                    {
                        if (known.AddAngleMeasureDegree(cas.ca2, measure1))
                        {
                            addedKnown = true;
                        }
                    }
                    if (measure1 <= 0 && measure2 > 0)
                    {
                        if (known.AddAngleMeasureDegree(cas.ca1, measure2))
                        {
                            addedKnown = true;
                        }
                    }
                    // else: both known
                }
            }

            return(addedKnown);
        }
Пример #2
0
        private static bool HandleRatioEquation(KnownMeasurementsAggregator known, SegmentRatioEquation theEq)
        {
            double topLeft     = known.GetSegmentLength(theEq.lhs.smallerSegment);
            double bottomLeft  = known.GetSegmentLength(theEq.lhs.largerSegment);
            double topRight    = known.GetSegmentLength(theEq.rhs.smallerSegment);
            double bottomRight = known.GetSegmentLength(theEq.rhs.largerSegment);

            int unknown = 0;

            if (topLeft <= 0)
            {
                unknown++;
            }
            if (bottomLeft <= 0)
            {
                unknown++;
            }
            if (topRight <= 0)
            {
                unknown++;
            }
            if (bottomRight <= 0)
            {
                unknown++;
            }
            if (unknown != 1)
            {
                return(false);
            }

            if (topLeft <= 0)
            {
                return(known.AddSegmentLength(theEq.lhs.smallerSegment, (topRight / bottomRight) * bottomLeft));
            }
            else if (bottomLeft <= 0)
            {
                return(known.AddSegmentLength(theEq.lhs.largerSegment, topLeft * (bottomRight / topRight)));
            }
            else if (topRight <= 0)
            {
                return(known.AddSegmentLength(theEq.rhs.smallerSegment, (topLeft / bottomLeft) * bottomRight));
            }
            else if (bottomRight <= 0)
            {
                return(known.AddSegmentLength(theEq.rhs.largerSegment, topRight * (bottomLeft / topLeft)));
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        public static bool AcquireCongruences(KnownMeasurementsAggregator known, List<GroundedClause> clauses)
        {
            bool addedKnown = false;

            foreach (GeometryTutorLib.ConcreteAST.GroundedClause clause in clauses)
            {
                GeometryTutorLib.ConcreteAST.CongruentSegments cs = clause as GeometryTutorLib.ConcreteAST.CongruentSegments;
                if (cs != null && !cs.IsReflexive())
                {
                    double length1 = known.GetSegmentLength(cs.cs1);
                    double length2 = known.GetSegmentLength(cs.cs2);

                    if (length1 >= 0 && length2 < 0)
                    {
                        if (known.AddSegmentLength(cs.cs2, length1)) addedKnown = true;
                    }
                    if (length1 <= 0 && length2 > 0)
                    {
                        if (known.AddSegmentLength(cs.cs1, length2)) addedKnown = true;
                    }
                    // else: both known
                }

                GeometryTutorLib.ConcreteAST.CongruentAngles cas = clause as GeometryTutorLib.ConcreteAST.CongruentAngles;
                if (cas != null && !cas.IsReflexive())
                {
                    double measure1 = known.GetAngleMeasure(cas.ca1);
                    double measure2 = known.GetAngleMeasure(cas.ca2);

                    if (measure1 >= 0 && measure2 < 0)
                    {
                        if (known.AddAngleMeasureDegree(cas.ca2, measure1)) addedKnown = true;
                    }
                    if (measure1 <= 0 && measure2 > 0)
                    {
                        if (known.AddAngleMeasureDegree(cas.ca1, measure2)) addedKnown = true;
                    }
                    // else: both known
                }
            }

            return addedKnown;
        }
Пример #4
0
        private static bool AddKnowns(KnownMeasurementsAggregator known, List <KeyValuePair <Segment, double> > pairs)
        {
            if (!pairs.Any())
            {
                return(false);
            }

            bool change = false;

            foreach (KeyValuePair <Segment, double> rightPair in pairs)
            {
                // Do we know this already?
                if (known.GetSegmentLength(rightPair.Key) < 0)
                {
                    change = true;
                    known.AddSegmentLength(rightPair.Key, rightPair.Value);
                }
            }
            return(change);
        }
Пример #5
0
        //
        // A right triangle means we can apply the pythagorean theorem to acquire an unknown.
        //
        public static bool HandleTriangle(KnownMeasurementsAggregator known, Triangle tri)
        {
            if (tri == null)
            {
                return(false);
            }

            KeyValuePair <Segment, double> pair = tri.PythagoreanTheoremApplies(known);

            if (pair.Value > 0)
            {
                // Do we know this already?
                if (known.GetSegmentLength(pair.Key) > 0)
                {
                    return(false);
                }

                // We don't know it, we add it.
                known.AddSegmentLength(pair.Key, pair.Value);
                return(true);
            }
            else
            {
                if (AddKnowns(known, tri.IsoscelesRightApplies(known)))
                {
                    return(true);
                }
                if (AddKnowns(known, tri.CalculateBaseOfIsosceles(known)))
                {
                    return(true);
                }
                if (AddKnowns(known, tri.RightTriangleTrigApplies(known)))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #6
0
        //
        // (1) Make a copy
        // (2) Collect the equation terms.
        // (3) Are all but one known?
        // (4) Substitute
        // (5) Simplify
        // (6) Acquire the unknown and its value.
        // (7) Add to the list of knowns.
        //
        private static bool HandleSegmentEquation(KnownMeasurementsAggregator known, List<GroundedClause> clauses, SegmentEquation theEq)
        {
            if (theEq.GetAtomicity() == Equation.BOTH_ATOMIC) return HandleSimpleSegmentEquation(known, theEq);

            // CTA: Verify this calls the correct Equation deep copy mechanism.
            // (1) Make a copy
            SegmentEquation copy = (SegmentEquation)theEq.DeepCopy();

            // (2) Collect the equation terms.
            List<GroundedClause> left = copy.lhs.CollectTerms();
            double[] leftVal = new double[left.Count];
            List<GroundedClause> right = copy.rhs.CollectTerms();
            double[] rightVal = new double[right.Count];

            // (3) Are all but one term known?
            int unknownCount = 0;
            for (int ell = 0; ell < left.Count; ell++)
            {
                if (left[ell] is NumericValue) leftVal[ell] = (left[ell] as NumericValue).DoubleValue;
                else
                {
                    leftVal[ell] = known.GetSegmentLength(left[ell] as Segment);
                    if (leftVal[ell] <= 0) unknownCount++;
                }
            }
            for (int r = 0; r < right.Count; r++)
            {
                if (right[r] is NumericValue) rightVal[r] = (right[r] as NumericValue).DoubleValue;
                else
                {
                    rightVal[r] = known.GetSegmentLength(right[r] as Segment);
                    if (rightVal[r] <= 0) unknownCount++;
                }
            }

            // We can't solve for more or less than one unknown.
            if (unknownCount != 1) return false;

            //
            // (4) Substitute
            //
            for (int ell = 0; ell < left.Count; ell++)
            {
                if (leftVal[ell] > 0) copy.Substitute(left[ell], new NumericValue(leftVal[ell]));
            }
            for (int r = 0; r < right.Count; r++)
            {
                if (rightVal[r] > 0) copy.Substitute(right[r], new NumericValue(rightVal[r]));
            }

            //
            // (5) Simplify
            //
            SegmentEquation simplified = (SegmentEquation)GenericInstantiator.Simplification.Simplify(copy);

            return HandleSimpleSegmentEquation(known, simplified);
        }
Пример #7
0
        private static bool HandleRatioEquation(KnownMeasurementsAggregator known, SegmentRatioEquation theEq)
        {
            double topLeft = known.GetSegmentLength(theEq.lhs.smallerSegment);
            double bottomLeft = known.GetSegmentLength(theEq.lhs.largerSegment);
            double topRight = known.GetSegmentLength(theEq.rhs.smallerSegment);
            double bottomRight = known.GetSegmentLength(theEq.rhs.largerSegment);

            int unknown = 0;
            if (topLeft <= 0) unknown++;
            if (bottomLeft <= 0) unknown++;
            if (topRight <= 0) unknown++;
            if (bottomRight <= 0) unknown++;
            if (unknown != 1) return false;

            if (topLeft <= 0)
            {
                return known.AddSegmentLength(theEq.lhs.smallerSegment, (topRight / bottomRight) * bottomLeft);
            }
            else if (bottomLeft <= 0)
            {
                return known.AddSegmentLength(theEq.lhs.largerSegment, topLeft * (bottomRight / topRight));
            }
            else if (topRight <= 0)
            {
                return known.AddSegmentLength(theEq.rhs.smallerSegment, (topLeft / bottomLeft) * bottomRight);
            }
            else if (bottomRight <= 0)
            {
                return known.AddSegmentLength(theEq.rhs.largerSegment, topRight * (bottomLeft / topLeft));
            }
            else return false;
        }
Пример #8
0
        private static bool AddKnowns(KnownMeasurementsAggregator known, List<KeyValuePair<Segment, double>> pairs)
        {
            if (!pairs.Any()) return false;

            bool change = false;
            foreach (KeyValuePair<Segment, double> rightPair in pairs)
            {
                // Do we know this already?
                if (known.GetSegmentLength(rightPair.Key) < 0)
                {
                    change = true;
                    known.AddSegmentLength(rightPair.Key, rightPair.Value);
                }
            }
            return change;
        }
Пример #9
0
        //
        // A right triangle means we can apply the pythagorean theorem to acquire an unknown.
        //
        public static bool HandleTriangle(KnownMeasurementsAggregator known, Triangle tri)
        {
            if (tri == null) return false;

            KeyValuePair<Segment, double> pair = tri.PythagoreanTheoremApplies(known);

            if (pair.Value > 0)
            {
                // Do we know this already?
                if (known.GetSegmentLength(pair.Key) > 0) return false;

                // We don't know it, we add it.
                known.AddSegmentLength(pair.Key, pair.Value);
                return true;
            }
            else
            {
                if (AddKnowns(known, tri.IsoscelesRightApplies(known))) return true;
                if (AddKnowns(known, tri.CalculateBaseOfIsosceles(known))) return true;
                if (AddKnowns(known, tri.RightTriangleTrigApplies(known))) return true;
            }

            return false;
        }
Пример #10
0
        //
        // (1) Make a copy
        // (2) Collect the equation terms.
        // (3) Are all but one known?
        // (4) Substitute
        // (5) Simplify
        // (6) Acquire the unknown and its value.
        // (7) Add to the list of knowns.
        //
        private static bool HandleSegmentEquation(KnownMeasurementsAggregator known, List <GroundedClause> clauses, SegmentEquation theEq)
        {
            if (theEq.GetAtomicity() == Equation.BOTH_ATOMIC)
            {
                return(HandleSimpleSegmentEquation(known, theEq));
            }

            // CTA: Verify this calls the correct Equation deep copy mechanism.
            // (1) Make a copy
            SegmentEquation copy = (SegmentEquation)theEq.DeepCopy();

            // (2) Collect the equation terms.
            List <GroundedClause> left = copy.lhs.CollectTerms();

            double[] leftVal            = new double[left.Count];
            List <GroundedClause> right = copy.rhs.CollectTerms();

            double[] rightVal = new double[right.Count];

            // (3) Are all but one term known?
            int unknownCount = 0;

            for (int ell = 0; ell < left.Count; ell++)
            {
                if (left[ell] is NumericValue)
                {
                    leftVal[ell] = (left[ell] as NumericValue).DoubleValue;
                }
                else
                {
                    leftVal[ell] = known.GetSegmentLength(left[ell] as Segment);
                    if (leftVal[ell] <= 0)
                    {
                        unknownCount++;
                    }
                }
            }
            for (int r = 0; r < right.Count; r++)
            {
                if (right[r] is NumericValue)
                {
                    rightVal[r] = (right[r] as NumericValue).DoubleValue;
                }
                else
                {
                    rightVal[r] = known.GetSegmentLength(right[r] as Segment);
                    if (rightVal[r] <= 0)
                    {
                        unknownCount++;
                    }
                }
            }

            // We can't solve for more or less than one unknown.
            if (unknownCount != 1)
            {
                return(false);
            }

            //
            // (4) Substitute
            //
            for (int ell = 0; ell < left.Count; ell++)
            {
                if (leftVal[ell] > 0)
                {
                    copy.Substitute(left[ell], new NumericValue(leftVal[ell]));
                }
            }
            for (int r = 0; r < right.Count; r++)
            {
                if (rightVal[r] > 0)
                {
                    copy.Substitute(right[r], new NumericValue(rightVal[r]));
                }
            }

            //
            // (5) Simplify
            //
            SegmentEquation simplified = (SegmentEquation)GenericInstantiator.Simplification.Simplify(copy);

            return(HandleSimpleSegmentEquation(known, simplified));
        }
Пример #11
0
        //
        // Filter the list of unknowns by any new information.
        //
        private List<Segment> AcquireCurrentUnknowns(KnownMeasurementsAggregator known, List<Segment> unknowns)
        {
            List<Segment> newUnknowns = new List<Segment>();

            foreach (Segment unknown in unknowns)
            {
                if (known.GetSegmentLength(unknown) < 0) newUnknowns.Add(unknown);
            }

            return newUnknowns;
        }