12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package jobshop.solvers;
-
- import jobshop.*;
- import jobshop.encodings.JobNumbers;
- import jobshop.encodings.Schedule;
-
- import java.util.Random;
-
- /** A solver that generates random solutions until a deadline is met.
- * Then returns the best solution that was generated.
- */
- public class RandomSolver implements Solver {
-
- @Override
- public Result solve(Instance instance, long deadline) {
- Random generator = new Random(0);
-
- JobNumbers sol = new JobNumbers(instance);
-
- for(int j = 0 ; j<instance.numJobs ; j++) {
- for(int t = 0 ; t<instance.numTasks ; t++) {
- sol.jobs[sol.nextToSet++] = j;
- }
- }
- Schedule best = sol.toSchedule();
- while(deadline - System.currentTimeMillis() > 1) {
- shuffleArray(sol.jobs, generator);
- Schedule s = sol.toSchedule();
- if(s.makespan() < best.makespan()) {
- best = s;
- }
- }
-
-
- return new Result(instance, best, Result.ExitCause.Timeout);
- }
-
- /** Simple Fisher–Yates array shuffling */
- private static void shuffleArray(int[] array, Random random)
- {
- int index;
- for (int i = array.length - 1; i > 0; i--)
- {
- index = random.nextInt(i + 1);
- if (index != i)
- {
- array[index] ^= array[i];
- array[i] ^= array[index];
- array[index] ^= array[i];
- }
- }
- }
- }
-
|