コード例 #1
0
        public static double NumericDistance(CombinatorialSemantics desired, Semantics candidate, IEnumerable <Semantics> allCandidates)
        {
            double distance = 0.0;

            for (int i = 0; i < Math.Min(desired.Length, candidate.Length); i++)
            {
                var desiredSemantics = desired[i];
                foreach (var value in desiredSemantics)
                {
                    if (candidate[i] == null)
                    {
                        // if candidate has no semantic value, take the worst distance of the other candidates
                        // if no other candidate has a semantic value, ignore i'th value

                        /*bool distanceFound;
                         * double worstDistance = GetWorstDistance((int) value, allCandidates, i, out distanceFound);
                         * if (distanceFound) {
                         *      distance += worstDistance;
                         * }*/
                    }
                    else
                    {
                        var dist = Math.Abs((int)value - (int)candidate[i]);
                        distance += dist;
                    }
                }
            }
            return(distance);
        }
コード例 #2
0
        public static double HammingDistance(CombinatorialSemantics desired, Semantics candidate, IEnumerable <Semantics> allCandidates)
        {
            double distance = 0.0;

            for (int i = 0; i < Math.Min(desired.Length, candidate.Length); i++)
            {
                var semantics = desired[i];
                foreach (var value in semantics)
                {
                    if (candidate == null || !value.Equals(candidate[i]))
                    {
                        distance++;
                    }
                }
            }
            return(distance);
        }
コード例 #3
0
        public TreeNode GetBySemantics(Type type, CombinatorialSemantics semantics)
        {
            var items = GetItemsByType(type);

            if (items.Count > 0)
            {
                PoolItem bestItem;
                if (type == typeof(bool))
                {
                    bestItem = GetBestItem(items, semantics, CombinatorialSemantics.HammingDistance);
                }
                else
                {
                    bestItem = GetBestItem(items, semantics, CombinatorialSemantics.NumericDistance);
                }
                return(bestItem.Node);
            }
            return(null);
        }
コード例 #4
0
        // Pawlak et al. Algorithm 1 in paper Semantic Backpropagation for Designing Search Operators in GP:
        // Naming: n ... node, p ... root, t ... target
        // semantic types may be mixed up in expression: e.g. bool ret = (a + b) == (c - a)
        // => have to use object type instead of one generic type
        public CombinatorialSemantics Propagate(TreeNode root, TreeNode node, Semantics target)
        {
            CombinatorialSemantics semantics = new CombinatorialSemantics(target.Length);
            var path = root.GetPathTo(node);

            for (int i = 0; i < target.Length; i++)                     // for all ti element of t do:
            {
                ISet <object> currentValueSet = new HashSet <object>(); // Di
                currentValueSet.Add(target[i]);                         // Di <- { ti }
                var  currentNode    = root;                             // a <- p
                bool ambiguityFound = false;                            // * not element of Di
                int  pathIndex      = 0;                                // index of path element
                while (currentNode != node && currentValueSet.Count > 0 && !ambiguityFound)
                {
                    int k = currentNode.Children.IndexOf(path[pathIndex]);

                    ISet <object> valueSet       = new HashSet <object>();              // D' <- {}
                    var           invertibleExpr = currentNode as IInvertible;          // if not invertible, loop will end
                    if (invertibleExpr != null && invertibleExpr.IsInvertible)
                    {
                        foreach (var desiredValue in currentValueSet)
                        {
                            bool ambiguous;
                            var  complementValue = invertibleExpr.GetComplementValue(k, i);
                            if (complementValue != null)                                // complement value can be null if semantics not evaluated
                            {
                                valueSet.UnionWith(invertibleExpr.Invert(desiredValue, k, complementValue, out ambiguous));
                                if (ambiguous)
                                {
                                    ambiguityFound = true;
                                }
                            }
                        }
                    }
                    // valueSet is empty in case of ambiguity (* element of D')
                    currentValueSet = valueSet;                     // Di <- D'
                    currentNode     = path[pathIndex++];            // a <- Child(a, n)
                }
                semantics[i] = currentValueSet;
            }
            return(semantics);
        }
コード例 #5
0
        private PoolItem GetBestItem(IList <PoolItem> items, CombinatorialSemantics semantics, DistanceMeasure distanceMeasure)
        {
            double   bestDistance = Double.MaxValue;
            PoolItem bestItem     = null;
            var      allSemantics = items.Where(item => item is ISemanticsHolder && ((ISemanticsHolder)item.Node).SemanticsEvaluated)
                                    .Select(item => ((ISemanticsHolder)item.Node).Semantics);

            foreach (var item in items)
            {
                var semanticsNode = item.Node as ISemanticsHolder;
                if (semanticsNode != null && semanticsNode.SemanticsEvaluated)
                {
                    var distance = distanceMeasure(semantics, semanticsNode.Semantics, allSemantics);
                    if (distance < bestDistance)
                    {
                        bestDistance = distance;
                        bestItem     = item;
                    }
                }
            }
            return(bestItem);
        }
コード例 #6
0
 protected virtual TreeNode DoLibrarySearch(Individual individual, ISemanticSubTreePool subTreePool,
                                            Type nodeType, CombinatorialSemantics desiredSemantics)
 {
     return(subTreePool.GetBySemantics(nodeType, desiredSemantics));
 }