Exemplo n.º 1
0
        /// <summary>
        /// Compute the energy function that we are trying to maximize.
        /// </summary>
        /// <param name="sketch"></param>
        /// <returns>an unbounded double representing the energy of the sketch</returns>
        private double computeEnergy(FeatureSketch featureSketch)
        {
            Sketch.Sketch sketch = featureSketch.Sketch;
            featureSketch.hasConsistentSubstrokes();
            sketch.CheckConsistency();
            double energy    = 0;
            var    shapes    = sketch.ShapesL;
            int    numShapes = shapes.Count;
            int    numGates  = shapes.FindAll(s => LogicDomain.IsGate(s.Type)).Count;

            foreach (Shape shape in shapes)
            {
                // Add energy for every connection
#if false
                // This is a bad idea. It favors interpretations with more connections, which basically means
                // that everything should alternate wire-text-wire-text-wire-...
                foreach (Shape connected in shape.ConnectedShapes)
                {
                    if (connected == shape)
                    {
                        continue;
                    }
                    if (connected.Type != LogicDomain.WIRE)
                    {
                        continue;
                    }
                    double connectionDistance = double.PositiveInfinity;
                    foreach (EndPoint endpoint in connected.Endpoints)
                    {
                        connectionDistance = Math.Min(shape.minDistanceTo(endpoint.X, endpoint.Y), connectionDistance);
                    }
                    connectionDistance = Math.Max(connectionDistance, 0.001); // avoid problems when connection distance is close to zero
                    energy            += 1 + 1 / connectionDistance;
                }
#endif

                // Add the context match score
                energy += (double)_domain.ClosestContext(shape).Item1 / numShapes;

                // Get recognition results
                RecognitionResult result     = RecognizeAsType(shape, shape.Type, featureSketch);
                double            confidence = result.Confidence;

                // Add the recognition score
                energy += confidence / numShapes;

#if false
                // Gate orientation also contributes
                if (LogicDomain.IsGate(shape.Type))
                {
                    // Determine the recognizer's and the connector's orientation values,
                    // in the range [0, 2Pi]
                    double orientation      = result.Orientation;
                    double otherOrientation = _domain.OrientShape(shape, sketch);

                    // Orientation might be off by PI...
                    double dist1 = Math.Abs(otherOrientation - orientation);
                    double dist2 = Math.Abs(otherOrientation - Math.PI - orientation);
                    double dist  = Math.Min(dist1, dist2);

                    // Add orientation score
                    double twoPI = 2 * Math.PI;
                    energy += (1 - (dist / twoPI)) / numGates;
                }
#endif
            }
            return(energy);
        }