/// <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); }
private bool MatchesAnyNegatives(Ptype.Mutable ptype, IEnumerable <Bitmap> negatives) { foreach (Bitmap negative in negatives) { IEnumerable <Tree> occurrences = PrototypeDetectionLayer.FindPrototypeOccurrences(negative, ptype); foreach (Tree occurrence in occurrences) { if (occurrence != null && occurrence.Left == 0 && occurrence.Top == 0 && occurrence.Width == negative.Width && occurrence.Height == negative.Height) { return(true); } } } return(false); }
private Ptype.Mutable GetPrototype(State state) { Ptype.Mutable ptype = _constructor.ConstructPtype(state.Parts, state.PositiveExamples, state.NegativeExamples, state.Cache); ptype.Model = _model; return(ptype); }