CantidateSolution GenerateRandomStart() { // Hash set containing an entry for each bid HashSet<Integer> Avail = new HashSet<Integer>(); Integer count = _bids.size(); for (Integer index = 0; index < count; index++) { Avail.add(index); } // Generate Random Solution Random gen = new Random(); // list of selected bids LinkedList<Integer> bidlist = new LinkedList<Integer>(); // The total value of selected bids Double value = 0.0; // Here's the algorithym to select a random solution // 1. Choose a bid at random from the avaialble set. // 2. Put the chosen bid in the Bids list. // 3. Go through all the bids remaining in the available set and remove any colliding bids. // 4. Repeat from step 1 until no bids remain. // The bid list now contains a valid solution chosen at random. // Iterate as long as anything is left in the "available set" while (Avail.size() > 0) { // generate a random integer between 0 and the size of the Available set. Integer newbid = gen.nextInt(Avail.size()); // Randomly choose an integer out of the Avail Set newbid = (Integer) Avail.toArray()[newbid]; // Add the value of the chosen bid value += _bids.get(newbid).value; // Add the chosen bid to the bidList (a list of integers) bidlist.add(newbid); RemoveColidingBids(_bids.get(newbid), Avail); } CantidateSolution sol = new CantidateSolution(); // Collections.sort(bidlist); sol.value = value; sol.bids = bidlist; return sol; }
CantidateSolution GenerateGoodStart(long offset) { // Hash set containing an entry for each bid HashSet<Integer> Avail = new HashSet<Integer>(); Integer count = _bids.size(); LinkedList<Integer> SortedBids = new LinkedList<Integer>(); for (Integer index = 0; index < count; index++) { Avail.add(index); SortedBids.add(index); } if (offset < SortedBids.size()) { for (int remove = 0; remove < offset; remove++) { SortedBids.removeFirst(); } } // Sort the bids Collections.sort(SortedBids, new RegionAvgCmp(_bids)); // list of selected bids LinkedList<Integer> bidlist = new LinkedList<Integer>(); // The total value of selected bids Double value = 0.0; Random gen = new Random(); while (Avail.size() > 0) { Integer newbid; if (SortedBids.size() > 0) { newbid = SortedBids.removeFirst(); } else { // generate a random integer between 0 and the size of the Available set. newbid = gen.nextInt(Avail.size()); // Randomly choose an integer out of the Avail Set newbid = (Integer) Avail.toArray()[newbid]; } // Add the value of the chosen bid value += _bids.get(newbid).value; // Add the chosen bid to the bidList (a list of integers) bidlist.add(newbid); RemoveColidingBids(_bids.get(newbid), Avail); // clean out our SortedBidsList Iterator<Integer> li = SortedBids.iterator(); while (li.hasNext()) { Integer bidItem = li.next(); if (!Avail.contains(bidItem)) { li.remove(); } } } CantidateSolution sol = new CantidateSolution(); // Collections.sort(bidlist); sol.value = value; sol.bids = bidlist; return sol; }