【发布时间】:2014-05-13 01:45:18
【问题描述】:
我有一个项目,它创建一个时间表并对其进行优化。该项目有 Schedule 类:
Schedule.h:
#pragma once
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Course.h"
#include "Prof.h"
#include "Room.h"
#include "TimeBlock.h"
#include "Weekdays.h"
#include "ScoreCalculator.h"
#include <map>
#include <vector>
/**
* This class represents a schedule for all courses.
*/
class Schedule {
public:
Schedule(std::vector < Room >, std::vector < Prof >, std::vector < Course > );
Schedule(const Schedule& schedule);
std::map < Room, std::vector < std::vector < Course > > > getSchedule();
bool setCourse(const Course&, const Room&, Weekdays, TimeBlock, int);
Course getCourse(const Room&, Weekdays, TimeBlock);
std::vector < Course > getCoursesAt(Weekdays, TimeBlock);
std::vector < Weekdays > getWeekdaysFor(const Course&);
TimeBlock getTimeFor(const Course&);
Room getRoomFor(const Course&);
void swapCourses(Room, Weekdays, TimeBlock, Room, Weekdays, TimeBlock);
void setScore (double score){ _score = score; }
double getScore() { return _score; }
Prof getProf(string);
Prof getProf(Course);
std::vector<Course> getCoursesTaughtBy(Prof&);
std::vector<Course> getCoursesOnGivenDayTaughtBy(Prof&, Weekdays);
std::vector<Room> getRooms();
std::map<string, Prof> getProfs();
void setSchedule(std::map < Room, std::vector < std::vector <Course> > >);
private:
std::map < Room, std::vector < std::vector < Course > > > _schedule;
std::map < std::string, Prof > _professors;
std::map < std::string, Course > _courses;
std::vector<Room> _rooms;
std::vector<Prof> _profs;
std::vector<Course> _coursesVector;
double _score;
};
#endif // SCHEDULE_H
Schedule.cpp:
#include "../include/Schedule.h"
#include "../include/Prof.h"
#include <vector>
#include <set>
#include <string>
using namespace std;
/**
* Constructor for the schedule. Creates the empty schedule
*/
Schedule::Schedule(vector < Room > rooms, vector < Prof > prof, vector < Course > courses){
_rooms = rooms;
_profs = prof;
_coursesVector = courses;
for(unsigned int i = 0; i < prof.size(); i++){
_professors.insert(make_pair(prof.at(i).getId(), prof.at(i)));
}
for (unsigned int i = 0; i < courses.size(); i++){
_courses.insert(make_pair(courses.at(i).getId(), courses.at(i)));
}
_score = -1;
for (unsigned int i = 0; i < _rooms.size(); i++){
vector < vector < Course > > dayVector;
dayVector.resize(WEEKDAYS_SIZE);
for (unsigned int j = 0; j < dayVector.size(); j++){
dayVector.at(j).resize(TIMEBLOCK_SIZE);
}
_schedule.insert(make_pair(_rooms.at(i), dayVector));
}
}
bool Schedule::setCourse(const Course& course, const Room& room, Weekdays firstMeeting, TimeBlock timeBlock, int numberOfMeetings){
if (course.getEnrolled() > room.getCapacity()){
return false;
}
_schedule.at(room).at(firstMeeting).at(timeBlock) = course;
switch (numberOfMeetings){
case 2:
_schedule.at(room).at((TimeBlock)(firstMeeting + 2)).at(timeBlock) = course;
break;
}
return true;
}
Course Schedule::getCourse(const Room& room, Weekdays day, TimeBlock timeBlock){
//returns course
}
vector < Course > Schedule::getCoursesAt(Weekdays day, TimeBlock timeBlock){
//returns course at the given time
}
TimeBlock Schedule::getTimeFor(const Course& course){
//returns time for the course
}
Room Schedule::getRoomFor(const Course& course){
//returns room for the course
}
vector < Weekdays > Schedule::getWeekdaysFor(const Course& course){
//returns weekdays for the course
}
// returns true if swap was successful, false otherwise
void Schedule::swapCourses(Room room1, Weekdays weekdays1, TimeBlock timeBlock1, Room room2, Weekdays weekdays2, TimeBlock timeBlock2){
Course c1, c2;
c1 = getCourse(room1, weekdays1, timeBlock1);
c2 = getCourse(room2, weekdays2, timeBlock2);
setCourse(c2, room1, weekdays1, timeBlock1, 2);
setCourse(c1, room2, weekdays2, timeBlock2, 2);
}
Prof Schedule::getProf(string id){
return _professors.at(id);
}
Prof Schedule::getProf(Course c){
return _professors.at(c.getProfId());
}
void Schedule::setSchedule(map < Room, vector < vector < Course > > > schedule){
_schedule = schedule;
}
map < Room, vector < vector < Course > > > Schedule::getSchedule(){
return _schedule;
}
vector<Room> Schedule::getRooms(){
return _rooms;
}
map <string, Prof> Schedule::getProfs(){
return _professors;
}
vector <Course> Schedule::getCoursesTaughtBy(Prof& prof){
//returns courses taught by
}
Schedule& Schedule::operator=(const Schedule& rhs){
_rooms = rhs._rooms;
_courses = rhs._courses;
_coursesVector = rhs._coursesVector;
_professors = rhs._professors;
_profs = rhs._profs;
_schedule = rhs._schedule;
return *this;
}
它有一个类通过交换两个随机课程并决定哪个时间表更好来处理优化:
GeneticScheduleGenerator.h:
#pragma once
#ifndef GENETICSCHEDULEGENERATOR_H
#define GENETICSCHEDULEGENERATOR_H
#include "ScheduleGenerator.h"
#include "ScoreCalculator.h"
#include "Schedule.h"
#include <map>
#include <string>
using namespace std;
class GeneticScheduleGenerator : public ScheduleGenerator {
public:
GeneticScheduleGenerator(ScoreCalculator&, Schedule*, long);
Schedule* getSchedule(void);
Schedule* _schedule;
map < Room, vector < vector < Course > > > getScheduleMap();
private:
double calculateScore(map < string, Prof >, Schedule*);
void optimize ();
std::map<string, ProfInfo> profInfoMap;
ScoreCalculator& _sc;
map<string, double> _scores;
map<Room, vector< vector< Course> > > _scheduleMap;
};
#endif // GENETICSCHEDULEGENERATOR_H
GeneticScheduleGenerator.cpp:
#include "../include/GeneticScheduleGenerator.h"
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include "../include/Weekdays.h"
#include "../include/TimeBlock.h"
#include <iostream>
GeneticScheduleGenerator::GeneticScheduleGenerator(ScoreCalculator& sc, Schedule* schedule, long timeout )
: ScheduleGenerator(timeout) , _sc(sc) {
_schedule = schedule;
}
double GeneticScheduleGenerator::calculateScore(map<string, Prof> professors, Schedule* schedule){
double score = 0;
//does some calculations
return score;
}
Schedule* GeneticScheduleGenerator::getSchedule(){
Schedule* bestSchedule = _schedule;
Schedule _changedSc = *_schedule;
Schedule *_changedSchedule = &_changedSc;
map<string, Prof> professors = bestSchedule->getProfs();
bestSchedule->setScore(calculateScore(professors, bestSchedule));
srand(time(NULL));
vector<Room> rooms = _schedule->getRooms();
int numberOfRooms = rooms.size();
Room room1, room2;
Weekdays day1, day2;
TimeBlock time1, time2;
long endTime = time(0) + getTimeout();
int counter = 0;
do{
counter++;
room1 = rooms.at(rand() % numberOfRooms);
room2 = rooms.at(rand() % numberOfRooms);
day1 = (Weekdays)(rand() % WED);
day2 = (Weekdays)(rand() % WED);
time1 = (TimeBlock)(rand() % TIMEBLOCK_SIZE);
time2 = (TimeBlock)(rand() % TIMEBLOCK_SIZE);
if (_changedSchedule->getCourse(room1, day1, time1).getEnrolled() > room2.getCapacity() ||
_changedSchedule->getCourse(room2, day2, time2).getEnrolled() > room1.getCapacity() ||
_changedSchedule->getCourse(room2, day2, time2) == _changedSchedule->getCourse(room1, day1, time1))
{
continue;
}
else
{
bestSchedule = _schedule;
_changedSchedule->swapCourses(room1, day1, time1, room2, day2, time2);
double newScore = calculateScore(professors, _changedSchedule);
if (bestSchedule->getScore() > newScore){
_schedule = _changedSchedule;
_schedule->setScore(newScore);
}
}
} while (time(0) < endTime);
std::cout << "counter: " << counter << endl;
_scheduleMap = _schedule->getSchedule();
/*map<string, Prof> tempProf = _schedule->getProfs();
map<string, Course> tempCorse = _schedule->getCourses();*/
return _schedule;
}
map < Room, vector < vector < Course > > > GeneticScheduleGenerator::getScheduleMap(){
return _scheduleMap;
}
ScheduleGeneratorClass 尚未完全正常运行(它不会测试课程冲突,即同一位教授不能同时教授 2 个课程),但它会交换课程并计算课程表的分数。问题是,在返回 _schedule 之前; part _schedule 包含有效的时间表:
但是当调用者得到它时,该对象只保存分数部分。其他所有内容似乎都被删除了:
过去几天我一直在尝试解决这个问题,但没有运气。 任何人都可以帮忙吗? 如果您需要其他代码或更多信息,请询问。 谢谢。
【问题讨论】:
-
代码太多,需要做个小例子!
-
TMC;DC。你能把你的代码缩小一点吗?
-
代码太多又不够用,你的类型是怎么用的?特别是,
GeneticScheduleGenerator的第二个参数是什么? -
原来的时间表
-
停止使用指针。大量代码中的问题恰好出在您停止使用漂亮的对象和引用并开始使用指针的地方,这绝非巧合!
标签: c++ function pointers reference