Example #1
0
        private SCPSet CreateNewVirtualSet(List <Panel> list)
        {
            SCPSet candidate = new SCPSet();

            candidate.Tag = 0;

            list.ForEach(i =>
            {
                if (candidate.Parents == null)
                {
                    candidate.Parents = new List <int>();
                    candidate.Parents.Add(i.SetTag);
                    candidate.Cost += i.SetCost;
                }
                else if (candidate.Parents.Contains(i.SetTag) == false) //Don`t add the same set more than twice and don`t cost the same set more than once
                {
                    candidate.Parents.Add(i.SetTag);
                    candidate.Cost += i.SetCost;
                }
                SCPAttribute attribute = new SCPAttribute();
                attribute.Tag          = i.AttributeTag;
                attribute.Cost         = i.AttributeCost;
                candidate.Attributes.Add(attribute);
            });
            return(candidate);
        }
Example #2
0
        private void VerticalSelection()
        {
            sp.Restart();
            AttributeWeighting();

            foreach (var att in _problem.Source.Attributes)
            {
                SCPSet set  = _problem.Source.Sets.Find(s => s.Tag == att.GetCheapestSetTag());
                bool   flag = false;
                set.Attributes.ForEach(a1 =>
                {
                    if (a1.Visit == false)
                    {
                        a1.Visit = true;
                        flag     = true;
                    }
                });
                if (flag)
                {
                    ((SCPSolution)OptimumSultion).Sets.Add(set);
                }
            }
            sp.Stop();
            _problem.Solution = (SCPSolution)OptimumSultion;
        }
Example #3
0
        private List <SCPSet> Order(double budget)
        {
            List <SCPSet>         candidate = new List <SCPSet>();
            List <HashSet <int> > subsets   = new List <HashSet <int> >();
            List <Tuple <double, int, HashSet <int> > > panel = new List <Tuple <double, int, HashSet <int> > >();

            subsets = SubsetMatrix();
            subsets = subsets.OrderByDescending(s => s.Count).ToList();

            foreach (var set in subsets)
            {
                List <SCPSet> neighbors = _problem.Source.GetNeighbors(set.ToArray());
                if (neighbors.Count > 0)
                {
                    SCPSet neighbor = (SCPSet)neighbors.OrderBy(n => n.Cost).First().Clone();
                    panel.Add(new Tuple <double, int, HashSet <int> >(neighbor.Cost, neighbor.Tag, set));
                }
            }

            panel = panel.OrderBy(p => p.Item1 / p.Item3.Count).ToList();
            foreach (var p in panel)
            {
                if (p.Item1 < budget)
                {
                    candidate.Add(_problem.Source.Sets.Find(s => s.Tag == p.Item2));
                    budget -= p.Item1;
                }
            }
            return(candidate);
        }
Example #4
0
        private void ImproveByThreeNeighborhoods()
        {
            //Create panel
            List <Panel> panels = null;

            panels = GenerateAuctionPanel();

            //Create possibility of pairs in the panel by making possibility matrix
            Matrix <int> matrix = new Matrix <int>();

            matrix = GeneratePossibilityMatrix(panels);

            //TODO: Create new pair (set) and compair with neighbors to see if it is profitable exchange
            List <SCPSet> candidates = GenerateMultiNeighborCandidateSet(matrix, panels);

            List <SCPSet> removes = new List <SCPSet>();
            List <SCPSet> adds    = new List <SCPSet>();

            _currSolution.Sets = _currSolution.Sets.OrderBy(s => s.Tag).ToList();
            candidates.ForEach(set =>
            {
                List <SCPSet> neighbors = _problem.Source.GetNeighbors(set);
                if (neighbors.Count > 1)
                {
                    SCPSet best = neighbors.OrderBy(n => n.Cost).First();
                    if (set.Cost > best.Cost)
                    {
                        adds.Add(best);
                        removes.Add(set);
                    }
                }
            });

            //Add
            adds.ForEach(a => _currSolution.Sets.Add(a));

            //Remove
            removes.ForEach(b =>
            {
                b.Parents.ForEach(p =>
                {
                    _currSolution.ComputeAttributeRedundancies();
                    SCPSet set = _currSolution.Sets.Find(s => s.Tag == p);
                    if (set != null)
                    {
                        _currSolution.RemoveSet(set);
                    }
                });
            });

            //Fix the died attributes by finding a cheapest set for them
            List <SCPSet> recovery = RecoverDeadAttributes(_currSolution.LostedAttributes.Select(l => l.Item2).ToList());

            _currSolution.Sets.AddRange(recovery);
        }
Example #5
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);
        }
Example #6
0
        public bool IsPrimeSet(SCPSet set)
        {
            SCPSet scpset = Source.Sets.Where(s => s.Tag == set.Tag).First();

            foreach (var attribute in scpset.Attributes)
            {
                if (attribute.GetCheapestSet().Tag == scpset.Tag)
                {
                    return(true);
                }
            }
            return(false);
        }
Example #7
0
        public List <SCPSet> GetCandidateSets(SCPSet set)
        {
            List <SCPSet> candidates = new List <SCPSet>();

            foreach (var candidate in Source.Sets)
            {
                if (set.IsSubset(candidate))
                {
                    candidates.Add(candidate);
                }
            }
            return(candidates);
        }
Example #8
0
        private SCPSet GetTheMostExSet(List <Tuple <SCPAttribute, SCPSet> > panel)
        {
            SCPSet set = new SCPSet();

            panel.ForEach(s =>
            {
                if (s.Item1.Visit == false)
                {
                    if (s.Item2.Cost > set.Cost)
                    {
                        set = s.Item2;
                    }
                }
            });
            return(set);
        }
Example #9
0
        private List <SCPSet> RecoverDeadAttributes(List <int> losted)
        {
            List <SCPSet> list      = new List <SCPSet>();
            Random        rnd       = new Random();
            bool          recovered = false;
            int           split     = 0;

            while (recovered == false)
            {
                List <List <int> > partitions = new List <List <int> >();

                if (split != 0)
                {
                    int size = (int)Math.Round(losted.Count / (double)split);
                    for (int i = 0; i < split; i++)
                    {
                        partitions.Add(losted.Skip(i * size).Take(size).ToList());
                    }
                }
                else
                {
                    partitions.Add(losted);
                }

                partitions.ForEach(p =>
                {
                    List <SCPSet> neighbors = _problem.Source.GetNeighbors(p);

                    if (neighbors.Count > 0)
                    {
                        SCPSet best = neighbors.OrderBy(n => n.Cost).First();
                        list.Add(best);
                        losted = losted.Except(p).ToList();
                    }
                });
                if (losted.Count == 0)
                {
                    recovered = true;
                }
                else
                {
                    split = rnd.Next(1, (losted.Count / rnd.Next(1, losted.Count)) + 1);
                }
            }

            return(list);
        }
Example #10
0
        private void ImproveBySecondNeighborhood()
        {
            int        index           = 0;
            int        setcount        = _currSolution.Sets.Count;
            List <int> need_to_covered = new List <int>();

            _currSolution.ComputeAttributeRedundancies();
            while (index < setcount)
            {
                SCPSet target = _currSolution.Sets[index];

                foreach (var attribute in target.Attributes)
                {
                    if (attribute.Redundancy <= 1)
                    {
                        need_to_covered.Add(attribute.Tag);
                    }
                }
                if (index % 2 == 0)
                {
                    index++;
                    continue;
                }

                List <SCPSet> neighbors = _problem.Source.GetNeighbors(need_to_covered);

                foreach (var set in neighbors)
                {
                    if ((set.Cost - target.Cost) < 0)
                    {
                        if (_currSolution.Sets.Exists(s => s.Tag.ToString() == set.Tag.ToString()) == false)
                        {
                            _currSolution.Sets.Remove(target);
                            _currSolution.Sets.Add(set);
                            _currSolution.ComputeAttributeRedundancies();
                            index = 0;
                            break;
                        }
                    }
                }

                need_to_covered.Clear();
                index++;
            }
        }
Example #11
0
        private List <SCPSet> GenerateMultiNeighborCandidateSet(Matrix <int> matrix, List <Panel> panels)
        {
            List <SCPSet> candidates = new List <SCPSet>();

            for (int i = 0; i < matrix.Size.X; i++)
            {
                List <Panel> pl = new List <Panel>();
                for (int j = 0; j < matrix.Size.Y; j++)
                {
                    if (matrix.Read(i, j) >= 2)
                    {
                        //Generate new candidate set by combination of possible pairs
                        if (pl.Contains(panels[i]) == false)
                        {
                            pl.Add(panels[i]);
                        }
                        if (pl.Contains(panels[j]) == false)
                        {
                            pl.Add(panels[j]);
                        }
                    }
                }
                if (pl.Count > 0)
                {
                    SCPSet candidate = CreateNewVirtualSet(pl);
                    candidates.Add(candidate);
                }
            }

            //Make the cost of each set realistic by not take into account cost of each set more than once in each pair
            //List<int> visited = new List<int>();
            //candidates = candidates.OrderByDescending(c => c.SetCost).ToList();
            //candidates.ForEach(c =>
            //{
            //    c.Parents.ForEach(p =>
            //    {
            //        if (visited.Contains(p))
            //            c.SetCost -= _problem.Source.Sets.Find(s => s.SetTag == p).SetCost;
            //        else
            //            visited.Add(p);
            //    });
            //});
            return(candidates);
        }
Example #12
0
        private SCPSolution RemoveRedundantSet(SCPSolution solution)
        {
            SCPSolution improved  = solution.Clone();
            List <int>  blacklist = new List <int>();

            solution.ComputeAttributeRedundancies();

            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 =>
            {
                SCPSet remove = improved.Sets.Find(s => s.Tag.ToString() == b.ToString());
                improved.Sets.Remove(remove);
            });
            if (improved.Cost < solution.Cost)
            {
                return(improved);
            }
            else
            {
                return(solution);
            }
        }
Example #13
0
        private List <Panel> GenerateAuctionPanel()
        {
            int          index    = 0;
            int          setcount = _currSolution.Sets.Count;
            List <Panel> panel    = new List <Panel>();

            _currSolution.ComputeAttributeRedundancies();
            while (index < setcount)
            {
                SCPSet     target          = _currSolution.Sets[index];
                List <int> need_to_covered = new List <int>();
                foreach (var attribute in target.Attributes)
                {
                    if (attribute.Redundancy <= 1)
                    {
                        need_to_covered.Add(attribute.Tag);
                    }
                }

                List <SCPSet> neighbors = _problem.Source.GetNeighbors(need_to_covered);

                if (neighbors.Count > 1)
                {
                    need_to_covered.ForEach(attribute =>
                    {
                        int set        = target.Tag;
                        double setcost = target.Cost;
                        double attcost = target.Cost / target.Attributes.Count;

                        Panel tuple = new Panel()
                        {
                            Useless = true, SetTag = set, SetCost = setcost, AttributeTag = attribute, AttributeCost = attcost
                        };
                        panel.Add(tuple);
                    });
                }

                index++;
            }
            return(panel);
        }
Example #14
0
        private double Destructor(SCPSolution solution, double alpha)
        {
            double        budget    = 0;
            List <SCPSet> blacklist = new List <SCPSet>();

            // _crSolution.Sets = _crSolution.Sets.OrderByDescending(s=>s.Cost).ThenByDescending(s => s.Overhead).ToList();

            solution.Sets.ForEach(set =>
            {
                if (set.Overhead >= alpha)
                {
                    blacklist.Add(set);
                }
            });

            blacklist.ForEach(bSet =>
            {
                SCPSet remove = bSet; // attribute.UsedIn.OrderByDescending(i => i.Cost).ThenByDescending(i => i.Overhead).First();
                int cla       = 0;    //Counter of losted attributes from the set that is going to be removed, I will divide the cost of set by the attributes that are going to be replace
                cla           = remove.Attributes.Where(a => a.Redundancy - 1 <= 0).Count();

                remove.Attributes.ForEach(attribute =>
                {
                    if (attribute.Redundancy - 1 <= 0)
                    {
                        Tuple <int, double, int> lost = new Tuple <int, double, int>(remove.Tag, remove.Cost / cla, attribute.Tag);
                        _losted.Add(lost);
                    }
                });

                budget += remove.Cost;
                solution.Sets.Remove(solution.Sets.Find(s => s.Tag == remove.Tag));
                if (_recovery.Exists(r => r.Tag == remove.Tag) == false)
                {
                    _recovery.Add(remove);
                }
            });

            return(budget);
        }
Example #15
0
        private List <SCPSet> GenerateTwoNeighborCandidateSet(Matrix <int> matrix, List <Panel> panels)
        {
            List <SCPSet> candidates = new List <SCPSet>();

            for (int i = 0; i < matrix.Size.X; i++)
            {
                for (int j = 0; j < matrix.Size.Y; j++)
                {
                    if (matrix.Read(i, j) >= 1)
                    {
                        //Generate new candidate set by combination of possible pairs
                        SCPSet candidate = CreateNewVirtualSet(new List <Constructive.Panel>()
                        {
                            panels[i], panels[j]
                        });
                        candidates.Add(candidate);
                    }
                }
            }

            return(candidates);
        }
Example #16
0
        public int ComputeConfilicts(SCPSet set)
        {
            int penalty = 0;

            Source.Sets.ForEach(s =>
            {
                penalty = 0;
                if (s.Tag.ToString() != set.Tag.ToString())
                {
                    s.Attributes.ForEach(a1 =>
                    {
                        set.Attributes.ForEach(a2 =>
                        {
                            if (a1.Tag.ToString() == a2.Tag.ToString())
                            {
                                penalty++;
                            }
                        });
                    });
                }
            });
            return(penalty);
        }
Example #17
0
        private List <SCPSet> Fitness(List <SCPSet> sets, List <SCPSet> union)
        {
            if (IsFeasible(sets))
            {
                return(sets);
            }

            HashSet <int> attributes = new HashSet <int>();
            double        price      = 0.0;

            sets.ForEach(s =>
            {
                price += s.Cost;
                s.Attributes.ForEach(a => attributes.Add(a.Tag));
            });
            union.ForEach(set =>
            {
                int frequency = set.Attributes.Select(a => a.Tag).Except(attributes).Count() + attributes.Count;
                set.Overhead  = (set.Cost + price) / frequency;
            });
            union = union.OrderBy(u => u.Overhead).ToList();

            SCPSet best = union.First();

            best.Visit = true;
            HashSet <int> additional = new HashSet <int>();

            best.Attributes.ForEach(a => additional.Add(a.Tag));

            if (additional.IsSubsetOf(attributes) == false)
            {
                sets.Add(best);
            }
            union = union.Where(set => set.Visit == false).ToList();

            return(Fitness(sets, union));
        }
Example #18
0
        public double Delta(SCPSet set, SCPSolution solution)
        {
            double gain  = 0;
            double price = 0;

            Dictionary <int, int> suFreq = solution.SUFrequency;

            int[,] catalog = solution.Catalog;
            List <int> targets = new List <int>();

            price += set.Cost;
            foreach (var attribute in set.Attributes)
            {
                for (int i = 0; i < catalog.Length / 2; i++)
                {
                    if (catalog[1, i] == attribute.Tag)
                    {
                        targets.Add(catalog[0, i]);
                    }
                }
            }

            foreach (var t in targets)
            {
                suFreq[t]--;
            }

            foreach (var item in suFreq)
            {
                if (item.Value <= 0)
                {
                    gain += solution.Sets.Find(s => s.Tag == item.Key).Cost;
                }
            }

            return(set.Cost - gain);
        }
Example #19
0
        public void Read(string path)
        {
            System.IO.StreamReader file = new System.IO.StreamReader(path);
            string str = file.ReadLine();

            _problem = new SCP(new MatrixSize()
            {
                X = int.Parse((str.Trim().Split(' ')[0])), Y = int.Parse(str.Trim().Split(' ')[1])
            });
            _problem.Source = new SCPDataSet();
            str             = "";
            for (int index = 0; index < _problem.Matrix.Size.Y;)
            {
                int read = file.Read();

                if (read == 32 || read == 10)
                {
                    if (str != "")
                    {
                        SCPSet set = new SCPSet()
                        {
                            Tag = ++index, Cost = int.Parse(str)
                        };
                        _problem.Source.Sets.Add(set);
                        str = "";
                    }
                    continue;
                }
                else
                {
                    str += (read - 48);
                }
            }

            DataBinding(true, 0, _problem.Matrix.Size.X, file);
        }
Example #20
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();
        }
Example #21
0
        private double Destructor(SCPSolution solution)
        {
            if (solution.Sets.Count() == 0)
            {
                return(0);
            }

            double budget = 0;

            solution.Sets = solution.Sets.OrderByDescending(s => s.Cost).ToList();
            SCPSet remove = solution.Sets.First();

            remove.Attributes.ForEach(attribute =>
            {
                if (attribute.Redundancy - 1 <= 0)
                {
                    Tuple <int, double, int> lost = new Tuple <int, double, int>(remove.Tag, remove.Cost, attribute.Tag);
                    if (_losted.Exists(l => l.Item3 == attribute.Tag) == false)
                    {
                        _losted.Add(lost);
                    }
                }
                else
                {
                    attribute.Redundancy--;
                }
            });

            budget += remove.Cost;
            solution.Sets.Remove(remove);
            if (_recovery.Exists(r => r.Tag == remove.Tag) == false)
            {
                _recovery.Add(remove);
            }

            //if (solution.Sets.Where(s => s.Visit == false).Count() <= ) return -1;
            // SCPSet set = solution.Sets.Where(s=>s.Visit == false).First();
            //set.Visit = true;
            //blacklist.Add(set);
            //blacklist.ForEach(bSet =>
            //{
            //    SCPSet remove = bSet;// attribute.UsedIn.OrderByDescending(i => i.Cost).ThenByDescending(i => i.Overhead).First();
            //    int cla = 0;//Counter of losted attributes from the set that is going to be removed, I will divide the cost of set by the attributes that are going to be replace
            //    cla = remove.Attributes.Where(a => a.Redundancy - 1 <= 0).Count();

            //    remove.Attributes.ForEach(attribute =>
            //    {
            //        if (attribute.Redundancy - 1 <= 0)
            //        {
            //            Tuple<int, double, int> lost = new Tuple<int, double, int>(remove.Tag, remove.Cost / cla, attribute.Tag);
            //            if (_losted.Exists(l => l.Item3 == attribute.Tag) == false)
            //                _losted.Add(lost);
            //        }
            //    });

            //    budget += remove.Cost;
            //    solution.Sets.Remove(solution.Sets.Find(s => s.Tag == remove.Tag));
            //    if (_recovery.Exists(r => r.Tag == remove.Tag) == false)
            //        _recovery.Add(remove);
            //});

            return(budget);
        }