Added randomness run numbers input from arguments

This commit is contained in:
alejeune 2023-03-20 10:41:53 +01:00
parent c1b0b6e945
commit 51c87b72e6
7 changed files with 36 additions and 11 deletions

View file

@ -34,6 +34,10 @@ public class Main {
.setDefault(0)
.type(Integer.class)
.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")
.nargs("+")
.required(true)
@ -70,6 +74,7 @@ public class Main {
List<Solver> solvers = solversToTest.stream().map(Solver::getSolver).collect(Collectors.toList());
Integer randomness = ns.getInt("random");
Integer randomRunNumber = ns.getInt("randomRunNumber");
// retrieve all instances on which we should run the solvers.
List<String> instances = new ArrayList<>();
@ -125,7 +130,7 @@ public class Main {
long start = System.currentTimeMillis();
long deadline = System.currentTimeMillis() + solveTimeMs;
// 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)
long runtime = System.currentTimeMillis() - start;

View file

@ -12,7 +12,7 @@ import java.util.Optional;
**/
public class BasicSolver implements Solver {
@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)
ResourceOrder sol = new ResourceOrder(instance);

View file

@ -23,7 +23,7 @@ public class DescentSolver implements Solver {
}
@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();
}

View file

@ -124,10 +124,8 @@ public class GreedySolver implements Solver {
return null;
}
@Override
public Optional<Schedule> solve(Instance instance, long deadline, int percentRandom) {
public Schedule solveInner(Instance instance, long deadline, int percentRandom)
{
Random r = new Random();
// 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));
}
}
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);
}
}

View file

@ -15,7 +15,7 @@ public interface Solver {
* 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.
*/
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 Solver getSolver(String name) {

View file

@ -16,7 +16,7 @@ public class BasicSolverTests {
Instance instance = Instance.fromFile(Paths.get("instances/aaa1"));
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";
// extract the schedule associated to the solution

View file

@ -23,7 +23,7 @@ public class ManualEncodingTests {
this.instance = Instance.fromFile(Paths.get("instances/aaa1"));
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";
// extract the schedule associated to the solution