Ejemplo n.º 1
0
        private void RemoveRedundantSet()
        {
            SCPSolution improved  = _currSolution.Clone();
            List <int>  blacklist = new List <int>();

            _currSolution.ComputeAttributeRedundancies();

            foreach (var set in _currSolution.Sets)
            {
                bool useless = true;

                foreach (var a in set.Attributes)
                {
                    if (a.Redundancy <= 1)
                    {
                        useless = false;
                        break;
                    }
                }
                if (useless)
                {
                    set.Attributes.ForEach(a => a.Redundancy--);
                    blacklist.Add(set.Tag);
                }
            }

            blacklist.ForEach(b => improved.Sets.Remove(improved.Sets.Find(s => s.Tag.ToString() == b.ToString())));
            if (improved.Cost < _currSolution.Cost)
            {
                _currSolution = improved;
            }
        }
Ejemplo n.º 2
0
 public void MarkPrimeSets(SCPSolution solution)
 {
     solution.Sets.ForEach(set =>
     {
         set.Prime = IsPrimeSet(set);
     });
 }
Ejemplo n.º 3
0
        private void HorizantalSelection()
        {
            sp.Restart();

            SCPDataSet source = (SCPDataSet)_problem.Source.Clone();
            // source.Attributes = AttributeProbability(source.Attributes,source.Sets.Count);
            //source.Sets = Weighting(source.Sets);

            //List<SCPSet> candidates = GetCandidates(source.Sets);
            Stack <SCPSet>     starts    = new Stack <SCPSet>(source.Sets);
            List <SCPSolution> solutions = new List <SCPSolution>();

            while (starts.Count > 0)
            {
                starts.Peek().Visit = true;
                List <SCPSet> best  = Fitness(new List <SCPSet>()
                {
                    starts.Pop()
                }, ((SCPDataSet)source.Clone()).Sets);
                SCPSolution solution = new SCPSolution();
                solution.Sets = best;
                solutions.Add(RemoveRedundantSet(solution));
            }

            FSolution = solutions.OrderBy(s => s.Cost).First();

            sp.Stop();
        }
Ejemplo n.º 4
0
        public double Execute(IProblem problem)
        {
            _sp.Restart();
            Initializer(problem);
            _currSolution.ComputeAttributeRedundancies();
            List <double> cost_history = new List <double>();


            while (true)
            {
                if (cost_history.Count > 2)
                {
                    if (cost_history[cost_history.Count - 1] == cost_history[cost_history.Count - 2])
                    {
                        break;
                    }
                }

                //STEP#1
                ImproveByOneNeighborhood();
                if (_currSolution.Cost < _opSolution.Cost)
                {
                    _opSolution = _currSolution.Clone();
                }

                //STEP#2
                ImproveByTwoNeighborhoods();
                if (_currSolution.Cost < _opSolution.Cost)
                {
                    _opSolution = _currSolution.Clone();
                }

                //STEP#3
                ImproveByThreeNeighborhoods();
                if (_currSolution.Cost < _opSolution.Cost)
                {
                    _opSolution = _currSolution.Clone();
                }

                //STEP#4
                RemoveRedundantSet();
                if (_currSolution.Cost < _opSolution.Cost)
                {
                    _opSolution = _currSolution.Clone();
                }

                cost_history.Add(_opSolution.Cost);
            }



            _sp.Stop();
            return(OptimumSultion.Cost);
        }
Ejemplo n.º 5
0
        private bool IsFeasible(SCPSolution solution)
        {
            HashSet <int> problem_att = new HashSet <int>();

            _problem.Source.Attributes.ForEach(a => problem_att.Add(a.Tag));

            HashSet <int> solution_att = new HashSet <int>();

            solution.USet.ForEach(a => solution_att.Add(a));

            return(problem_att.IsSubsetOf(solution_att));
        }
Ejemplo n.º 6
0
        public void Test()
        {
            SCP        problem = (SCP)scpParser.Problem;
            SCPDataSet source  = (SCPDataSet)problem.Source.Clone();
            List <Tuple <SCPAttribute, SCPSet> > panel = new List <Tuple <SCPAttribute, SCPSet> >();

            source.Attributes.ForEach(a =>
            {
                a.Cost = a.GetCheapestCost();
                panel.Add(new Tuple <SCPAttribute, SCPSet>(a, a.GetCheapestSet()));
            });



            int         covered   = 0;
            SCPSolution solution2 = new SCPSolution();

            while (covered < source.Attributes.Count)
            {
                source.Attributes = source.Attributes.OrderByDescending(a => a.Cost).ToList();
                source.Attributes.ForEach(a1 =>
                {
                    a1.Visit = false;
                    a1.UsedIn.ForEach(s =>
                    {
                        s.Weight = s.Cost / s.Frequency;
                    });
                    a1.UsedIn = a1.UsedIn.OrderBy(ui => ui.Weight).ToList();
                });

                SCPSet set = source.Attributes.Where(at => at.Visit == false).First().UsedIn.First();

                set.Attributes.ForEach(a =>
                {
                    if (a.Visit == false)
                    {
                        a.Visit = true;
                        covered++;
                        a.UsedIn.ForEach(s => s.Frequency--);
                    }
                });
                if (set.Visit == false)
                {
                    solution2.Sets.Add(set);
                }
                set.Visit = true;
            }


            RemoveRedundantSet(solution2);

            bool result = IsFeasible(solution2, problem);
        }
Ejemplo n.º 7
0
        private bool IsFeasible(SCPSolution solution)
        {
            if (solution.Sets.Count <= 0)
            {
                return(false);
            }
            #region Step#1 Exhustive test
            List <int> attributes = new List <int>();
            solution.Sets.ForEach(s => s.Attributes.ForEach(a => attributes.Add(a.Tag)));
            attributes = attributes.OrderBy(a => a).ToList();
            for (int i = 1; i < attributes.Count; i++)
            {
                if (attributes[i] - attributes[i - 1] > 1)
                {
                    return(false);
                }
            }
            #endregion

            #region Step#2 Formulation test nx(n-1)/2
            int n    = _problem.Matrix.Size.X;
            int sum1 = n * (n + 1) / 2;

            int seed = attributes[0];
            int sum2 = seed;
            for (int i = 1; i < attributes.Count; i++)
            {
                if (attributes[i] != seed)
                {
                    seed  = attributes[i];
                    sum2 += seed;
                }
            }
            if (sum2 != sum1)
            {
                return(false);
            }
            #endregion

            #region Step#3 Hashing test
            int[] list = new int[n];
            foreach (int item in attributes)
            {
                list[item - 1] = item;
            }
            #endregion

            return(true);
        }
Ejemplo n.º 8
0
        private SCPSolution RemoveRedundantSet(SCPSolution solution)
        {
            if (solution.Sets.Count == 1)
            {
                return(solution);
            }

            SCPSolution improved  = solution.Clone();
            List <int>  blacklist = new List <int>();

            solution.ComputeAttributeRedundancies();
            solution.Sets = solution.Sets.OrderByDescending(s => s.Cost).ToList();
            foreach (var set in solution.Sets)
            {
                bool useless = true;

                foreach (var a in set.Attributes)
                {
                    if (a.Redundancy <= 1)
                    {
                        useless = false;
                        break;
                    }
                }
                if (useless)
                {
                    set.Attributes.ForEach(a =>
                    {
                        a.Redundancy--;
                    });
                    blacklist.Add(set.Tag);
                }
            }

            blacklist.ForEach(b => improved.Sets.Remove(improved.Sets.Find(s => s.Tag.ToString() == b.ToString())));
            if (improved.Cost < solution.Cost)
            {
                return(improved);
            }
            else
            {
                return(solution);
            }
        }
Ejemplo n.º 9
0
        private void improve()
        {
            while (true)
            {
                _problem.Source.Sets.ForEach(set =>
                {
                    if (_problem.Solution.Sets.Exists(s => s.Tag == set.Tag) == false)
                    {
                        _problem.Solution.Adapt(set);
                    }
                });

                SCPSolution solution = RemoveRedundantSet(_problem.Solution);
                if (solution.Cost < OptimumSultion.Cost)
                {
                    OptimumSultion = solution;
                }
            }
        }
Ejemplo n.º 10
0
        private void HorizantalSelection()
        {
            OptimumSultion = new SCPSolution();
            sp.Restart();
            int coveredatt = 0;
            int att_count  = _problem.Matrix.Size.X;

            while (coveredatt < att_count)
            {
                SCPSet set      = _problem.Source.GetLightestNotVisitedSet();
                bool   valuable = false;
                set.Attributes.ForEach(a1 =>
                {
                    if (a1.Visit == false)
                    {
                        if (_problem.Source.Attributes.Select(a => a.Tag).ToList().Exists(a => a == a1.Tag))
                        {
                            valuable = true;
                            a1.Visit = true;
                            coveredatt++;
                            a1.UsedIn.ForEach(s1 =>
                            {
                                s1.Frequency--;
                            });
                        }
                    }
                });
                if (set.Visit == false && valuable)
                {
                    ((SCPSolution)OptimumSultion).Sets.Add(set);
                }
                _problem.Source.Sets.Find(s => s.Tag == set.Tag).Visit = true;
                _problem.Source.Weighting();
            }
            sp.Stop();
        }
Ejemplo n.º 11
0
 private void Initializer(IProblem problem)
 {
     _problem      = (SCP)problem;
     _currSolution = _problem.Solution.Clone();
     _opSolution   = _currSolution.Clone();
 }
Ejemplo n.º 12
0
 public SCPImprovementGreedy()
 {
     FSolution = new SCPSolution();
 }
Ejemplo n.º 13
0
 public SCP(MatrixSize size)
 {
     Matrix   = new Matrix <int>(size);
     Solution = new SCPSolution();
 }
Ejemplo n.º 14
0
 public SCPGreedyImprovement()
 {
     OptimumSultion = new SCPSolution();
 }
Ejemplo n.º 15
0
 public SCPFirstOrderGreedy()
 {
     FSolution = new SCPSolution();
 }