⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timetableallocatehoursform.cpp

📁 基于遗传算法的排课软件源码 根据需要安排合理的课程时间等
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/***************************************************************************                          timetableallocatehoursform.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 "timetableallocatehoursform.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 TimeSolvingThread timeSolvingThread;extern QApplication *pqapplication;//Represents the current status of the 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 TimeChromosome best_time_chromosome;extern QString timeConflictsString;TimetableAllocateHoursForm::TimetableAllocateHoursForm(){	simulation_running=false;}TimetableAllocateHoursForm::~TimetableAllocateHoursForm(){}TimeSolvingThread::TimeSolvingThread(){	callingForm=NULL;}void TimeSolvingThread::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 TimetableAllocateHoursForm::generationLogging(int generation){	assert(gt.rules.initialized && gt.rules.internalStructureComputed);	assert(gt.timePopulation.initialized);	TimeChromosome& c1=gt.timePopulation.bestChromosome(gt.rules);	if(SIMULATION_LOGGING==true){		TimeChromosome& c2=gt.timePopulation.worstChromosome(gt.rules);		TimeChromosome& c3=gt.timePopulation.medianChromosome(gt.rules);		double th=gt.timePopulation.totalHardFitness(gt.rules);		double ts=gt.timePopulation.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.timePopulation.n;		logg<<", Medium SoftFitness="<<ts/gt.timePopulation.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.timePopulation.n;		cout<<", Medium SoftFitness="<<ts/gt.timePopulation.n;		cout<<endl<<flush;	}	//write to the Qt interface	QString s;	s = QObject::tr("Population number:"); s+=QString::number(gt.timePopulation.n); s+="\n";	s += QObject::tr("Generation:"); s+=QString::number(generation+1)+"\n";	s+=QObject::tr("Compulsory constraints conflicts:"); s+=QString::number(c1.hardFitness(gt.rules))+"\n";	s+=QObject::tr("Non-compulsory constraints conflicts:"); s+=QString::number(c1.softFitness(gt.rules));	pqapplication->lock();	currentResultsTextEdit->setText(s);	currentResultsTextEdit->repaint();	pqapplication->unlock();}void TimetableAllocateHoursForm::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( &ltime );	//open log file	QString s=OUTPUT_DIR;	s+=FILE_SEP;	s+=TIME_LOG_FILENAME_TXT;	logg.open((const char*)s);	logg<<"Population number="<<gt.timePopulation.populationNumber()<<endl;	logg<<"Current time is: "<<ctime( &ltime )<<endl<<endl;	for(int i=0; i<max_generations; i++){		if(evolution_method==1){			gt.timePopulation.evolve1(gt.rules);		}		else if(evolution_method==2){			gt.timePopulation.evolve2(gt.rules);		}		else{			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.timePopulation.write(gt.rules, OUTPUT_DIR+FILE_SEP+s+"_time_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();			TimeChromosome &c=gt.timePopulation.bestChromosome(gt.rules);			getStudentsTimetable(c);			getTeachersTimetable(c);			//update the string representing the conflicts			timeConflictsString = "";			timeConflictsString += QObject::tr("COMPULSORY CONSTRAINTS CONFLICTS (more important):\n");			c.hardFitness(gt.rules, &timeConflictsString);			timeConflictsString += "\n--------------------------\n\n";			timeConflictsString += QObject::tr("NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n");			c.softFitness(gt.rules, &timeConflictsString);			writeSimulationResults(c);			pqapplication->unlock();			mutex.unlock();		}		else			mutex.unlock();	}	//Print ending time	time( &ltime );	logg<<endl<<"Current time is: "<<ctime( &ltime )<<endl;	logg.close();	TimeChromosome &c=gt.timePopulation.bestChromosome(gt.rules);	mutex.lock(); //needed because of the updateStudentsTimetable	pqapplication->lock();	getStudentsTimetable(c);	getTeachersTimetable(c);	//update the string representing the conflicts	timeConflictsString = "";	timeConflictsString += QObject::tr("COMPULSORY CONSTRAINTS CONFLICTS (more important):\n");	c.hardFitness(gt.rules, &timeConflictsString);	timeConflictsString += "\n--------------------------\n\n";	timeConflictsString += QObject::tr("NON-COMPULSORY CONSTRAINTS CONFLICTS (less important):\n");	c.softFitness(gt.rules, &timeConflictsString);	writeSimulationResults(c);	pqapplication->unlock();	mutex.unlock();	simulation_running=false;}void TimetableAllocateHoursForm::writeSimulationResults(TimeChromosome &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.timePopulation.initialized);	assert(students_schedule_ready && teachers_schedule_ready);	QString s;		QString s2=INPUT_FILENAME_XML.right(INPUT_FILENAME_XML.length()-INPUT_FILENAME_XML.findRev(FILE_SEP)-1);	//write the time conflicts - in txt mode	s=OUTPUT_DIR+FILE_SEP+s2+"_"+TIME_CONFLICTS_FILENAME;	QFile file(s);	if(!file.open(IO_WriteOnly))		assert(0);	QTextStream tos(&file);	tos<<timeConflictsString<<endl;		file.close();	//now write the solution in xml files	//students	s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_FILENAME_XML;	writeStudentsTimetableXml(s);	//teachers	s=OUTPUT_DIR+FILE_SEP+s2+"_"+TEACHERS_TIMETABLE_FILENAME_XML;	writeTeachersTimetableXml(s);	//now write the solution in html files	//students	s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_1_DAYS_HORIZONTAL_FILENAME_HTML;	writeStudentsTimetable1DaysHorizontalHtml(s);	s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_1_DAYS_VERTICAL_FILENAME_HTML;	writeStudentsTimetable1DaysVerticalHtml(s);	s=OUTPUT_DIR+FILE_SEP+s2+"_"+STUDENTS_TIMETABLE_2_FILENAME_HTML;	writeStudentsTimetable2Html(s);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -