198 lines
8.2 KiB
Java
198 lines
8.2 KiB
Java
|
|
import org.chocosolver.solver.Model;
|
|
import org.chocosolver.solver.Solution;
|
|
import org.chocosolver.solver.Solver;
|
|
import org.chocosolver.solver.constraints.Constraint;
|
|
import org.chocosolver.solver.exception.ContradictionException;
|
|
import org.chocosolver.solver.search.limits.FailCounter;
|
|
import org.chocosolver.solver.search.strategy.Search;
|
|
import org.chocosolver.solver.search.strategy.selectors.variables.DomOverWDeg;
|
|
import org.chocosolver.solver.search.strategy.selectors.variables.ImpactBased;
|
|
import org.chocosolver.solver.variables.IntVar;
|
|
import org.chocosolver.solver.variables.BoolVar;
|
|
import java.io.*;
|
|
import java.util.Arrays;
|
|
|
|
public class Exercice2FB {
|
|
public static final int HEURISTIQUE_DEFAUT = 0;
|
|
public static final int HEURISTIQUE_DOMOVERWDEG = 1;
|
|
public static final int HEURISTIQUE_IMPACT_BASED_SEARCH = 2;
|
|
public static final int HEURISTIQUE_ACTIVITY = 3;
|
|
public static final String COHERENCE_BORNES = "BC";
|
|
public static final String COHERENCE_DOMAINES = "AC";
|
|
|
|
public static final int RESTART_AUCUN = 0;
|
|
public static final int RESTART_LUBY = 1;
|
|
public static final int RESTART_GEOMETRIQUE = 2;
|
|
|
|
public static void main(String[] args) {
|
|
|
|
final int p = 16;
|
|
String fileName = "instance.txt";
|
|
String line = null;
|
|
int[] PARAMETRES_HORAIRE = new int[4];
|
|
int[] NBR_EMPLOYES_REQUIS = new int[p];
|
|
int[] NBR_EMPLOYES_SOUHAITES = new int[p];
|
|
try {
|
|
FileReader fileReader =
|
|
new FileReader(fileName);
|
|
|
|
BufferedReader bufferedReader =
|
|
new BufferedReader(fileReader);
|
|
|
|
if((line = bufferedReader.readLine()) != null) {
|
|
String[] arrayLine= line.split("\\s+");
|
|
PARAMETRES_HORAIRE[0] = Integer.parseInt(arrayLine[0]);
|
|
PARAMETRES_HORAIRE[1] = Integer.parseInt(arrayLine[1]);
|
|
PARAMETRES_HORAIRE[2] = Integer.parseInt(arrayLine[2]);
|
|
PARAMETRES_HORAIRE[3] = Integer.parseInt(arrayLine[3]);
|
|
}
|
|
if((line = bufferedReader.readLine()) != null) {
|
|
String[] arrayLine= line.split("\\s+");
|
|
for (int i = 0 ; i < p ; i++) {
|
|
NBR_EMPLOYES_REQUIS[i] = Integer.parseInt(arrayLine[i]);
|
|
}
|
|
}
|
|
if((line = bufferedReader.readLine()) != null) {
|
|
String[] arrayLine= line.split("\\s+");
|
|
for (int i = 0 ; i < p ; i++) {
|
|
NBR_EMPLOYES_SOUHAITES[i] = Integer.parseInt(arrayLine[i]);
|
|
}
|
|
}
|
|
bufferedReader.close();
|
|
}
|
|
catch(FileNotFoundException ex) {
|
|
System.out.println(
|
|
"Unable to open file '" +
|
|
fileName + "'");
|
|
}
|
|
catch(IOException ex) {
|
|
System.out.println(
|
|
"Error reading file '"
|
|
+ fileName + "'");
|
|
}
|
|
final int N = PARAMETRES_HORAIRE[0];
|
|
final int MIN_H = PARAMETRES_HORAIRE[1];
|
|
final int MAX_H = PARAMETRES_HORAIRE[2];
|
|
final int MIN_PERIODE = PARAMETRES_HORAIRE[3];
|
|
|
|
Model model = new Model("Optimisation Employes");
|
|
|
|
IntVar heure_pause[] = model.intVarArray("HEURE_PAUSE", N, MIN_PERIODE, p-MIN_PERIODE-1, true);
|
|
IntVar heure_debut[] = model.intVarArray("HEURE_DEBUT", N, 0, p-MIN_H, true);
|
|
IntVar heure_fin[] = model.intVarArray("HEURE_FIN", N, p-MIN_H, p, true);
|
|
|
|
// Creation des contraintes
|
|
for (int i = 0; i < N; i++) {
|
|
model.arithm(heure_debut[i], "<=", heure_pause[i], "-", MIN_PERIODE).post();
|
|
model.arithm(heure_fin[i], ">=", heure_pause[i], "+", (MIN_PERIODE + 1) ).post();
|
|
}
|
|
|
|
IntVar[] heures_employe = model.intVarArray("C", N, MIN_H, MAX_H, true);
|
|
for (int i = 0; i < N; i++) {
|
|
model.arithm(heures_employe[i], "=", heure_fin[i], "-", heure_debut[i]).post();
|
|
}
|
|
|
|
// Creation de la transpose de la matrice horaire.
|
|
IntVar[][] horaire = model.intVarMatrix(p, N, 0, 1);
|
|
IntVar[] nbr_employe = model.intVarArray("C", p, 0, N, true);
|
|
for (int i = 0; i < p; i++) {
|
|
for (int j = 0; j < N; j++) {
|
|
BoolVar c1 = model.arithm(heure_pause[j], "!=", i).reify();
|
|
BoolVar c2 = model.arithm(heure_debut[j], "<=", i).reify();
|
|
BoolVar c3 = model.arithm(heure_fin[j], ">", i).reify();
|
|
model.ifThenElse(model.and(c1, c2, c3), model.arithm(horaire[i][j], "=", 1), model.arithm(horaire[i][j], "=", 0));
|
|
}
|
|
model.count(model.intVar(1), horaire[i], nbr_employe[i]).post();
|
|
model.arithm(nbr_employe[i], ">=", NBR_EMPLOYES_REQUIS[i]).post();
|
|
}
|
|
|
|
IntVar[] diff_employe = model.intVarArray("DIFF_EMPLOYE", p, 0, N);
|
|
IntVar[] diff_employe_abs = model.intVarArray("DIFF_EMPLOYE_ABS", p, 0, N);
|
|
IntVar[] perte = model.intVarArray("COST", p, 0, 20 * N, true);
|
|
for (int i = 0; i < p; i++) {
|
|
model.arithm(diff_employe[i], "=", nbr_employe[i] , "-", NBR_EMPLOYES_SOUHAITES[i]).post();
|
|
model.absolute(diff_employe_abs[i], diff_employe[i]).post();
|
|
model.times(diff_employe_abs[i], model.intVar(20), perte[i]).post();
|
|
|
|
}
|
|
|
|
int[] coeffs = new int[p];
|
|
Arrays.fill(coeffs, 0, p, 1);
|
|
IntVar tot_perte = model.intVar("TOTAL_COST", 0, 20 * N * p, true);
|
|
model.scalar(perte , coeffs , "=", tot_perte).post();
|
|
|
|
// Creation du solveur
|
|
Solver solver = model.getSolver();
|
|
Solution best = solver.findOptimalSolution(tot_perte, Model.MINIMIZE);
|
|
for (int i = 0; i < N; i++) {
|
|
System.out.print("Heure_debut : ");
|
|
System.out.print(best.getIntVal(heure_debut[i]));
|
|
System.out.print(" ");
|
|
System.out.print("Heure_pause : ");
|
|
System.out.print(best.getIntVal(heure_pause[i]));
|
|
System.out.print(" ");
|
|
System.out.print("Heure_fin : ");
|
|
System.out.print(best.getIntVal(heure_fin[i]));
|
|
System.out.println("");
|
|
}
|
|
System.out.print(9);
|
|
System.out.print(" | ");
|
|
System.out.print(" ");
|
|
System.out.print(" | ");
|
|
for (int j = 10; j < 17; j++) {
|
|
System.out.print(j);
|
|
System.out.print("| ");
|
|
System.out.print(" ");
|
|
System.out.print("| ");
|
|
}
|
|
System.out.print("Nbr_heures_travaillees_incluant_pause");
|
|
System.out.println("");
|
|
for (int i = 0; i < N; i++) {
|
|
for (int j = 0; j < p; j++) {
|
|
if (best.getIntVal(horaire[j][i]) == 1)
|
|
System.out.print("1");
|
|
else
|
|
System.out.print(" ");
|
|
System.out.print(" | ");
|
|
}
|
|
System.out.print(best.getIntVal(heures_employe[i]));
|
|
System.out.println("");
|
|
|
|
}
|
|
for (int i = 0; i < p; i++) {
|
|
System.out.print(best.getIntVal(nbr_employe[i]));
|
|
System.out.print(" | ");
|
|
}
|
|
System.out.print("Nbr_employes");
|
|
System.out.println("");
|
|
for (int i = 0; i < p; i++) {
|
|
System.out.print(NBR_EMPLOYES_REQUIS[i]);
|
|
System.out.print(" | ");
|
|
}
|
|
System.out.print("Nbr_employes_requis");
|
|
System.out.println("");
|
|
for (int i = 0; i < p; i++) {
|
|
System.out.print(NBR_EMPLOYES_SOUHAITES[i]);
|
|
System.out.print(" | ");
|
|
}
|
|
System.out.print("Nbr_employes_souhaites");
|
|
System.out.println("");
|
|
for (int i = 0; i < p; i++) {
|
|
|
|
System.out.print(best.getIntVal(perte[i]));
|
|
if(best.getIntVal(perte[i])==0) {
|
|
System.out.print(" | ");
|
|
}else {
|
|
System.out.print("| ");
|
|
}
|
|
}
|
|
System.out.print("perte");
|
|
System.out.println("");
|
|
System.out.print("perte_totale : ");
|
|
System.out.print(best.getIntVal(tot_perte));
|
|
System.out.println("");
|
|
solver.printStatistics();
|
|
|
|
}
|
|
}
|