Exemplo n.º 1
0
        /// <summary>
        /// Searches for the best rendition given the variables and constraints.
        /// </summary>
        private Ptype.Mutable Search(State state)
        {
            //Removes any paths that conflict with the given constraints
            if (!Propagate(state.Parts.Values, state.Constraints))
            {
                return(state.BestRendition);
            }

            //Computes the cost of the full or partial assignment
            double cost = _costFunction.Cost(state.Parts, state.PositiveExamples, state.NegativeExamples, state.Cache);

            if (cost >= state.BestCost)
            {
                return(state.BestRendition);
            }


            //If the assignment is complete, then we know it's
            //the best cost so far, so assign it as the best rendition
            if (CompleteAssignment(state.Parts.Values))
            {
                Ptype.Mutable rendition = GetPrototype(state);
                if (!MatchesAnyNegatives(rendition, state.NegativeExamples))
                {
                    state.BestCost      = cost;
                    state.BestRendition = rendition;
                }
                return(state.BestRendition);
            }

            //Select the next variable to assign
            Part partToAssign = _orderSelecter.SelectNextPartToAssign(state.Parts, state.PositiveExamples, state.NegativeExamples);

            //Copy the variable's values so we can iterate through them
            ArrayList partValues = new ArrayList(partToAssign.CurrentValidValues);

            //Assign each value to the variable and continue searching
            foreach (object value in partValues)
            {
                partToAssign.AssignValue(value);
                Search(state);
                UndoPropogate(state.Parts.Values);
            }

            //Give the variable all of it's values back
            partToAssign.UnassignValueAndRestoreDomain();

            //Put all of the possible search paths back
            UndoPropogate(state.Parts.Values);


            return(state.BestRendition);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Calculates the cost of the specified prediction.
        /// </summary>
        /// <param name="prediction">The prediction.</param>
        /// <param name="oneHot">The one hot value.</param>
        /// <returns>The cost.</returns>
        public virtual double CalculateError(double[] prediction, int oneHot)
        {
            double error = _costFunction.Cost(prediction, oneHot.OneHot(prediction.Length));

            return(error);
        }