📄 timetableallocateroomsform.cpp
字号:
/*************************************************************************** timetableallocateroomsform.cpp - description ------------------- begin : Tue Apr 22 2003 copyright : (C) 2003 by Lalescu Liviu email : liviu@lalescu.ro ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "timetableallocateroomsform.h"#include "fetmainform.h"#include "genetictimetable_defs.h"#include "genetictimetable.h"#include "fet.h"#include <qcombobox.h>#include <qmessagebox.h>#include <qgroupbox.h>#include <qspinbox.h>#include <qcheckbox.h>#include <qpushbutton.h>#include <qlineedit.h>#include <qtable.h>#include <qapplication.h>#include <qtextedit.h>#include <qstring.h>#include <qtextstream.h>#include <qfile.h>static const bool SIMULATION_LOGGING=false; //warning: making "true" here slows down the program, //introducing an additional sorting if the evolution method is three-tournament.#include <iostream>#include <fstream>using namespace std;static QMutex mutex;static SpaceSolvingThread spaceSolvingThread;extern QApplication *pqapplication;//Represents the current state - simulation running or stopped.extern bool simulation_running;//Communication variables when the simulation is running.static bool simulation_stopped=false;static bool simulation_paused=false;static bool simulation_write_results=false;static bool simulation_save_position=false;extern bool students_schedule_ready;extern bool teachers_schedule_ready;extern bool rooms_schedule_ready;extern SpaceChromosome best_space_chromosome;extern QString spaceConflictsString;TimetableAllocateRoomsForm::TimetableAllocateRoomsForm(){ simulation_running=false;}TimetableAllocateRoomsForm::~TimetableAllocateRoomsForm(){}SpaceSolvingThread::SpaceSolvingThread(){ callingForm=NULL;}void SpaceSolvingThread::run(){ assert(callingForm!=NULL); pqapplication->lock(); callingForm->startPushButton->setDisabled(TRUE); callingForm->stopPushButton->setEnabled(TRUE); callingForm->pausePushButton->setEnabled(TRUE); callingForm->savePositionPushButton->setEnabled(TRUE); callingForm->loadPositionPushButton->setDisabled(TRUE); callingForm->initializeUnallocatedPushButton->setDisabled(TRUE); callingForm->initializeRandomlyPushButton->setDisabled(TRUE); callingForm->closePushButton->setDisabled(TRUE); pqapplication->unlock(); callingForm->simulationRunning(); pqapplication->lock(); callingForm->startPushButton->setEnabled(TRUE); callingForm->stopPushButton->setDisabled(TRUE); callingForm->pausePushButton->setDisabled(TRUE); callingForm->savePositionPushButton->setDisabled(TRUE); callingForm->loadPositionPushButton->setEnabled(TRUE); callingForm->initializeUnallocatedPushButton->setEnabled(TRUE); callingForm->initializeRandomlyPushButton->setEnabled(TRUE); callingForm->closePushButton->setEnabled(TRUE); pqapplication->unlock();}void TimetableAllocateRoomsForm::generationLogging(int generation){ assert(gt.rules.initialized && gt.rules.internalStructureComputed); assert(gt.spacePopulation.initialized); SpaceChromosome& c1=gt.spacePopulation.bestChromosome(gt.rules); if(SIMULATION_LOGGING==true){ SpaceChromosome& c2=gt.spacePopulation.worstChromosome(gt.rules); SpaceChromosome& c3=gt.spacePopulation.medianChromosome(gt.rules); double th=gt.spacePopulation.totalHardFitness(gt.rules); double ts=gt.spacePopulation.totalSoftFitness(gt.rules); //write to log file logg<<"Generation number "<<generation+1<<endl; logg<<" Best chromosome:"; logg<<" HardFitness="<<c1.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); logg<<", SoftFitness="<<c1.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; logg<<" Median chromosome:"; logg<<" HardFitness="<<c3.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); logg<<", SoftFitness="<<c3.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; logg<<" Worst chromosome:"; logg<<" HardFitness="<<c2.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); logg<<", SoftFitness="<<c2.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; logg<<" Medium HardFitness="<<th/gt.spacePopulation.n; logg<<", Medium SoftFitness="<<ts/gt.spacePopulation.n; logg<<endl<<flush; //write to display cout<<"Generation number "<<generation+1<<endl; cout<<" Best chromosome:"; cout<<" HardFitness="<<c1.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); cout<<", SoftFitness="<<c1.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; cout<<" Median chromosome:"; cout<<" HardFitness="<<c3.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); cout<<", SoftFitness="<<c3.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; cout<<" Worst chromosome:"; cout<<" HardFitness="<<c2.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours); cout<<", SoftFitness="<<c2.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)<<endl; cout<<" Medium HardFitness="<<th/gt.spacePopulation.n; cout<<", Medium SoftFitness="<<ts/gt.spacePopulation.n; cout<<endl<<flush; } //write to the Qt interface QString s; s = "Generation:"; s+=QString::number(generation+1)+"\n"; s+="Compulsory constraints conflicts:"; s+=QString::number(c1.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours))+"\n"; s+="Non-compulsory constraints conflicts:"; s+=QString::number(c1.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours)); pqapplication->lock(); currentResultsTextEdit->setText(s); currentResultsTextEdit->repaint(); pqapplication->unlock();}void TimetableAllocateRoomsForm::simulationRunning(){ simulation_running=true; pqapplication->lock();#ifdef WIN32 QMessageBox::information(NULL, QObject::tr("FET information"), QObject::tr("Entering simulation..."));#else QMessageBox::information(this, QObject::tr("FET information"), QObject::tr("Entering simulation..."));#endif pqapplication->unlock(); //To print current time (starting time) time_t ltime; tzset(); /* Get UNIX-style time and display as number and string. */ time( <ime ); //open log file QString s=OUTPUT_DIR; s+=FILE_SEP; s+=SPACE_LOG_FILENAME_TXT; logg.open((const char*)s); logg<<"Population number="<<gt.spacePopulation.populationNumber()<<endl; logg<<"Current time is: "<<ctime( <ime )<<endl<<endl; for(int i=0; i<max_generations; i++){ if(evolution_method==1){ gt.spacePopulation.evolve1(gt.rules); } else if(evolution_method==2){ gt.spacePopulation.evolve2(gt.rules); } else{ QMessageBox::critical(this, QObject::tr("FET information"), QObject::tr("error in timetableallocateroomsform.cpp (1)")); assert(0); } pqapplication->lock(); generationLogging(i); pqapplication->unlock(); time_t tmp; time( &tmp ); if(tmp-ltime>=timelimit){ break; } mutex.lock(); if(simulation_stopped){ mutex.unlock(); break; } else mutex.unlock(); mutex.lock(); if(simulation_paused){ simulation_paused=0; mutex.unlock(); pqapplication->lock();#ifdef WIN32 QMessageBox::information(NULL, QObject::tr("FET information"), QObject::tr("Simulation paused.\nPress button to continue."));#else QMessageBox::information(this, QObject::tr("FET information"), QObject::tr("Simulation paused.\nPress button to continue."));#endif pqapplication->unlock(); } else mutex.unlock(); mutex.lock(); if(simulation_save_position){ simulation_save_position=false; QString s=INPUT_FILENAME_XML.right(INPUT_FILENAME_XML.length()-INPUT_FILENAME_XML.findRev(FILE_SEP)-1); gt.spacePopulation.write(gt.rules, OUTPUT_DIR+FILE_SEP+s+"_space_population_state.txt"); mutex.unlock(); pqapplication->lock();#ifdef WIN32 QMessageBox::information(NULL, QObject::tr("FET information"), QObject::tr("Simulation position saved to hard disk.\nPress button to continue."));#else QMessageBox::information(this, QObject::tr("FET information"), QObject::tr("Simulation position saved to hard disk.\nPress button to continue."));#endif pqapplication->unlock(); } else mutex.unlock(); mutex.lock(); if(simulation_write_results){ simulation_write_results=false; pqapplication->lock(); SpaceChromosome &c=gt.spacePopulation.bestChromosome(gt.rules); getRoomsTimetable(c); //update the string representing the conflicts spaceConflictsString = ""; spaceConflictsString += "COMPULSORY CONSTRAINTS CONFLICTS (more important):\n"; c.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours, &spaceConflictsString); spaceConflictsString += "\n--------------------------\n\n"; spaceConflictsString += "NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n"; c.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours, &spaceConflictsString); writeSimulationResults(c); pqapplication->unlock(); mutex.unlock(); } else mutex.unlock(); } //Print ending time time( <ime ); logg<<endl<<"Current time is: "<<ctime( <ime )<<endl; logg.close(); SpaceChromosome &c=gt.spacePopulation.bestChromosome(gt.rules); mutex.lock(); //needed because of the updateStudentsTimetable pqapplication->lock(); getRoomsTimetable(c); //update the string representing the conflicts spaceConflictsString = ""; spaceConflictsString += "COMPULSORY CONSTRAINTS CONFLICTS (more important):\n"; c.hardFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours, &spaceConflictsString); spaceConflictsString += "\n--------------------------\n\n"; spaceConflictsString += "NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n"; c.softFitness(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours, &spaceConflictsString); writeSimulationResults(c); pqapplication->unlock(); mutex.unlock(); simulation_running=false;}void TimetableAllocateRoomsForm::writeSimulationResults(SpaceChromosome &c){ if(&c!=NULL) ; QDir dir; //make sure that the input directory exists - only for GNU/Linux //For Windows, I make a "fet.ini" in the current working directory#ifndef WIN32 if(!dir.exists(QDir::homeDirPath()+"/.fet")) dir.mkdir(QDir::homeDirPath()+"/.fet");#endif //make sure that the output directory exists if(!dir.exists(OUTPUT_DIR)) dir.mkdir(OUTPUT_DIR); assert(gt.rules.initialized && gt.rules.internalStructureComputed); assert(gt.spacePopulation.initialized); assert(students_schedule_ready && teachers_schedule_ready); assert(rooms_schedule_ready); QString s; QString s2=INPUT_FILENAME_XML.right(INPUT_FILENAME_XML.length()-INPUT_FILENAME_XML.findRev(FILE_SEP)-1); //write the space conflicts - in txt mode s=OUTPUT_DIR+FILE_SEP+s2+"_"+SPACE_CONFLICTS_FILENAME; QFile file(s); if(!file.open(IO_WriteOnly)) assert(0); QTextStream tos(&file); tos<<spaceConflictsString<<endl; file.close(); //now write the solution in xml and html //students - html s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_WITH_ROOMS_DAYS_HORIZONTAL_FILENAME_HTML; writeStudentsTimetableWithRoomsDaysHorizontalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_WITH_ROOMS_DAYS_VERTICAL_FILENAME_HTML; writeStudentsTimetableWithRoomsDaysVerticalHtml(s); //teachers - html s=OUTPUT_DIR+FILE_SEP+s2+"_"+TEACHERS_TIMETABLE_WITH_ROOMS_1_DAYS_HORIZONTAL_FILENAME_HTML; writeTeachersTimetableWithRooms1DaysHorizontalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+TEACHERS_TIMETABLE_WITH_ROOMS_1_DAYS_VERTICAL_FILENAME_HTML; writeTeachersTimetableWithRooms1DaysVerticalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+TEACHERS_TIMETABLE_WITH_ROOMS_2_FILENAME_HTML; writeTeachersTimetableWithRooms2Html(s); //rooms - xml and html s=OUTPUT_DIR+FILE_SEP+s2+"_"+ROOMS_TIMETABLE_FILENAME_XML; writeRoomsTimetableXml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+ROOMS_TIMETABLE_DAYS_HORIZONTAL_FILENAME_HTML; writeRoomsTimetableDaysHorizontalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+ROOMS_TIMETABLE_DAYS_VERTICAL_FILENAME_HTML; writeRoomsTimetableDaysVerticalHtml(s); cout<<"Writing simulation results to disk completed successfully"<<endl;}void TimetableAllocateRoomsForm::getRoomsTimetable(SpaceChromosome &c){ assert(gt.spacePopulation.initialized); assert(gt.rules.initialized && gt.rules.internalStructureComputed); //assert(c.HFitness()==0); - for perfect solutions c.getRoomsTimetable(gt.rules, gt.spacePopulation.days, gt.spacePopulation.hours, rooms_timetable_week1, rooms_timetable_week2); best_space_chromosome.copy(gt.rules, c); rooms_schedule_ready=true;}/**Function writing the rooms' timetable in xml format to a file*/void TimetableAllocateRoomsForm::writeRoomsTimetableXml(const QString& xmlfilename){ assert(gt.rules.initialized && gt.rules.internalStructureComputed); assert(gt.spacePopulation.initialized); assert(students_schedule_ready && teachers_schedule_ready);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -