Added randomness run numbers input from arguments
This commit is contained in:
parent
c1b0b6e945
commit
51c87b72e6
7 changed files with 36 additions and 11 deletions
|
@ -34,6 +34,10 @@ public class Main {
|
||||||
.setDefault(0)
|
.setDefault(0)
|
||||||
.type(Integer.class)
|
.type(Integer.class)
|
||||||
.help("percentage of randomness in solving");
|
.help("percentage of randomness in solving");
|
||||||
|
parser.addArgument("-rn", "--randomRunNumber")
|
||||||
|
.setDefault(10)
|
||||||
|
.type(Integer.class)
|
||||||
|
.help("number of runs in random mode");
|
||||||
parser.addArgument("--solver")
|
parser.addArgument("--solver")
|
||||||
.nargs("+")
|
.nargs("+")
|
||||||
.required(true)
|
.required(true)
|
||||||
|
@ -70,6 +74,7 @@ public class Main {
|
||||||
List<Solver> solvers = solversToTest.stream().map(Solver::getSolver).collect(Collectors.toList());
|
List<Solver> solvers = solversToTest.stream().map(Solver::getSolver).collect(Collectors.toList());
|
||||||
|
|
||||||
Integer randomness = ns.getInt("random");
|
Integer randomness = ns.getInt("random");
|
||||||
|
Integer randomRunNumber = ns.getInt("randomRunNumber");
|
||||||
|
|
||||||
// retrieve all instances on which we should run the solvers.
|
// retrieve all instances on which we should run the solvers.
|
||||||
List<String> instances = new ArrayList<>();
|
List<String> instances = new ArrayList<>();
|
||||||
|
@ -125,7 +130,7 @@ public class Main {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
long deadline = System.currentTimeMillis() + solveTimeMs;
|
long deadline = System.currentTimeMillis() + solveTimeMs;
|
||||||
// run the solver on the current instance
|
// run the solver on the current instance
|
||||||
Optional<Schedule> result = solver.solve(instance, deadline,randomness);
|
Optional<Schedule> result = solver.solve(instance, deadline,randomness,randomRunNumber);
|
||||||
// measure elapsed time (in milliseconds)
|
// measure elapsed time (in milliseconds)
|
||||||
long runtime = System.currentTimeMillis() - start;
|
long runtime = System.currentTimeMillis() - start;
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import java.util.Optional;
|
||||||
**/
|
**/
|
||||||
public class BasicSolver implements Solver {
|
public class BasicSolver implements Solver {
|
||||||
@Override
|
@Override
|
||||||
public Optional<Schedule> solve(Instance instance, long deadline, int randomness) {
|
public Optional<Schedule> solve(Instance instance, long deadline, int randomness, int randomRunNumber) {
|
||||||
|
|
||||||
// resource order that will be populated (initially empty)
|
// resource order that will be populated (initially empty)
|
||||||
ResourceOrder sol = new ResourceOrder(instance);
|
ResourceOrder sol = new ResourceOrder(instance);
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class DescentSolver implements Solver {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Schedule> solve(Instance instance, long deadline, int randomness) {
|
public Optional<Schedule> solve(Instance instance, long deadline, int randomness, int randomRunNumber) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -124,10 +124,8 @@ public class GreedySolver implements Solver {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Schedule solveInner(Instance instance, long deadline, int percentRandom)
|
||||||
|
{
|
||||||
@Override
|
|
||||||
public Optional<Schedule> solve(Instance instance, long deadline, int percentRandom) {
|
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
|
|
||||||
// keeps track of the next time the machine is free
|
// keeps track of the next time the machine is free
|
||||||
|
@ -168,7 +166,29 @@ public class GreedySolver implements Solver {
|
||||||
possibleTasks.add(new Task(task.job, task.task + 1));
|
possibleTasks.add(new Task(task.job, task.task + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Optional.of(schedule);
|
return schedule;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Schedule> solve(Instance instance, long deadline, int percentRandom, int randomRunNumber)
|
||||||
|
{
|
||||||
|
if (percentRandom == 0) {
|
||||||
|
return Optional.of(solveInner(instance, deadline, percentRandom));
|
||||||
|
} else {
|
||||||
|
Integer bestTime = Integer.MAX_VALUE;
|
||||||
|
Schedule bestSchedule = null;
|
||||||
|
for (int i = 0; i < randomRunNumber; i++) {
|
||||||
|
Schedule schedule = solveInner(instance, deadline, percentRandom);
|
||||||
|
System.out.println(schedule.makespan());
|
||||||
|
if (schedule.makespan() < bestTime){
|
||||||
|
bestTime = schedule.makespan();
|
||||||
|
bestSchedule = schedule;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.of(bestSchedule);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ public interface Solver {
|
||||||
* This time is in milliseconds and can be compared with System.currentTimeMilliseconds()
|
* This time is in milliseconds and can be compared with System.currentTimeMilliseconds()
|
||||||
* @return An optional schedule that will be non empty if a solution was found.
|
* @return An optional schedule that will be non empty if a solution was found.
|
||||||
*/
|
*/
|
||||||
Optional<Schedule> solve(Instance instance, long deadline, int randomness);
|
Optional<Schedule> solve(Instance instance, long deadline, int randomness, int randomRunNumber);
|
||||||
|
|
||||||
/** Static factory method to create a new solver based on its name. */
|
/** Static factory method to create a new solver based on its name. */
|
||||||
static Solver getSolver(String name) {
|
static Solver getSolver(String name) {
|
||||||
|
|
|
@ -16,7 +16,7 @@ public class BasicSolverTests {
|
||||||
Instance instance = Instance.fromFile(Paths.get("instances/aaa1"));
|
Instance instance = Instance.fromFile(Paths.get("instances/aaa1"));
|
||||||
|
|
||||||
Solver solver = new BasicSolver();
|
Solver solver = new BasicSolver();
|
||||||
Optional<Schedule> result = solver.solve(instance, System.currentTimeMillis() + 10, 0);
|
Optional<Schedule> result = solver.solve(instance, System.currentTimeMillis() + 10, 0,10);
|
||||||
|
|
||||||
assert result.isPresent() : "The solver did not find a solution";
|
assert result.isPresent() : "The solver did not find a solution";
|
||||||
// extract the schedule associated to the solution
|
// extract the schedule associated to the solution
|
||||||
|
|
|
@ -23,7 +23,7 @@ public class ManualEncodingTests {
|
||||||
this.instance = Instance.fromFile(Paths.get("instances/aaa1"));
|
this.instance = Instance.fromFile(Paths.get("instances/aaa1"));
|
||||||
|
|
||||||
Solver solver = new BasicSolver();
|
Solver solver = new BasicSolver();
|
||||||
Optional<Schedule> result = solver.solve(this.instance, System.currentTimeMillis() + 10,0);
|
Optional<Schedule> result = solver.solve(this.instance, System.currentTimeMillis() + 10,0, 10);
|
||||||
|
|
||||||
assert result.isPresent() : "The solver did not find a solution";
|
assert result.isPresent() : "The solver did not find a solution";
|
||||||
// extract the schedule associated to the solution
|
// extract the schedule associated to the solution
|
||||||
|
|
Loading…
Reference in a new issue