From b357bed413013da0d06bc3e5a718eb79fc4a7a63 Mon Sep 17 00:00:00 2001 From: oblonski Date: Wed, 14 Jan 2015 19:35:28 +0100 Subject: [PATCH] add helper to easily get random routes and jobs from collection --- .../java/jsprit/core/util/RandomUtils.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 jsprit-core/src/main/java/jsprit/core/util/RandomUtils.java diff --git a/jsprit-core/src/main/java/jsprit/core/util/RandomUtils.java b/jsprit-core/src/main/java/jsprit/core/util/RandomUtils.java new file mode 100644 index 00000000..5dfa66d8 --- /dev/null +++ b/jsprit-core/src/main/java/jsprit/core/util/RandomUtils.java @@ -0,0 +1,34 @@ +package jsprit.core.util; + +import jsprit.core.problem.job.Job; +import jsprit.core.problem.solution.route.VehicleRoute; + +import java.util.Collection; +import java.util.Random; + +/** + * Created by schroeder on 14/01/15. + */ +public class RandomUtils { + + public static VehicleRoute nextRoute(Collection routes, Random random){ + int randomIndex = random.nextInt(routes.size()); + int count = 0; + for(VehicleRoute route : routes){ + if(count <= randomIndex) return route; + count++; + } + return null; + } + + public static Job nextJob(Collection jobs, Random random){ + int randomIndex = random.nextInt(jobs.size()); + int count = 0; + for(Job job : jobs){ + if(count <= randomIndex) return job; + count++; + } + return null; + } + +}