📄 timetableallocatehoursroomsform.cpp
字号:
/*************************************************************************** timetableallocatehoursroomsform.cpp - description ------------------- begin : Feb 14, 2005 copyright : (C) 2005 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 "timetableallocatehoursroomsform.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 TimeSpaceSolvingThread timeSpaceSolvingThread;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_ready2;extern bool teachers_schedule_ready2;extern bool rooms_schedule_ready2;extern TimeSpaceChromosome best_time_space_chromosome;extern QString timeSpaceConflictsString;TimetableAllocateHoursRoomsForm::TimetableAllocateHoursRoomsForm(){ simulation_running=false;}TimetableAllocateHoursRoomsForm::~TimetableAllocateHoursRoomsForm(){}TimeSpaceSolvingThread::TimeSpaceSolvingThread(){ callingForm=NULL;}void TimeSpaceSolvingThread::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 TimetableAllocateHoursRoomsForm::generationLogging(int generation){ assert(gt.rules.initialized && gt.rules.internalStructureComputed); assert(gt.timeSpacePopulation.initialized); TimeSpaceChromosome& c1=gt.timeSpacePopulation.bestChromosome(gt.rules); if(SIMULATION_LOGGING==true){ TimeSpaceChromosome& c2=gt.timeSpacePopulation.worstChromosome(gt.rules); TimeSpaceChromosome& c3=gt.timeSpacePopulation.medianChromosome(gt.rules); double th=gt.timeSpacePopulation.totalHardFitness(gt.rules); double ts=gt.timeSpacePopulation.totalSoftFitness(gt.rules); //write to log file logg<<"Generation number "<<generation+1<<endl; logg<<" Best chromosome:"; logg<<" HardFitness="<<c1.hardFitness(gt.rules); logg<<", SoftFitness="<<c1.softFitness(gt.rules)<<endl; logg<<" Median chromosome:"; logg<<" HardFitness="<<c3.hardFitness(gt.rules); logg<<", SoftFitness="<<c3.softFitness(gt.rules)<<endl; logg<<" Worst chromosome:"; logg<<" HardFitness="<<c2.hardFitness(gt.rules); logg<<", SoftFitness="<<c2.softFitness(gt.rules)<<endl; logg<<" Medium HardFitness="<<th/gt.timeSpacePopulation.n; logg<<", Medium SoftFitness="<<ts/gt.timeSpacePopulation.n; logg<<endl<<flush; //write to display cout<<"Generation number "<<generation+1<<endl; cout<<" Best chromosome:"; cout<<" HardFitness="<<c1.hardFitness(gt.rules); cout<<", SoftFitness="<<c1.softFitness(gt.rules)<<endl; cout<<" Median chromosome:"; cout<<" HardFitness="<<c3.hardFitness(gt.rules); cout<<", SoftFitness="<<c3.softFitness(gt.rules)<<endl; cout<<" Worst chromosome:"; cout<<" HardFitness="<<c2.hardFitness(gt.rules); cout<<", SoftFitness="<<c2.softFitness(gt.rules)<<endl; cout<<" Medium HardFitness="<<th/gt.timeSpacePopulation.n; cout<<", Medium SoftFitness="<<ts/gt.timeSpacePopulation.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))+"\n"; s+="Non-compulsory constraints conflicts:"; s+=QString::number(c1.softFitness(gt.rules)); pqapplication->lock(); currentResultsTextEdit->setText(s); currentResultsTextEdit->repaint(); pqapplication->unlock();}void TimetableAllocateHoursRoomsForm::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+=TIME_SPACE_LOG_FILENAME_TXT; logg.open((const char*)s); logg<<"Population number="<<gt.timeSpacePopulation.populationNumber()<<endl; logg<<"Current time is: "<<ctime( <ime )<<endl<<endl; for(int i=0; i<max_generations; i++){ if(evolution_method==1){ gt.timeSpacePopulation.evolve1(gt.rules); } else if(evolution_method==2){ gt.timeSpacePopulation.evolve2(gt.rules); } else{ QMessageBox::critical(this, QObject::tr("FET information"), QObject::tr("error in timetableallocatehoursroomsform.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.timeSpacePopulation.write(gt.rules, OUTPUT_DIR+FILE_SEP+s+"_time_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(); TimeSpaceChromosome &c=gt.timeSpacePopulation.bestChromosome(gt.rules); getTeachersTimetable(c); getStudentsTimetable(c); getRoomsTimetable(c); //update the string representing the conflicts timeSpaceConflictsString = ""; timeSpaceConflictsString += "COMPULSORY CONSTRAINTS CONFLICTS (more important):\n"; c.hardFitness(gt.rules, &timeSpaceConflictsString); timeSpaceConflictsString += "\n--------------------------\n\n"; timeSpaceConflictsString += "NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n"; c.softFitness(gt.rules, &timeSpaceConflictsString); writeSimulationResults(c); pqapplication->unlock(); mutex.unlock(); } else mutex.unlock(); } //Print ending time time( <ime ); logg<<endl<<"Current time is: "<<ctime( <ime )<<endl; logg.close(); TimeSpaceChromosome &c=gt.timeSpacePopulation.bestChromosome(gt.rules); mutex.lock(); //needed because of the updateStudentsTimetable pqapplication->lock(); getTeachersTimetable(c); getStudentsTimetable(c); getRoomsTimetable(c); //update the string representing the conflicts timeSpaceConflictsString = ""; timeSpaceConflictsString += "COMPULSORY CONSTRAINTS CONFLICTS (more important):\n"; c.hardFitness(gt.rules, &timeSpaceConflictsString); timeSpaceConflictsString += "\n--------------------------\n\n"; timeSpaceConflictsString += "NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n"; c.softFitness(gt.rules, &timeSpaceConflictsString); writeSimulationResults(c); pqapplication->unlock(); mutex.unlock(); simulation_running=false;}void TimetableAllocateHoursRoomsForm::writeSimulationResults(TimeSpaceChromosome &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.timeSpacePopulation.initialized); assert(students_schedule_ready2); assert(teachers_schedule_ready2); assert(rooms_schedule_ready2); 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+"_"+TIME_SPACE_CONFLICTS_FILENAME; QFile file(s); if(!file.open(IO_WriteOnly)) assert(0); QTextStream tos(&file); tos<<timeSpaceConflictsString<<endl; file.close(); //now write the solution in xml and html //students - xml s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_FILENAME_XML; writeStudentsTimetableXml(s); //teachers - xml s=OUTPUT_DIR+FILE_SEP+s2+"_"+TEACHERS_TIMETABLE_FILENAME_XML; writeTeachersTimetableXml(s); //students - html s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_WITH_ROOMS_1_DAYS_HORIZONTAL_FILENAME_HTML; writeStudentsTimetableWithRooms1DaysHorizontalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_WITH_ROOMS_1_DAYS_VERTICAL_FILENAME_HTML; writeStudentsTimetableWithRooms1DaysVerticalHtml(s); s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_WITH_ROOMS_2_FILENAME_HTML; writeStudentsTimetableWithRooms2Html(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 s=OUTPUT_DIR+FILE_SEP+s2+"_"+ROOMS_TIMETABLE_FILENAME_XML; writeRoomsTimetableXml(s); //rooms - html
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -