ERLEDIGT
NEIN
NEIN
ANTWORTEN
9
9
ZUGRIFFE
784
784
EMPFEHLEN
-
28.12.11 11:54 #1
- Registriert seit
- Dec 2011
- Beiträge
- 5
Hallo,
Muss eine Art scheduling problem lösen. Dabei müssen n Aufträge an m Maschinen bearbeitet werden.
Das ganze soll mit Hilfe von genetischem Algorithmen geschehen.
Bin noch ganz am Anfang. Wie kann ich Individuen(Lösungsvorschläge) erzeugen? Ein derartiger Maschinenbelegungsplan wird ja meist in einem Gantt-Diagramm dargestellt. Wie kann ich so einen Plan codieren(z.B in ein Array, dass die Auftragsfolge darstellt)?
Wenn man z.B 3 Aufträge hat, die jeweils an 3 Maschinen bearbeitet werden müssen, wie würde eine mögliche Lösung aussehen? Es ist sinnvoll die Lösung in einem array darzustellen, oder?
Wie stelle ich sicher, dass meine Individuen alle zulässige Lösungen sind(keine Überschneidungen etc.)?
Was ich auch nicht verstehe ist wie man diese Lösungen(Arrays) automatisch generiert?
Danke und lg
-
Bevor Du Dich mit java rumschlägst, solltest Du im Bereich des Operations Research stöbern. Dort werden genau derartige Probleme gelöst, z.B. der Johnson-Algorithmus. Bei Deiner Problembeschreibung ist mir noch nicht klar, ob die Problemlösung zu einer Doktorarbeit ausartet.
Geändert von j2se (28.12.11 um 12:33 Uhr)
-
28.12.11 13:29 #3
- Registriert seit
- Dec 2011
- Beiträge
- 5
Wie ein genetischer Algorithmus funktioniert, versteh ich so halbwegs. Aber dieser braucht Individuen, von denen er ausgeht. Diese müsste ich irgendwie generieren, aber so, dass es wirklich zulässige Lösungen für das Maschinenbelegungsproblem sind.
Bin erst im 3. Semester, zur Doktorarbeit sollte es also nicht werden.
Hier noch einmal die genaue Angabe:
Das Java-Programm soll einen optimalen Belegungsplan für n Aufträge,die an m>2 Maschinen bearbeitet werden müssen nach der Idee der genetischen Algorithmen bestimmen.
-
Die Individuen können ja Instanzen sein oder eine Sammlung von Objekten wie z.B. ein Array, eine List, Map oder was auch immer sich für Dein Algorithmus eignet.
-
02.01.12 08:21 #5
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
du musst noch etwas mehr Angaben zu deinem Problem machen.
1) Wodurch sind ein deinem System Aufträge charakterisiert?
Auftrag = (Id, Dauer, Spätester Endtermin, Priorität, Ressourcen / bestimmte Maschinen-Voraussetzungen)?
2) Wodurch sind ein deinem System Maschinen charakterisiert?
Maschine=(Id, Kapazität, Verarbeitungsdauer, Maschinen-Fähigkeiten, Kosten pro Auftrag / Zeiteinheit)?
3) Müssen die Aufträge nur von einer oder auch von mehreren Maschinen bearbeitet werden?
4) Was willst du denn im Belegungsplan Optimieren?
4.1) Die Durchlaufzeit minimieren?
4.2) Die Kapazitätsauslastung (Maschinenauslastung) maximieren?
4.3) Die Terminabweichungen minimieren?
4.4) Die Verarbeitungskosten minimieren?
...
4.x) eine Kombination mehrerer Kriterien?
zu deinen Fragen:
In deiner Fitness-Funktion kodierst du die Ermittlung der Güte eines konkreten Belegungsplans (deines Individuums) (Aufträge auf Maschinen verteilt). Dabei kannst du sowohl positive Einflüsse (Termintreue, Kostengünstigkeit, Gesamtauslastungsgrad) als auch negative Einflüsse (ungültige Belegungen, Terminverzug, etc.) einbeziehen.Wie stelle ich sicher, dass meine Individuen alle zulässige Lösungen sind(keine Überschneidungen etc.)?
...
in dem du in deiner Fitness-Funktion unzulässige Lösungen maximal bestrafst (maximal schlechte Fitness) werden diese vom genetischen Algorithmus automatisch gemieden.
Die (möglichen) Lösungen -> Lösungskandidaten -> Individuen generierst du mithilfe eines Zufallszahlengenerators zufällig. In deinem Fall verteilst du einfach die gegeben Auftrage auf dieWas ich auch nicht verstehe ist wie man diese Lösungen(Arrays) automatisch generiert?
zur Verfügung stehenden Maschinen.
Da die Kodierung die Lösungsfindung in einem genetischen Algorithmus stark beeinflussen kann, möchte ich noch auf die Antworten zu den obigen Fragen warten, bevor ich einen Vorschlag mache.Bin noch ganz am Anfang. Wie kann ich Individuen(Lösungsvorschläge) erzeugen? Ein derartiger Maschinenbelegungsplan wird ja meist in einem Gantt-Diagramm dargestellt. Wie kann ich so einen Plan codieren(z.B in ein Array, dass die Auftragsfolge darstellt)?
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
02.01.12 11:36 #6
- Registriert seit
- Dec 2011
- Beiträge
- 5
Hi!
Versuche mal deine Fragen zu beantworten:
1) Jeder Auftrag muss an jeder Maschine bearbeitet werden. Jeder Auftrag hat eine nummer(Id) und einen Zeitwert für jede Maschine. Das wars auch schon. Ressourcen, deadline etc. sollten keine Rolle spielen.
2) Wichtig ist, dass jede Maschine immer nur einen Auftrag bearbeiten kann(nicht 2 Aufträge zur gleichen zeit). Und dass Auftrag 1 nicht an Maschine1 bearbeitet werden kann, wenn er gerade an Maschine2 bearbeitet wird.
3) Wie gesagt,jeder Auftrag an jeder Maschine.
4) Die Durchlaufzeit minimieren. Allerdings nicht die Zeit für einen Auftrag, sondern die Zeit, bis alle n Aufträge fertig bearbeitet worden sind.
Vielen Dank dass du deine Hilfe anbietest!
lg
-
02.01.12 14:47 #7
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
Ist die Reihenfolge des Maschineneinsatzes für eine Auftrag beliebig?3) Wie gesagt,jeder Auftrag an jeder Maschine.
Bsp.: Wenn der Auftrag 1 bei 3 Maschinen 3 Zeit-Einheiten von Maschine 1 (M1), 1 Zeit-Einheit von M2 und 4 Zeit-Einheiten von M3 braucht -> A_i := (M1_i, M2_i, M3_i) -> A_1 := (3,1,4), kann ich dann mit 4 Zeit-Einheiten auf M3 beginnen oder muss ich mit 3 Einheiten bei M1 und anschließend M2 etc. anfangen?
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
02.01.12 22:23 #8
- Registriert seit
- Dec 2011
- Beiträge
- 5
Die Angabe ist leider etwas knapp. Gehe aber davon aus, dass die Reihenfolge beliebig ist.
-
02.01.12 23:21 #9
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
da ich den entsprechenden Code noch vorrätig hatte (http://www.tutorials.de/archiv/35834...gorithmus.html), habe ich dir mal ein kleines Beispiel gebastelt, schau mal hier:
//Edit: ich weiß, dass das Beispielproblem für die Parameterisierung des genetischen Algorithmus etwas unterdimensioniert ist, aber ich hatte keine Muße mehr mir ein größeres Beispiel auszudenken.
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
package de.tutorials; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; public class MachineAssignmentPlanOptimizationExample { public static class MachineSchedule { private int totalDuration = -1; private final int[] orderIndexSequence; private final ProductionPlant productionPlant; private final ProductionPlan productionPlan; private MachineSchedule(ProductionPlant productionPlant,ProductionPlan productionPlan, int... orderIndexSequence) { this.productionPlant = productionPlant; this.productionPlan = productionPlan; this.orderIndexSequence = orderIndexSequence; } public int getTotalDuration() { if (this.totalDuration == -1) { this.totalDuration = computeTotalDuration(); } return this.totalDuration; } private int computeTotalDuration() { int totalDuration = 0; int[] timeSlots = new int[productionPlant.getMachineCount()]; for (int i = 0; i < orderIndexSequence.length; i++) { int orderIndex = orderIndexSequence[i]; ProductionOrder productionOrder = productionPlan.getProductionOrder(orderIndex); for (int m = 0, mCount = productionPlant.getMachineCount(); m < mCount; m++) { if (m > 0) { timeSlots[m] = Math.max(timeSlots[m], timeSlots[m - 1]); } timeSlots[m] += productionOrder.getDurationOnMachine(m); totalDuration = Math.max(totalDuration, timeSlots[m]); } } return totalDuration; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < orderIndexSequence.length; i++) { sb.append(i + 1).append(" ").append(productionPlan.getProductionOrder(orderIndexSequence[i])).append("\n"); } return sb.toString(); } public boolean allDifferent() { // //naive approach... // Set<Integer> set = new HashSet<>(); // for(int i : orderIndexSequence){ // set.add(i); // } // return set.size() == orderIndexSequence.length; BitSet alreadySeenIndices = new BitSet(orderIndexSequence.length); for(int i = 0; i < orderIndexSequence.length;i++){ if(alreadySeenIndices.get(orderIndexSequence[i])){ return false; }else{ alreadySeenIndices.set(orderIndexSequence[i]); } } boolean allIndicesSeen = alreadySeenIndices.cardinality() == orderIndexSequence.length; return allIndicesSeen; } } public static class ProductionPlant { private final int machineCount; public ProductionPlant(int machineCount) { this.machineCount = machineCount; } public void execute(ProductionPlan productionPlan) { System.out.println("Computing optimal production plan..."); MachineSchedule machineSchedule = optimizeMachineAssignmentFor(productionPlan); System.out.println("Finished! Total Schedule Duration: " + machineSchedule.getTotalDuration()); System.out.println(machineSchedule); } private MachineSchedule optimizeMachineAssignmentFor(ProductionPlan productionPlan) { GeneticSolver<MachineSchedule> geneticSolver = new GeneticSolver<>(); SolutionFitness<MachineSchedule> best = geneticSolver.solve(new OptimalProductionScheduleProblem(this,productionPlan)); return best.solution; } public int getMachineCount() { return this.machineCount; } } public static class ProductionOrder { private final String id; private final int[] machineProcessingDurations; public ProductionOrder(String id, int... machineProcessingDurations) { this.id = id; this.machineProcessingDurations = machineProcessingDurations; } public int getDurationOnMachine(int machineIndex) { return this.machineProcessingDurations[machineIndex]; } @Override public String toString() { return "ProductionOrder [id=" + id + "]"; } } public static class ProductionPlan { private final List<ProductionOrder> productionOrders = new ArrayList<>(); public void add(ProductionOrder productionOrder) { this.productionOrders.add(productionOrder); } public ProductionOrder getProductionOrder(int orderIndex) { return productionOrders.get(orderIndex); } public int getOrderCount() { return this.productionOrders.size(); } } public static void main(String[] args) { /* * Problem Setting: * * Production Plant with 2 Machines (M1, M2) * 3 Production Orders to be scheduled. * * Order 1 (O1) needs 3 time units on M1 and 1 time unit on M2 * Order 2 (O2) needs 2 time units on M1 and 3 time units on M2 * Order 3 (O3) needs 1 time unit on M1 and 4 time units on M2 * * Optimization Goal: Minimize total processing duration! * * Example for an Optimal Production Setting: O3, O1, O2 * * M1 O3 O1 O1 O1 O2 O2 * M2 O3 O3 O3 O3 O1 O2 O2 O2 * --------------------------------------------- * Time 01 02 03 04 05 06 07 08 09 10 11 12 13 * */ ProductionPlant productionPlant = new ProductionPlant(2); ProductionPlan productionPlan = new ProductionPlan(); productionPlan.add(new ProductionOrder("O1", 3, 1)); productionPlan.add(new ProductionOrder("O2", 2, 3)); productionPlan.add(new ProductionOrder("O3", 1, 4)); productionPlant.execute(productionPlan); } static class OptimalProductionScheduleProblem implements Problem<MachineSchedule> { private final ProductionPlant productionPlant; private final ProductionPlan productionPlan; private final Random randomizer = new Random(); public OptimalProductionScheduleProblem(ProductionPlant productionPlant, ProductionPlan productionPlan) { this.productionPlant = productionPlant; this.productionPlan = productionPlan; } @Override public double fitness(MachineSchedule solution) { double fitness = -solution.getTotalDuration(); // Minimize total duration if(!solution.allDifferent()){ //penalty for invalid solutions fitness = Double.NEGATIVE_INFINITY; } return fitness; } @Override public MachineSchedule generate() { int[] orderIndices = new int[productionPlan.getOrderCount()]; for (int i = 0; i < orderIndices.length; i++) { orderIndices[i] = randomizer.nextInt(orderIndices.length); } return new MachineSchedule(productionPlant, productionPlan,orderIndices); } @Override public MachineSchedule combine(MachineSchedule first,MachineSchedule second) { int[] combinationOrderIndices = new int[productionPlan.getOrderCount()]; //single point crossover int crossOverIndex = randomizer.nextInt(combinationOrderIndices.length); for (int i = 0; i < crossOverIndex; i++) { combinationOrderIndices[i] = first.orderIndexSequence[i]; } for (int i = crossOverIndex; i < combinationOrderIndices.length; i++) { combinationOrderIndices[i] = second.orderIndexSequence[i]; } return new MachineSchedule(productionPlant, productionPlan,combinationOrderIndices); } @Override public MachineSchedule mutate(MachineSchedule solution) { //swap first and last production order int[] mutatedOrderSequence = solution.orderIndexSequence.clone(); int temp = mutatedOrderSequence[0]; mutatedOrderSequence[0] = mutatedOrderSequence[mutatedOrderSequence.length-1]; mutatedOrderSequence[mutatedOrderSequence.length-1] = temp; return new MachineSchedule(productionPlant, productionPlan,mutatedOrderSequence); } } static interface Problem<TSolution> extends FitnessFunction<TSolution>, SolutionGenerator<TSolution>, Combiner<TSolution>, Mutator<TSolution> { } static class Population<TSolution> { private final List<TSolution> solutions = new ArrayList<TSolution>(); public Population() { } public Population(SolutionFitness<TSolution>[] topElite) { for (SolutionFitness<TSolution> soluationFitness : topElite) { solutions.add(soluationFitness.solution); } } public int size() { return solutions.size(); } TSolution get(int index) { return solutions.get(index); } void add(TSolution solution) { solutions.add(solution); } } static interface FitnessFunction<TSolution> { double fitness(TSolution solution); } static interface SolutionGenerator<TSolution> { TSolution generate(); } static interface Combiner<TSolution> { TSolution combine(TSolution first, TSolution second); } static interface Mutator<TSolution> { TSolution mutate(TSolution solution); } static class SolutionFitness<TSolution> implements Comparable<SolutionFitness<TSolution>> { private final TSolution solution; private final double fitness; public SolutionFitness(TSolution solution, double fitness) { this.solution = solution; this.fitness = fitness; } @Override public int compareTo(SolutionFitness<TSolution> that) { return -Double.compare(this.fitness, that.fitness); } } static class GeneticSolver<TSolution> { private SolutionGenerator<TSolution> solutionGenerator; private Combiner<TSolution> combiner; private Mutator<TSolution> mutator; private FitnessFunction<TSolution> fitnessFunction; private Random randomizer = new Random(); /* * tuning options */ private int populationSize = 20; private int maxIterations = 100; private int maxIterationsWithNoImprovement = maxIterations; private double eliteRatioToKeep = 0.3; private double mutationProbability = 0.4; public SolutionFitness<TSolution> solve(Problem<TSolution> problem) { init(problem); System.out.println("Starting genetic search..."); SolutionFitness<TSolution> currentBest = null; Population<TSolution> currentPopulation = generatePopulation(); int eliteCount = (int) (populationSize * eliteRatioToKeep); int iterationsWithNoImprovement = 0; for (int i = 0; i < maxIterations; i++) { SolutionFitness<TSolution>[] solutionFitnesses = evaluate(currentPopulation); SolutionFitness<TSolution>[] rankedSolutions = computeRankingBestSolutionsFirst(solutionFitnesses); SolutionFitness<TSolution>[] topElite = takeTopEliteSolutions(eliteCount, rankedSolutions); currentPopulation = new Population<TSolution>(topElite); while (currentPopulation.size() < populationSize) { TSolution candidate = applyGeneticOperators(topElite); currentPopulation.add(candidate); } SolutionFitness<TSolution> nextBest = topElite[0]; if (currentBest != null) { if (currentBest.fitness == nextBest.fitness) { iterationsWithNoImprovement++; } else { iterationsWithNoImprovement = 0; } if (iterationsWithNoImprovement > maxIterationsWithNoImprovement) { System.out.println("No improvement for " + maxIterationsWithNoImprovement + " iterations"); break; } } currentBest = nextBest; //System.out.println("Current best: " + currentBest.solution + " " + currentBest.fitness); } return currentBest; } private SolutionFitness<TSolution>[] takeTopEliteSolutions(int eliteCount, SolutionFitness<TSolution>[] rankedSolutions) { return Arrays.copyOf(rankedSolutions, eliteCount); } void init(Problem<TSolution> problem) { this.solutionGenerator = problem; this.combiner = problem; this.mutator = problem; this.fitnessFunction = problem; } TSolution applyGeneticOperators(SolutionFitness<TSolution>[] topElite) { TSolution candidate; if (randomizer.nextDouble() < mutationProbability) { TSolution mutationCandidate = topElite[randomizer.nextInt(topElite.length)].solution; TSolution mutation = mutator.mutate(mutationCandidate); candidate = mutation; } else { TSolution firstCandidate = topElite[randomizer.nextInt(topElite.length)].solution; TSolution secondCandidate = topElite[randomizer.nextInt(topElite.length)].solution; TSolution combination = combiner.combine(firstCandidate,secondCandidate); candidate = combination; } return candidate; } SolutionFitness<TSolution>[] computeRankingBestSolutionsFirst(SolutionFitness<TSolution>[] individualFitnesses) { Arrays.sort(individualFitnesses); return individualFitnesses; } SolutionFitness<TSolution>[] evaluate(Population<TSolution> population) { SolutionFitness<TSolution>[] solutionFitnesses = new SolutionFitness[populationSize]; for (int i = 0; i < solutionFitnesses.length; i++) { TSolution solution = population.get(i); double fitness = fitnessFunction.fitness(solution); solutionFitnesses[i] = new SolutionFitness<TSolution>(solution,fitness); } return solutionFitnesses; } Population<TSolution> generatePopulation() { Population<TSolution> population = new Population<TSolution>(); for (int i = 0; i < populationSize; i++) { TSolution individual = solutionGenerator.generate(); population.add(individual); } return population; } } }
Ausgabe:
Code :1 2 3 4 5 6
Computing optimal production plan... Starting genetic search... Finished! Total Schedule Duration: 9 1 ProductionOrder [id=O3] 2 ProductionOrder [id=O1] 3 ProductionOrder [id=O2]
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
04.01.12 12:10 #10
- Registriert seit
- Dec 2011
- Beiträge
- 5
Danke für deine Mühe!
Muss mir deinen Code genauer ansehen. Falls Fragen auftreten, meld ich mich wieder.
lg





Zitieren

Login





