ERLEDIGT
NEIN
NEIN
ANTWORTEN
1
1
ZUGRIFFE
1951
1951
EMPFEHLEN
-
09.04.10 18:55 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
hier mal eine beispielhafte Lösung mit OPL. Nachspielen kann man die Geschichte mit der Trial Version des Analyst Studios:
http://www-01.ibm.com/software/websp...ching-edition/
Neben der Formulierung von Optimierungsproblemen in einer problemnahen Sprache (OPL)
erlaubt der CPLEX Optimierer auchdie Aufbereitung von Eingabe / Ausgabe-Daten mit JavaScript.
Code javascript: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
/********************************************* * OPL 6.3 Model * Author: thomas.darimont * Creation Date: 09.04.2010 at 18:30:49 *********************************************/ tuple Sweet{ key int index; string name; float weightInGramm; float kcal; } int maxWeightInGramm = 0; int numberOfSweets = 0; {Sweet} sweets = {}; //exotisches Eingabeformat lesen execute{ //mit großen Dataset von [url]http://www.tutorials.de/forum/diskussion/357969-quiz-15-lisas-osternest-3.html#post1855627[/url] var inputFileLocation = "C:/Users/thomas.darimont/opl/training/100k.txt"; //var inputFileLocation = "C:/Users/thomas.darimont/opl/training/easterEggs.dat"; var inputFile = new IloOplInputFile(); inputFile.open(inputFileLocation); maxWeightInGramm = parseInt(inputFile.readline()); while(!inputFile.eof){ var line = inputFile.readline(); if(line ==""){ break; } var currentSweetName = line; line = inputFile.readline(); var weightAndKcal = line.split(" "); var currentSweetWeight = parseInt(weightAndKcal[0]); var currentSweetKcal = parseInt(weightAndKcal[1]); sweets.add(numberOfSweets++,currentSweetName,currentSweetWeight,currentSweetKcal); } inputFile.close(); } //eigentliche Optimierung range sweetsRange = 1..numberOfSweets; dvar int take[sweetsRange] in 0..1; dexpr float currentWeight = sum(s in sweetsRange) item(sweets,s-1).weightInGramm * take[s]; dexpr float currentKcal = sum(s in sweetsRange) item(sweets,s-1).kcal * take[s]; maximize currentKcal; subject to{ ctMaxWeight: currentWeight <= maxWeightInGramm; } //Ergebnis Aufbereitung execute{ var sweetNames = ""; for(var i = 1; i < numberOfSweets+1;i++){ if(take[i] == 1){ var sweet = sweets.get(i-1); if(i > 1){ sweetNames += ", " } sweetNames += sweet.name ; } } writeln("Optimale Auswahl: " + sweetNames); writeln("Masse: " + currentWeight); writeln("Nährwert: " + cplex.getObjValue()); }
Ausgabe für Standard Beispiel:
Code :1 2 3
Optimale Auswahl: Nougat-Eier, Spannungs-Eier, Melker Runzelhase, Lynt Platinhase Masse: 467 Nährwert: 2488
Ausgabe für OnlyFoos 100k.txt Eingabe:
Code :1 2 3
Optimale Auswahl: , ei-2383, ei-8465, ei-23359, ei-40669, ei-47555, ei-62142, ei-62736, ei-66390, ei-77632, ei-79046 Masse: 500 Nährwert: 9983
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
-
Hi Tom,
das ist ja schon fast geschummelt
Ich könnte noch eine Lösung in Mosel nachreichen, aber die würde wohl auch nicht recht viel anders ausschauen.
Grüße,
Matthias„Gib einem Menschen einen Fisch, und er wird für einen Tag satt. Lehre ihn Fischen, und er wird ein Leben lang satt.“
“For every complex problem, there is an answer that is short, simple and wrong.”
“Pessimism is safe, but optimism is a lot faster!”
Aktuelles Coding Quiz: #17 - Wörter kreuz und quer
Ähnliche Themen
-
[Quiz #9] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 21.07.09, 23:35 -
[QUIZ#7] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 08.12.08, 09:45 -
[QUIZ #2] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 28.09.08, 14:08 -
[QUIZ#2] Thomas Darimont (Scala)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 26.09.08, 01:52 -
[Quiz #1] Thomas Darimont (Scala)
Von Thomas Darimont im Forum ArchivAntworten: 2Letzter Beitrag: 22.09.08, 10:19







Login





