Initial import from SVN (svn://129.187.65.205/platz/trunk, r1)

Converted from SVN working copy. Original SVN server no longer available.
Original commit date: 2006-05-03, author: sm
This commit is contained in:
Michael Stangl
2006-05-03 22:21:24 +02:00
commit 97382c8e9d
15 changed files with 1669 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
*.pyc
__pycache__/
*.pyo
work/ausgabe.txt

17
bin/run Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/bash
# -------------------------------------
# Wo ist das Programm hinkopiert
export PLATZ=/Users/sm/develop/python/platz
echo $PLATZ
# -------------------------------------
# Umgebung setzen
export PLATZ_CFG=$PLATZ/cfg
export PLATZ_LIBS=$PLATZ/libs
export PLATZ_WORK=$PLATZ/work
# Wo sind die Pythonskripte ?
export PYTHONPATH=$PYTHONPATH:$PLATZ_LIBS
# Startet das Sitzplatzprogramm
python $PLATZ_LIBS/platz.py

21
bin/run.bat Normal file
View File

@@ -0,0 +1,21 @@
echo off
REM Hier die Umgebung anpassen
REM -------------------------------------
REM Wo ist das Programm hinkopiert
set PLATZ=h:\develop\python\platz
REM Wo liegt der Interpreter
set PYTHONSHELL=l:\tools\i486_nt\python24
REM -------------------------------------
REM Umgebung setzen
set PLATZ_CFG=%PLATZ%\cfg
set PLATZ_LIBS=%PLATZ%\libs
set PLATZ_WORK=%PLATZ%\work
set PATH=%PYTHONSHELL%;%PATH%
REM Wo sind die Pythonskripte ?
set PYTHONPATH=%PLATZ_LIBS%
REM Startet das Sitzplatzprogramm
python %PLATZ_LIBS%\platz.py

10
cfg/platz.cfg Normal file
View File

@@ -0,0 +1,10 @@
[Config]
Zyklusdatei=$PLATZ_CFG/zyklus.cfg
Zyklusart=Easy
Strafendatei=$PLATZ_CFG/strafen.cfg
Tischedatei=$PLATZ_WORK/test1/tische.ini
XMLdatei_Bestellung=$PLATZ_WORK/test1/bestellung.xml
AusgabeTischePersonen=$PLATZ_WORK/test1/TischePersonen.txt
AusgabePersonTisch=$PLATZ_WORK/test1/PersonenTische.csv

19
cfg/strafen.cfg Normal file
View File

@@ -0,0 +1,19 @@
# Strafpunkte fuer die Trennung einer Gruppe:
# Aufbau: Anzahl der Trennungen = { Gruppengroesse:Strafpunkte, .. }
[Trennung]
1={ 2:-10, 3:-8, 4:-6, 6:-4, 10:-2 }
2={ 3:-20, 4:-8, 6:-6, 10:-4 }
3={ 4:-30, 6:-10, 10:-6 }
# Wieviele Strafpunkte bekommt man
# wenn zwei Gruppen nicht am Nachbartisch sitzen
# wenn einer aus einer Gruppe allein sitzen muss
[Globals]
NichtNachbar=-3
Allein=-30
TischWertigkeiten={ 1:-3, 2:-2, 3:-1 }
# TischWertigkeiten sind Strafpunkte fuer schlechte Ausnutzung der
# freien Sitze an einem Tisch.
# An einem Tisch, der bis auf einen Platz voll besetzt ist,
# kann niemand mehr gut dazu. wenn zwei Plaetze frei sind, ist das etwas besser.

8
cfg/zyklus.cfg Normal file
View File

@@ -0,0 +1,8 @@
[Easy]
Abfolge='e,'+'s,z,m,j,z,'*3+'s'
Anzahl='10,'+'5,x,2,x,x,'*3+'1'
[Simple]
Abfolge='e,s'
Anzahl='20,1'

1092
libs/Strukturdaten.py Executable file

File diff suppressed because it is too large Load Diff

194
libs/ga.py Executable file
View File

@@ -0,0 +1,194 @@
"""
Enthaelt alle Datenstrukturen um die einen genetischen Algorithmus darzustellen
"""
from random import *
from itertools import chain
from string import split
from copy import deepcopy
import ConfigParser, os
__author__ = "Michael Stangl"
__version__ = "$Revision: $"
__date__ = "$Date: $"
__copyright__ = "Copyright (c) 2005 Michael Stangl"
__license__ = "Python"
class Loesung:
"""ein einfaches Loesungsobjekt. Taugt um es als Superklasse zu ueberladen"""
def __init__(self, *Argumente, **keywords):
self.Id = -1 * randint(1,100)
self.value = self.Id
print " initialisiere Loesung " + repr(self.Id)
def mutieren(self):
print " mutiere Loesung " + repr(self.Id)
self.Id = -1 * randint(1,100)
self.value = self.Id
return self
def kreuzen(self,obj):
print " kreuze Loesung " + repr(self.Id)
def laden(self):
print " lade Loesung " + repr(self.Id)
def speichern(self):
print " speichere Loesung " + repr(self.Id)
def __repr__(self):
return ' L '+ repr(self.Id)
def __cmp__(self,other):
if self.value < other.value: return -1
elif self.value == other.value: return 0
else: return 1
class Zyklus:
"""Definiert wie ein Zyklus ablaufen soll:
wann sollen Objekte generiert, gekreuzt, mutiert und selektiert werden
wieviele sind davon jeweils betroffen
"""
def __init__(self, Name='default',
Abfolge='e,m,s',
Anzahl='10,10,10'):
self.Name = Name
self.Abfolge = split(Abfolge, ',')
self.Anzahl = split(Anzahl,',')
def __repr__(self):
return( "Zyklus: %s\n"
" Zyklusabfolge: %s\n"
" Anzahl : %s\n"
% ( self.Name,
self.Abfolge,
self.Anzahl ) )
def laden(self, FileName, ZyklusName):
config = ConfigParser.ConfigParser()
ZyklusPath= os.path.split( FileName )
ZyklusPath= os.path.join( FileName )
config.read( ZyklusPath )
self.Name = ZyklusName
Abfolge = eval( config.get( ZyklusName, 'Abfolge' ))
Anzahl = eval( config.get( ZyklusName, 'Anzahl' ))
self.Abfolge = Abfolge.split(',')
self.Anzahl = Anzahl.split(',')
pass
class Farm:
"""enthaelt und pflegt Loesungsobjekte, kreuzt, mutiert, selektiert sie"""
def __init__(self, Zyklus, Klasse, *Arguments, **keywords):
"""initialisiere die Farm
mit einem definerten Zyklus fuer die Zucht und dem gewunschten zu Zuchtobjekt
"""
self.Position = 0
self.Pool = []
self.PoolNeu = []
self.Zyklus = Zyklus
self.Klasse = Klasse
for Aktion, Zahl in zip(Zyklus.Abfolge,Zyklus.Anzahl):
#print 'Aktion:',Aktion
#print 'Zahl:',Zahl
self.Position = self.Position + 1
if Aktion == 'e':
self.LoesungenErzeugen(int(Zahl), Klasse, *Arguments, **keywords)
elif Aktion == 'm':
self.LoesungenMutieren(int(Zahl))
elif Aktion == 's':
self.LoesungenAuswaehlen(int(Zahl))
elif Aktion == 'S':
self.LoesungenZufaelligAuswaehlen(int(Zahl))
elif Aktion == 'j':
self.LoesungenBehalten()
elif Aktion == 'z':
self.NeueGeneration()
else:
pass
def LoesungenErzeugen(self, N, Klasse, *Arguments, **keywords):
""" Erzeuge Loesungen"""
for i in xrange(N):
L = Klasse( *Arguments, **keywords )
self.Pool.append(L)
#print repr(len(self.Pool)) + ' Loesungsobjekte erzeugt'
def LoesungenKreuzen(self,N):
"""mache aus zwei Loesungen eine neue"""
#print "+ Loesungen kreuzen"
for i in xrange(N):
self.Pool.append(self.Pool[i].kreuzen())
def LoesungenMutieren(self, N):
"""veraendere die Loesung, so dass sie hoffentlich besser wird"""
print "+ Loesungen mutieren: ", N
for n in xrange(N):
#print " -"+repr(n+1)+" mal"
for L in chain(self.Pool):
O = deepcopy(L)
self.PoolNeu.append(O.mutieren())
#print self
def LoesungenBehalten(self):
"""Behalte alle Eltern aus dem alten Pool"""
print "+ alten Loesungspool behalten"
self.PoolNeu = self.PoolNeu + self.Pool
#print self
def NeueGeneration(self):
"""Loesche alle bisherigen Eltern und mache einen neuen Zyklus"""
print "+ Neue Generation erzeugen"
self.Pool = self.PoolNeu
self.PoolNeu = []
#print self
#print "---------------"
def LoesungenAuswaehlen(self, N):
"""Selektiere die besten Loesungen"""
print "+ die besten -", repr(N) +" - Loesungen auswaehlen -"
for i in xrange(N):
L = max(self.Pool)
self.PoolNeu.append(L)
self.Pool.remove(L)
#print self
def LoesungenZufaelligAuswaehlen(self, N):
"""Selektiere zufaellig Loesungen"""
print "+ zufaellig auswaehlen -"+ repr(N) + "-"
for i in xrange(N):
self.PoolNeu.append(choice(self.Pool))
#print self
def __repr__(self):
"""Drucke die Farm am Bildschirm aus"""
return( " gewaehlte Objekte (%d): %s\n"
" Elternobjekte (%d) : %s\n"
% ( len(self.PoolNeu), self.PoolNeu,
len(self.Pool), self.Pool))
def laden(self):
print "+ laden"
def speichern(self):
print "+ speichern"
def Bester(self):
return max(self.Pool)
if __name__ == '__main__':
print "\n-- Erzeuge Zyklus:"
#Z = Zyklus( Name='Easy',
#Abfolge='e,s,+,m,j,S,+',
#Anzahl='10,5,x,2,x,5,x'
#)
Z = Zyklus( Name='Easy',
Abfolge='e,'+'s,z,m,j,z,'*3+'s',
Anzahl='10,'+'5,x,2,x,x,'*3+'1'
)
print Z
print "\n-- Erzeuge Loesungen:"
L1 = Loesung()
L2 = Loesung()
L3 = Loesung()
print L1, L2, L3
print 'L1 > L2 =', L1 > L2
print 'L2 > L3 =', L2 > L3
print 'L1 > L3 =', L1 > L3
A=2
B=3
print "\n-- Erzeuge Farm:"
F1 = Farm( Z, Loesung, Arg1=A, Arg2=B, Arg3=L1 )
print F1
else:
pass

73
libs/platz.py Executable file
View File

@@ -0,0 +1,73 @@
"""
Enthaelt alle Datenstrukturen um die Sitzplatzverteilung als GA zu variieren
"""
__author__ = "Michael Stangl"
__version__ = "$Revision: $"
__date__ = "$Date: $"
__copyright__ = "Copyright (c) 2005 Michael Stangl"
__license__ = "Python"
from ga import *
from Strukturdaten import *
import ConfigParser
if __name__ == '__main__':
# lade Vorgaben fuer Optimierungszyklus und
config = ConfigParser.ConfigParser()
ConfigPath= os.path.join( os.getenv('PLATZ_CFG'), 'platz.cfg')
config.read( os.path.expandvars( ConfigPath ))
Zyklusconfig = os.path.normpath( config.get( 'Config', 'Zyklusdatei' ))
Zyklusconfig = os.path.expandvars( Zyklusconfig )
Zyklusart = config.get( 'Config', 'Zyklusart' )
Strafenconfig = os.path.normpath(config.get( 'Config', 'Strafendatei' ))
Strafenconfig = os.path.expandvars( Strafenconfig )
## print Strafenconfig
# lade Eingabedateien
Tischconfig = os.path.normpath(config.get( 'Config', 'Tischedatei' ))
Tischconfig = os.path.expandvars(Tischconfig)
Bestellung = os.path.normpath(config.get( 'Config', 'XMLdatei_Bestellung' ))
Bestellung = os.path.expandvars( Bestellung )
# lade Ausgabedateien
AusgabeTP= os.path.normpath(config.get( 'Config', 'AusgabeTischePersonen' ))
AusgabeTP = os.path.expandvars(AusgabeTP)
AusgabePT = os.path.normpath(config.get( 'Config', 'AusgabePersonTisch' ))
AusgabePT = os.path.expandvars(AusgabePT)
Z = Zyklus()
Z.laden( Zyklusconfig, Zyklusart )
print Z
SL = Strafliste()
SL.laden( Strafenconfig )
print SL
TE = Tische()
TE.laden( Tischconfig )
print TE
InputFile = XMLConfig()
[ PN, GN, VIPGruppe, VIPListePT ] = InputFile.laden( Bestellung )
print PN
print GN
print VIPGruppe
print VIPListePT
print "\n-- Erzeuge Farm:"
F1 = Farm( Z, Sitzplatzverteilung, Tische=TE, Gruppen=GN, Strafen=SL, VIPListe=VIPListePT, VIPs=VIPGruppe )
#print F1
S = F1.Bester()
print S
S.speichern( AusgabeTP, AusgabePT )
else:
pass

View File

@@ -0,0 +1,19 @@
AA, A, Dipl. Ing. = Hof 1, Tisch 1, Nummer 2
AA, A, Dipl. Ing. = Hof 1, Tisch 1, Nummer 3
AA, A, Dipl. Ing. = Hof 1, Tisch 1, Nummer 4
AA, A, Dipl. Ing. = Hof 1, Tisch 1, Nummer 5
AA, A, Dipl. Ing. = Hof 1, Tisch 1, Nummer 6
AA, A, Dipl. Ing. = Hof 1, Tisch 2, Nummer 2
AA, A, Dipl. Ing. = Hof 1, Tisch 2, Nummer 3
AA, A, Dipl. Ing. = Hof 1, Tisch 2, Nummer 4
AA, A, Dipl. Ing. = Hof 1, Tisch 2, Nummer 5
AA, A, Dipl. Ing. = Hof 1, Tisch 2, Nummer 6
BB, B, Prof. = Hof 1, Tisch 3, Nummer 2
BB, B, Prof. = Hof 1, Tisch 3, Nummer 3
BB, B, Prof. = Hof 1, Tisch 3, Nummer 4
BB, B, Prof. = Hof 1, Tisch 3, Nummer 5
CC, C, Dr. = Hof 1, Tisch 4, Nummer 2
CC, C, Dr. = Hof 1, Tisch 4, Nummer 3
CC, C, Dr. = Hof 1, Tisch 4, Nummer 4
CC, C, Dr. = Hof 1, Tisch 4, Nummer 5
CC, C, Dr. = Hof 1, Tisch 4, Nummer 6
1 AA A Dipl. Ing. = Hof 1 Tisch 1 Nummer 2
2 AA A Dipl. Ing. = Hof 1 Tisch 1 Nummer 3
3 AA A Dipl. Ing. = Hof 1 Tisch 1 Nummer 4
4 AA A Dipl. Ing. = Hof 1 Tisch 1 Nummer 5
5 AA A Dipl. Ing. = Hof 1 Tisch 1 Nummer 6
6 AA A Dipl. Ing. = Hof 1 Tisch 2 Nummer 2
7 AA A Dipl. Ing. = Hof 1 Tisch 2 Nummer 3
8 AA A Dipl. Ing. = Hof 1 Tisch 2 Nummer 4
9 AA A Dipl. Ing. = Hof 1 Tisch 2 Nummer 5
10 AA A Dipl. Ing. = Hof 1 Tisch 2 Nummer 6
11 BB B Prof. = Hof 1 Tisch 3 Nummer 2
12 BB B Prof. = Hof 1 Tisch 3 Nummer 3
13 BB B Prof. = Hof 1 Tisch 3 Nummer 4
14 BB B Prof. = Hof 1 Tisch 3 Nummer 5
15 CC C Dr. = Hof 1 Tisch 4 Nummer 2
16 CC C Dr. = Hof 1 Tisch 4 Nummer 3
17 CC C Dr. = Hof 1 Tisch 4 Nummer 4
18 CC C Dr. = Hof 1 Tisch 4 Nummer 5
19 CC C Dr. = Hof 1 Tisch 4 Nummer 6

View File

@@ -0,0 +1,27 @@
[1]
Dipl. Ing. A AA = Hof 1, Tisch 1, Nummer 1
Dipl. Ing. A AA = Hof 1, Tisch 1, Nummer 2
Dipl. Ing. A AA = Hof 1, Tisch 1, Nummer 3
Dipl. Ing. A AA = Hof 1, Tisch 1, Nummer 4
Dipl. Ing. A AA = Hof 1, Tisch 1, Nummer 5
[2]
Dipl. Ing. A AA = Hof 1, Tisch 2, Nummer 1
Dipl. Ing. A AA = Hof 1, Tisch 2, Nummer 2
Dipl. Ing. A AA = Hof 1, Tisch 2, Nummer 3
Dipl. Ing. A AA = Hof 1, Tisch 2, Nummer 4
Dipl. Ing. A AA = Hof 1, Tisch 2, Nummer 5
[3]
Prof. B BB = Hof 1, Tisch 3, Nummer 1
Prof. B BB = Hof 1, Tisch 3, Nummer 2
Prof. B BB = Hof 1, Tisch 3, Nummer 3
Prof. B BB = Hof 1, Tisch 3, Nummer 4
[4]
Dr. C CC = Hof 1, Tisch 4, Nummer 1
Dr. C CC = Hof 1, Tisch 4, Nummer 2
Dr. C CC = Hof 1, Tisch 4, Nummer 3
Dr. C CC = Hof 1, Tisch 4, Nummer 4
Dr. C CC = Hof 1, Tisch 4, Nummer 5

32
work/test1/bestellung.xml Executable file
View File

@@ -0,0 +1,32 @@
<Bestellung>
<Gruppe>
<Person>
<Vorname>A</Vorname>
<Nachname>AA</Nachname>
<Titel>Dipl. Ing.</Titel>
</Person>
<Anzahl>10</Anzahl>
</Gruppe>
<Gruppe>
<Person>
<Vorname>B</Vorname>
<Nachname>BB</Nachname>
<Titel>Prof.</Titel>
</Person>
<Anzahl>4</Anzahl>
</Gruppe>
<Gruppe>
<Person>
<Vorname>C</Vorname>
<Nachname>CC</Nachname>
<Titel>Dr.</Titel>
</Person>
<Anzahl>5</Anzahl>
</Gruppe>
</Bestellung>

28
work/test1/tische.ini Executable file
View File

@@ -0,0 +1,28 @@
[1]
Nummer=1
Hof=1
Plaetze=5
Nachbarliste=[2]
Koordinaten=(1,1)
[2]
Nummer=2
Hof=1
Plaetze=5
Nachbarliste=[1,3]
Koordinaten=(2,1)
[3]
Nummer=3
Hof=1
Plaetze=5
Nachbarliste=[2,4]
Koordinaten=(3,1)
[4]
Nummer=4
Hof=1
Plaetze=5
Nachbarliste=[3]
Koordinaten=(4,1)

90
work/test2/bestellung.xml Executable file
View File

@@ -0,0 +1,90 @@
<Bestellung>
<!-- einfache Gruppe aus drei Personen
mit festem Wunsch fuer einen Tisch-->
<Gruppe>
<Person>
<Vorname>Michael</Vorname>
<Nachname>Stangl</Nachname>
<Titel>Dipl. Ing.</Titel>
</Person>
<Person>
<Vorname>Magnus</Vorname>
<Nachname>Mueller</Nachname>
<Titel>Professor</Titel>
</Person>
<Person>
<Vorname>Bernd Robert</Vorname>
<Nachname>Hoehn</Nachname>
<Titel>Professor</Titel>
</Person>
<Tisch>1</Tisch>
</Gruppe>
<!-- einfache Gruppe aus zwei Personen -->
<Gruppe>
<Person>
<Vorname>Heinz</Vorname>
<Nachname>Bruegge</Nachname>
</Person>
<Person>
<Vorname>Hubert</Vorname>
<Nachname>K</Nachname>
</Person>
</Gruppe>
<Gruppe>
<Person>
<Vorname>Heinz</Vorname>
<Nachname>Ruehmann</Nachname>
</Person>
<Person>
<Vorname>Marlene</Vorname>
<Nachname>Dietrich</Nachname>
</Person>
</Gruppe>
<Gruppe>
<Person>
<Vorname>Sergio</Vorname>
<Nachname>Leone</Nachname>
</Person>
<Person>
<Vorname>Walter</Vorname>
<Nachname>Emmerlich</Nachname>
</Person>
<Person>
<Vorname>Harald</Vorname>
<Nachname>Junke</Nachname>
</Person>
<Person>
<Vorname>Willi</Vorname>
<Nachname>Brandt</Nachname>
<Titel>Dr.</Titel>
</Person>
<Person>
<Vorname>Michael</Vorname>
<Nachname>Schumacher</Nachname>
</Person>
<Person>
<Vorname>Ralf</Vorname>
<Nachname>Schumacher</Nachname>
</Person>
<Person>
<Vorname>Heinz Harald</Vorname>
<Nachname>Frenzen</Nachname>
</Person>
</Gruppe>
<!--Gruppe aus drei anonymen Personen-->
<Gruppe>
<Person>
<Vorname>Bestelli</Vorname>
<Nachname>Bloedkopf</Nachname>
</Person>
<Anzahl>3</Anzahl>
</Gruppe>
</Bestellung>

35
work/test2/tische.ini Executable file
View File

@@ -0,0 +1,35 @@
[1]
Nummer=1
Hof=1
Plaetze=3
Nachbarliste=[2]
Koordinaten=(1,1)
[2]
Nummer=2
Hof=1
Plaetze=5
Nachbarliste=[1,3]
Koordinaten=(2,1)
[3]
Nummer=3
Hof=1
Plaetze=3
Nachbarliste=[2,4]
Koordinaten=(3,1)
[4]
Nummer=4
Hof=1
Plaetze=4
Nachbarliste=[3,5]
Koordinaten=(4,1)
[5]
Nummer=5
Hof=1
Plaetze=4
Nachbarliste=[4]
Koordinaten=(5,1)