예제 #1
0
        public void PerformLocalSearch(ACO.Ant <Edge> ant)
        {
            Solution <Edge> solution = ant.Solution;

            double bestQuality = solution.Quality;

            for (int elementIndex = solution.Components.Count - 1; elementIndex >= 0; elementIndex--)
            {
                DecisionComponent <Edge> remove = solution.Components[elementIndex];
                double previousQuality          = solution.Quality;

                solution.Components.RemoveAt(elementIndex);
                this._classificationQualityEvaluator.EvaluateSolutionQuality(solution);
                if (solution.Quality >= bestQuality)
                {
                    bestQuality             = solution.Quality;
                    ant.Trail[elementIndex] = -1;
                }
                else
                {
                    solution.Components.Insert(elementIndex, remove);
                    solution.Quality = previousQuality;
                }
            }
        }
예제 #2
0
파일: TAN.cs 프로젝트: skn123/iFourmi
        public iFourmi.DataMining.Model.IClassifier CreateClassifier()
        {
            Solution <Edge>           solution    = new Solution <Edge>();
            CyclicRelationInvalidator invalidator = new CyclicRelationInvalidator();

            invalidator.MaxDependencies = 1;

            ConstructionGraph <Edge> constructionGraph = ConstructionGraphBuilder.BuildBNConstructionGraph(this._trainingset.Metadata);
            CMICalculator            cmiCalculator     = new CMICalculator();

            cmiCalculator.Dataset = this._trainingset;

            constructionGraph.SetHeuristicValues(cmiCalculator, false);
            List <DecisionComponent <Edge> > components = constructionGraph.Components.OrderByDescending(e => e.Heuristic).ToList();

            while (components.Count != 0)
            {
                DecisionComponent <Edge> component = components[0];
                solution.Components.Add(component);
                invalidator.Invalidate(component, solution, constructionGraph);
                components = constructionGraph.Components.Where(e => e.IsValid).OrderByDescending(e => e.Heuristic).ToList();
            }


            BayesianNetworkClassifier BNClassifier = new BayesianNetworks.Model.BayesianNetworkClassifier(this._trainingset.Metadata, solution.ToList());

            BNClassifier.LearnParameters(this._trainingset);
            return(BNClassifier);
        }
예제 #3
0
        public BayesianNetwork LearnBayesianNetwork()
        {
            while (true)
            {
                List<DecisionComponent<Edge>> validComponents = this.ConstructionGraph.GetValidComponents();
                if (validComponents.Count == 0 || this._stop)
                    break;

                DecisionComponent<Edge> component = this.SelectBestComponent(validComponents);
                if (component == null)
                    break;

                this._bayesianNetwork.Connect(component.Element.ParentIndex, component.Element.ChildIndex);

                this._solution.Components.Add(component);
                this.EvaluateSolutionQuality(this._solution);
                

                this.Problem.ComponentInvalidator.Invalidate(component, this._solution, this.ConstructionGraph);

                if (this.OnProgress != null)
                    this.OnProgress(this, null);

            }

            BayesianNetwork network = new BayesianNetwork(this._trainingSet.Metadata, this._solution.ToList());
            network.LearnParameters(this._trainingSet);
            return network;

        }
예제 #4
0
        private List <int> GetAncestorIndexes(ConnectionDC input, Solution <ConnectionDC> solution)
        {
            int switchIndex = (InputUnitCount * HiddenUnitCount) + (InputUnitCount * OutputUnitCount) + (HiddenUnitCount * OutputUnitCount);

            List <int>  indexes = new List <int>();
            Stack <int> stack   = new Stack <int>();

            stack.Push(input.Connection.From);

            while (stack.Count != 0)
            {
                int index = stack.Pop();


                for (; switchIndex < solution.Components.Count; switchIndex++)
                {
                    DecisionComponent <ConnectionDC> component = solution.Components[switchIndex];

                    if (component.Element.Connection.From == index)
                    {
                        stack.Push(component.Element.Connection.To);
                        indexes.Add(index);
                    }
                }
            }

            return(indexes);
        }
예제 #5
0
        private void InvalidateLoops(ACO.DecisionComponent <ConnectionDC> input, ACO.Solution <ConnectionDC> solution, ACO.ConstructionGraph <ConnectionDC> graph)
        {
            int dependencies = 0;

            int switchIndex  = (InputUnitCount * HiddenUnitCount) + (InputUnitCount * OutputUnitCount) + (HiddenUnitCount * OutputUnitCount);
            int switchIndex2 = (InputUnitCount * HiddenUnitCount * 2) + (InputUnitCount * OutputUnitCount * 2) + (HiddenUnitCount * OutputUnitCount * 2);

            for (int index = switchIndex; index < solution.Components.Count; index++)
            {
                DecisionComponent <ConnectionDC> component = solution.Components[index];
                if (input.Element.Connection.To == component.Element.Connection.To)
                {
                    dependencies++;
                }
            }

            List <int> descendantIndexes = this.GetDescendantIndexes(input.Element, solution);
            List <int> ancestorIndexes   = this.GetAncestorIndexes(input.Element, solution);

            List <DecisionComponent <ConnectionDC> > validComponents = graph.GetValidComponents(switchIndex2, graph.Components.Length - switchIndex2);

            foreach (DecisionComponent <ConnectionDC> component in validComponents)
            {
                if (component.Element.Connection.To == input.Element.Connection.To && component.Element.Connection.From == input.Element.Connection.From)
                {
                    component.IsValid = false;
                    continue;
                }

                if (component.Element.Connection.To == input.Element.Connection.To)
                {
                    if (dependencies == MaxParents)
                    {
                        component.IsValid = false;
                        continue;
                    }
                }


                if (descendantIndexes.Contains(component.Element.Connection.From))
                {
                    if (ancestorIndexes.Contains(component.Element.Connection.To))
                    {
                        component.IsValid = false;
                        continue;
                    }
                }

                if (ancestorIndexes.Contains(component.Element.Connection.To))
                {
                    if (descendantIndexes.Contains(component.Element.Connection.From))
                    {
                        component.IsValid = false;
                        continue;
                    }
                }
            }
        }
예제 #6
0
 public void Invalidate(DecisionComponent <int> component, Solution <int> solution, ConstructionGraph <int> graph)
 {
     graph.Components[component.Index].IsValid = false;
     if (solution.Components.Count == this._clustersNumber)
     {
         foreach (DecisionComponent <int> current in graph.Components)
         {
             current.IsValid = false;
         }
     }
 }
예제 #7
0
        public static ConstructionGraph <int> BuildMBClusteringConstructionGraph(Dataset dataset, int clustersNumber)
        {
            int counter = 0;
            List <DecisionComponent <int> > decisionComponents = new List <DecisionComponent <int> >();

            foreach (Example example in dataset)
            {
                DecisionComponent <int> component = new DecisionComponent <int>(counter++, example.Index);
                decisionComponents.Add(component);
            }
            return(new ConstructionGraph <int>(decisionComponents.ToArray()));
        }
예제 #8
0
        //public static ConstructionGraph<Edge> BuildBNConstructionGraph(Metadata metadata, List<VariableTypeAssignment> variableTypeAssignments)
        //{
        //    int counter = 0;
        //    List<DecisionComponent<Edge>> decisionComponents = new List<DecisionComponent<Edge>>();
        //    for (int parentIndex = 0; parentIndex < metadata.Attributes.Length; parentIndex++)
        //        for (int childIndex = 0; childIndex < metadata.Attributes.Length; childIndex++)
        //            if (parentIndex != childIndex)
        //            {
        //                VariableType childVariableType = variableTypeAssignments.Find(v => v.VariableIndex == childIndex).Type;
        //                if(childVariableType== VariableType.Effect)
        //                    decisionComponents.Add(new DecisionComponent<Edge>(counter++, new Edge(parentIndex, childIndex)));

        //            }

        //    return new ConstructionGraph<Edge>(decisionComponents.ToArray());
        //}

        public static ConstructionGraph <ClusterExampleAssignment> BuildIBClusteringConstructionGraph(Dataset dataset, int clustersNumber)
        {
            int counter = 0;
            List <DecisionComponent <ClusterExampleAssignment> > decisionComponents = new List <DecisionComponent <ClusterExampleAssignment> >();

            foreach (Example example in dataset)
            {
                for (int clusterLabel = 0; clusterLabel < clustersNumber; clusterLabel++)
                {
                    DecisionComponent <ClusterExampleAssignment> component = new DecisionComponent <ClusterExampleAssignment>(counter++, new ClusterExampleAssignment(example.Index, clusterLabel));
                    decisionComponents.Add(component);
                }
            }
            return(new ConstructionGraph <ClusterExampleAssignment>(decisionComponents.ToArray()));
        }
예제 #9
0
        public override void CreateSoltion()
        {
            NNConnectorInvalidator invalidator = (NNConnectorInvalidator)this._colony.Problem.ComponentInvalidator;

            for (int i = 0; i < 2; i++)
            {
                this._colony.ConstructionGraph.Components[i].IsValid = true;
            }

            int length   = 0;
            int position = 0;

            while (true)
            {
                List <DecisionComponent <ConnectionDC> > validElements = null;

                if (!invalidator.SecondPhase)
                {
                    validElements = this._colony.ConstructionGraph.CalculateProbabilities(this._alpha, this._beta, position, 2);
                }
                else
                {
                    length        = this._colony.ConstructionGraph.Components.Length - position;
                    validElements = this._colony.ConstructionGraph.CalculateProbabilities(this._alpha, this._beta, position, length);
                }

                if (validElements.Count == 0)
                {
                    break;
                }

                DecisionComponent <ConnectionDC> element = this.SelectElementProbablistically(validElements);
                if (element == null)
                {
                    break;
                }
                this._solution.Components.Add(element);
                this._trail.Add(element.Index);

                if (!invalidator.SecondPhase)
                {
                    position += 2;
                }

                invalidator.Invalidate(element, this.Solution, this._colony.ConstructionGraph);
            }
        }
예제 #10
0
        public static ConstructionGraph <VariableTypeAssignment> BuildABCConstructionGraph(Metadata metadata)
        {
            int counter = 0;
            List <DecisionComponent <VariableTypeAssignment> > decisionComponents = new List <DecisionComponent <VariableTypeAssignment> >();

            foreach (DataMining.Data.Attribute attribute in metadata.Attributes)
            {
                DecisionComponent <VariableTypeAssignment> component1 = new DecisionComponent <VariableTypeAssignment>(counter++, new VariableTypeAssignment(attribute.Index, VariableType.Cause));
                decisionComponents.Add(component1);
                DecisionComponent <VariableTypeAssignment> component2 = new DecisionComponent <VariableTypeAssignment>(counter++, new VariableTypeAssignment(attribute.Index, VariableType.Effect));
                decisionComponents.Add(component2);
                DecisionComponent <VariableTypeAssignment> component3 = new DecisionComponent <VariableTypeAssignment>(counter++, new VariableTypeAssignment(attribute.Index, VariableType.None));
                decisionComponents.Add(component3);
            }

            return(new ConstructionGraph <VariableTypeAssignment>(decisionComponents.ToArray()));
        }
        public void PerformLocalSearch(Ant <VariableTypeAssignment> ant)
        {
            Solution <VariableTypeAssignment> solution = ant.Solution;

            double bestQuality = solution.Quality;

            for (int index = 0; index < solution.Components.Count; index++)
            {
                int firstOptionIndex   = solution.Components[index].Element.VariableIndex * 3;
                int currentOptionIndex = solution.Components[index].Index;

                for (int i = firstOptionIndex; i < firstOptionIndex + 3; i++)
                {
                    double previousQuality = solution.Quality;

                    if (i == currentOptionIndex)
                    {
                        continue;
                    }

                    DecisionComponent <VariableTypeAssignment> current = solution.Components[index];
                    solution.Components[index] = this._constructionGraph.Components[i];
                    this._classificationQualityEvaluator.EvaluateSolutionQuality(solution);

                    if (solution.Quality >= bestQuality)
                    {
                        bestQuality      = solution.Quality;
                        ant.Trail[index] = i;
                    }

                    else
                    {
                        solution.Components[index] = current;
                        solution.Quality           = previousQuality;
                    }
                }
            }
        }
예제 #12
0
        public override void CreateSolution()
        {
            while (true)
            {
                List<DecisionComponent<Edge>> validElements = this.ConstructionGraph.GetValidComponents();
                if (validElements.Count == 0 || this._stop)
                    break;

                DecisionComponent<Edge> element = this.SelectBestElement(validElements);
                if (element == null)
                    break;

                this._solution.Components.Add(element);
                this.EvaluateSolutionQuality(this._solution);
                
                this.Problem.ComponentInvalidator.Invalidate(element, this._solution, this.ConstructionGraph);

                if (this.OnProgress != null)
                    this.OnProgress(this, null);

            }
            
        }
예제 #13
0
        public void PerformLocalSearch(Ant <DRComponent> ant)
        {
            Solution <DRComponent> solution = ant.Solution;

            double bestQuality = solution.Quality;

            for (int elementIndex = 0; elementIndex < solution.Components.Count; elementIndex++)
            {
                DecisionComponent <DRComponent> toChange = solution.Components[elementIndex];
                double previousQuality = solution.Quality;

                toChange.Element.Include = !toChange.Element.Include;
                this._qualityEvaluator.EvaluateSolutionQuality(solution);
                if (solution.Quality >= bestQuality)
                {
                    bestQuality = solution.Quality;
                }
                else
                {
                    toChange.Element.Include = !toChange.Element.Include;
                    solution.Quality         = previousQuality;
                }
            }
        }
예제 #14
0
        public override void CreateSolution()
        {
            while (true)
            {
                List<DecisionComponent<Edge>> validComponents = this.ConstructionGraph.GetValidComponents();
                if (validComponents.Count == 0 || this._stop)
                    break;

                DecisionComponent<Edge> component = this.SelectBestComponent(validComponents);
                if (component == null)
                    break;

                this._bayesianNetwork.Connect(component.Element.ParentIndex, component.Element.ChildIndex);

                this._solution.Components.Add(component);
                this._solution.Quality = this._variablesK2.Sum();

                this.Problem.ComponentInvalidator.Invalidate(component, this._solution, this.ConstructionGraph);

                if (this.OnProgress != null)
                    this.OnProgress(this, null);

            }
        }
예제 #15
0
        private void PerformLocalSearch()
        {
            double bestQuality = this._iterationBestAnts[0].Solution.Quality;

            for (int index = 0; index < this._iterationBestAnts.Count; index++)
            {
                Ant <Edge>      ant      = this._iterationBestAnts[index];
                Solution <Edge> solution = ant.Solution;

                for (int elementIndex = solution.Components.Count - 1; elementIndex >= 0; elementIndex--)
                {
                    DecisionComponent <Edge> remove = solution.Components[elementIndex];
                    //double previousQuality = solution.Quality;

                    solution.Components.RemoveAt(elementIndex);

                    double currentQuality = this.EvaluateQuality(this._iterationBestAnts);

                    if (currentQuality >= bestQuality)
                    {
                        bestQuality             = currentQuality;
                        ant.Trail[elementIndex] = -1;
                    }
                    else
                    {
                        solution.Components.Insert(elementIndex, remove);
                        //solution.Quality = previousQuality;
                    }
                }
            }

            foreach (Ant <Edge> ant in this._iterationBestAnts)
            {
                ant.Solution.Quality = bestQuality;
            }
        }