/*------------------------------------------------------------- Copyright (C) 2000 Alessandro Macri. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ import java.io.*; import java.awt.*; import java.awt.event.*; public class Bio extends Frame implements WindowListener { private Bio eigen; private Evolution evolution; // Hauptprogramm: // Dient eigentlich nur als Durchreiche. public static void main(String [] arg) { (new Bio(arg)).aufbau(); } public Bio(String[] arg) { this.eigen=this; this.evolution=argumentAuswertung(arg); } // Fensterlayout: // Erzeugt alle Kontrollelemente private void aufbau () { this.setSize(800,800); this.setLayout(new BorderLayout()); this.setVisible(true); this.addWindowListener(this); // Textbereich für die Ausgabe des besten Gens this.add(evolution.getTextBereich(),"Center"); // Bereich für die Ausgabe der Statistik this.add(evolution.getStatistik(),"West"); // Statuszeile this.add(evolution.getStatuszeile(),"South"); // Knoepfe werden im eigenen Container abgelegt Panel knöpfe=new Panel(); knöpfe.setLayout(new GridLayout(0,1)); Button gen1=knopf("Gen+1",knöpfe); Button gen10=knopf("Gen+10",knöpfe); Button gen100=knopf("Gen+100",knöpfe); Button kopie=knopf("Kopie nach STDOUT",knöpfe); Button ende=knopf("Ende",knöpfe); this.add(knöpfe,"East"); // Die Knöpfe müssen natürlich auch überwacht werden gen1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((Component) eigen).setCursor(new Cursor(Cursor.WAIT_CURSOR)); evolution.nächsteGeneration(); ((Component) eigen).setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }); gen10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((Component) eigen).setCursor(new Cursor(Cursor.WAIT_CURSOR)); for (int i=0;i<10;i++) evolution.nächsteGeneration(); ((Component) eigen).setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }); gen100.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ((Component) eigen).setCursor(new Cursor(Cursor.WAIT_CURSOR)); for (int i=0;i<100;i++) evolution.nächsteGeneration(); ((Component) eigen).setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }); kopie.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { evolution.erzeugeAusdruck(); } }); ende.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { eigen.programmende(); } }); } private Button knopf(String name, Container wohin) { Button knopf=new Button(name); wohin.add(knopf); return knopf; } // Hier kommt die Maus ... // ... nein, hier kommt der WindowListener public void windowOpened(WindowEvent e) { } public void windowClosing(WindowEvent e) { this.programmende(); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } // Argumentauswertung: // Hier werden die Optionen der Kommandozeile uebersetzt ausgewertet. private static Evolution argumentAuswertung (String [] arg) { int zähler=0; // Operator, Parameter: String opt,par; // Aminosäurentabelle File aminoDatei=new File("Standard.amino"); Aminos amino; // Mutationsrate double mut=0.01; // Population: int pop=20; // Genpool Gen[] gene; while (zähler1000) fehler("Population zu groß"); else if(popNeu%2==1) fehler("Population muß geradzahlig sein"); else { pop=popNeu; zähler++; } } else if(opt.equals("-m") || opt.equals("-mutation")) { double mutNeu=mut; try { mutNeu=Double.valueOf(arg[zähler]).doubleValue(); } catch (NumberFormatException e) { fehler(arg[zähler]+": keine Fließkommazahl"); } if(mutNeu<0.0) { fehler(mutNeu+": Mutationsrate zu klein");} else if(mutNeu>1.0) { fehler(mutNeu+": Mutationsrate zu groß");} else { mut=mutNeu; zähler++; } } else { fehler("Ungültige Option: "+opt); } } // Aminosäuren einlesen ... amino=new Aminos(aminoDatei); // ... Genpool erstellen ... gene=new Gen[pop]; for(zähler=pop-1; zähler>=0; zähler--) gene[zähler]=new Gen(amino); // ... und zurück. return new Evolution(gene,mut); } private void programmende() { this.dispose(); System.exit(0); } private static void fehler (String text) { System.err.println(text); System.err.println("Optionen:"); System.err.println(" -a , -amino "); System.err.println(" liest Aminosäuren aus Datei ein "+ "(sonst:Standard.amino)"); System.err.println(" -m , -mutation "); System.err.println(" setzt Mutationsrate auf (sonst XXX)"); System.err.println(" -p , -population "); System.err.println(" setzt Bevölkerung auf (sonst XXX)"); System.exit(1); } }