private HashSet <SellerCity> GreedyCover(HashSet <long> requestItems) { //greedy set cover alg HashSet <SellerCity> sellerSet = new HashSet <SellerCity>(); //1 HashSet <long> covered = new HashSet <long>(); //2 while (!covered.SetEquals(requestItems)) { HashSet <long> remains = new HashSet <long>(requestItems); remains.ExceptWith(covered); //check cost-effective int minRemain = int.MaxValue; SellerCity minSellerCity = null; foreach (SellerCity sc in sellerCities) { HashSet <long> sellerRemainItems = new HashSet <long>(remains); sellerRemainItems.ExceptWith(maps[sc]); if (sellerRemainItems.Count < minRemain) { minRemain = sellerRemainItems.Count; minSellerCity = sc; } } Debug.Assert(minSellerCity != null); sellerSet.Add(minSellerCity); HashSet <long> sellerProvide = new HashSet <long>(maps[minSellerCity]); sellerProvide.IntersectWith(requestItems); covered.UnionWith(sellerProvide); } return(sellerSet); }
private HashSet <SellerCity> SteinerTree(HashSet <SellerCity> x0, out long treecost) { HashSet <Tuple <SellerCity, SellerCity> > connection = new HashSet <Tuple <SellerCity, SellerCity> >(); treecost = 0; HashSet <SellerCity> xSteiner = new HashSet <SellerCity>(sellerCities); xSteiner.ExceptWith(x0); HashSet <SellerCity> xSteinerExcept = new HashSet <SellerCity>(x0); HashSet <SellerCity> x_ = new HashSet <SellerCity>(); x_.Add(PickRandom(x0)); xSteinerExcept.ExceptWith(x_); while (xSteinerExcept.Count != 0) { // Console.WriteLine(xSteinerExcept.Count); int minDistance = int.MaxValue; SellerCity v = null; foreach (SellerCity sc in xSteinerExcept) { int dis = GetMinDistance(sc, x_); if (dis < minDistance) { minDistance = dis; v = sc; } } SellerCity p = GetPath(v, x_); if (p != null) { x_.Add(p); x_.Add(v); //connection cost var t1 = new Tuple <SellerCity, SellerCity>(p, v); var t2 = new Tuple <SellerCity, SellerCity>(v, p); if (!connection.Contains(t1) && !connection.Contains(t2)) { connection.Add(t1); connection.Add(t2); treecost += GetDistance(v, p); } } else { Debug.Assert(false); return(null); } xSteinerExcept.Clear(); xSteinerExcept.UnionWith(x0); xSteinerExcept.ExceptWith(x_); } return(x_); }
private ulong GetPathMinDistance(long item, HashSet <SellerCity> xCities, HashSet <long> xItems, out List <SellerCity> bridges) { List <SellerCity> supports = maps2[item]; ulong minDis = UInt64.MaxValue; SellerCity bridge1 = null, bridge2 = null; foreach (var support in supports) { //check cities foreach (var city in xCities) { ulong dis = (ulong)GetDistance(support, city); if (dis < minDis) { minDis = dis; bridge1 = support; bridge2 = city; } } if (minDis > Distance_D) { //check item foreach (var i in xItems) { //HashSet<SellerCity> iSupport = new HashSet<SellerCity>(maps2[i]); var iSupports = maps2[i]; foreach (var iSupport in iSupports) { ulong dis = (ulong)GetDistance(support, iSupport); dis += Distance_D; if (dis < minDis) { minDis = dis; bridge1 = support; bridge2 = iSupport; if (dis == 0) { break; } } } } } } Debug.Assert(bridge1 != null); Debug.Assert(bridge2 != null); bridges = new List <SellerCity>(2); bridges.Add(bridge1); bridges.Add(bridge2); return(minDis); }
public Alg(IEnumerable <Item> items) { this.items = new HashSet <Item>(items); this.sellerCities = new HashSet <SellerCity>(); foreach (Item i in items) { SellerCity t = new SellerCity(i.SellerTaobaoId, i.Location); long id = i.UniqId == 0 ? i.TaobaoId : i.UniqId; sellerCities.Add(t); maps.Add(t, id); maps2.Add(id, t); } }
public int GetMinDistance(SellerCity seller, IEnumerable <SellerCity> sellers) { int minD = int.MaxValue; if (sellers == null) { return(minD); } foreach (var s in sellers) { minD = Math.Min(minD, GetDistance(seller, s)); } return(minD); }
public int GetDistance(SellerCity s1, SellerCity s2) { if (s1 == s2) { return(0); } else if (s1.SellerID == s2.SellerID) { return(Constants.SAME_SELLER_DIFF_CITY); } else { return(Constants.DIFF_SELLER); } }
public SellerCity GetPath(SellerCity src, IEnumerable <SellerCity> tars) { int minD = int.MaxValue; SellerCity tar = null; foreach (var s in tars) { if (src.SellerID == s.SellerID) { minD = Constants.SAME_SELLER_DIFF_CITY; tar = s; } else { if (minD > Constants.DIFF_SELLER) { minD = Constants.DIFF_SELLER; tar = s; } } } return(tar); }
public override Result DoAlg(List <long> requestItems) { long start = Environment.TickCount; List <List <SellerCity> > itemsSellers = new List <List <SellerCity> >(); //3 find the minimum List<SellerCity> count int minSize = int.MaxValue; long arare = 0; foreach (long item in requestItems) { List <SellerCity> sellers = maps2[item]; itemsSellers.Add(sellers); if (sellers.Count < minSize) { minSize = sellers.Count; arare = item; } } //4 SellerCity iStar = null; int argMinRi = int.MaxValue; foreach (SellerCity i in maps2[arare]) { //5 int Ri = int.MinValue; foreach (long a in requestItems) { if (a != arare) { //6 int Ria = GetMinDistance(i, maps2[a]); Ri = Math.Max(Ri, Ria); } } if (Ri < argMinRi) { argMinRi = Ri; iStar = i; } } if (iStar == null) { return(null); } HashSet <SellerCity> ss = new HashSet <SellerCity>(); ss.Add(iStar); foreach (long a in requestItems) { ss.Add(GetPath(iStar, maps2[a])); } long end = Environment.TickCount; //calculate cost int cost = CalcCost(ss); return(new Result { Cost = cost, Sellers = ss, AlgorithmName = this.AlgorithmName, Time = end - start, }); }