現在、ブール値を使用して方向を出力しようとしています。
1 は真です。0 は偽です。
1 はエレベーターが上昇していることを示します。0 はエレベーターが下降することを示します。
出力を次のようにしたい:
ノード番号: タイムスタンプ。現在(ユーザーフロア)。目的地(ユーザーフロア)。方向(ユーザーが向いている)。
Node 0 : 1 3 7 true
Node 1 : 1 2 9 true
Node 2 : 1 7 9 true
Node 3 : 2 4 6 true
Node 4 : 2 4 8 true
Node 5 : 2 1 17 true
Node 6 : 5 1 15 true
Node 7 : 5 5 1 false
Node 8 : 6 17 4 false
Node 9 : 6 4 17 true
代わりに、出力として取得しています:
Node 0 : 1 3 7 205
Node 1 : 1 2 9 205
Node 2 : 1 7 9 205
Node 3 : 2 4 6 205
Node 4 : 2 4 8 205
Node 5 : 2 1 17 205
Node 6 : 5 1 15 205
Node 7 : 5 5 1 205
Node 8 : 6 17 4 205
Node 9 : 6 4 17 205
コード自体はエラーなしでコンパイルされます。私の論理がどこで間違っていたのか、私にはわかりません。ユーザーがエレベーターにサービスを提供するように要求する方向を割り当てる方法を見つけようとしていたとき(ユーザーが行う要求)。
reqNode *temp = new reqNode;
if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);
私が使用したロジックでした。
使用される T1.txt ファイル:
1 3 7
1 2 9
1 7 9
2 4 6
2 4 8
2 1 17
5 1 15
5 5 1
6 17 4
6 4 17
これが私のファイルです(コード全体)
binaryHeap.h ファイル:
#include <vector>
using namespace std;
template <class Comparable>
class BinaryHeap
{
public:
BinaryHeap( ): Array( 11 ), theSize( 0 ){}
bool isEmpty( ) const
{
return theSize == 0;
}
const Comparable & findMin( ) const
{
if( isEmpty( ) )
{
cout << "heap empty" << endl; //throw UnderflowException( );
//break;
}
return Array[ 1 ];
}
void insert( const Comparable & x)
{
Array[ 0 ] = x; // initialize sentinel
if( theSize + 1 == Array.size( ) )
Array.resize( Array.size( ) * 2 + 1 );
// Percolate up
int hole = ++theSize;
for( ; x < Array[ hole / 2 ]; hole /= 2 )
Array[ hole ] = Array[ hole / 2 ];
Array[ hole ] = x;
}
void deleteMin( )
{
if( isEmpty( ) )
{
cout << "heap empty" << endl; //throw UnderflowException( );
break;
}
Array[ 1 ] = Array[ theSize-- ];
percolateDown( 1 );
}
void deleteMin( Comparable & minItem )
{
minItem = findMin( );
Array[ 1 ] = Array[ theSize-- ];
percolateDown( 1 );
}
void makeEmpty( )
{
theSize = 0;
}
private:
int theSize; // Number of elements in heap
vector<Comparable> Array; // The heap Array
void buildHeap( )
{
for( int i = theSize / 2; i > 0; i-- )
percolateDown( i );
}
void percolateDown( int hole )
{
int child;
Comparable tmp = Array[ hole ];
for( ; hole * 2 <= theSize; hole = child )
{
child = hole * 2;
if( child != theSize && Array[ child + 1 ] < Array[ child ] )
child++;
if( Array[ child ] < tmp )
Array[ hole ] = Array[ child ];
else
break;
}
Array[ hole ] = tmp;
}
};
reqnode.h ファイル:
class reqNode//create a node that takes in several properties.
{
public:
reqNode()
:direction(true)
{
//default constructor
//initialize the memeber variables of this class.
priority = start = destination
= timestamp = start_time
= finish_time = -1;
set_dir(true);
}
reqNode(const reqNode ©){ //copy constructor
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;
}
reqNode & operator=(const reqNode & copy){ // operation overload
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;
return *this;
}
bool operator<(const reqNode &rhs) const// operation overload
{
if(this->priority < rhs.priority)
return true;
else
return false;
}
void setPriority(int x){//assigning the priority to of whatever I declare "x" to be.
priority = x;
}
void calculate_priority()
{
if( start < destination )
priority = destination; // Requests for higher floors will have lower priorities due to min heap structure
else
priority = 20 - destination; // Requests for higher floors will have higher priorities
}
void set_dir(bool dir)
{
direction = dir;
}
//declare the member variables.
int priority, start, destination,
timestamp, start_time, finish_time;
bool direction;
};
Elevator2.cpp ファイル:
#include <iostream>
#include <fstream>
#include <stdio.h> //allows for the usage of getchar
#include "Elevator2.h"
void Elevator::displayMessage()
{
cout <<"Welcome to University Towers " << endl << endl << endl<< "----------------------------" << endl;
}
double Elevator::get_total_cost()
{
return total_cost;
}
double Elevator::get_total_wait_time()
{
return total_wait_time;
}
bool Elevator::get_direction()
{
return direction;
}
void Elevator::change_direction()
{
direction = !direction;
}
void Elevator::set_direction(bool new_direction)
{
direction = new_direction;
}
int Elevator::get_elevator_floor()
{
return elevator_floor;
}
void Elevator::increment_elevator_floor()
{
if(direction) //if direction is TRUE (elevator moving upward) increment
elevator_floor++;
else
elevator_floor--;
}
void Elevator::set_elevator_floor(int new_floor)
{
elevator_floor = new_floor;
}
void Elevator::readRequests()
{
ifstream myStream("T1.txt");
while(!myStream.eof())
{
int timestamp ,currentFloor, destinationFloor;
myStream >> timestamp >> currentFloor >> destinationFloor;
//cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " << currentFloor
// << " and the destination floor is " << destinationFloor << endl << endl;
//cout<< endl;
reqNode *temp = new reqNode;
//initialize request node object
temp->timestamp = timestamp;
temp->start = currentFloor;
temp->destination = destinationFloor;
temp->start_time = -1;
temp->finish_time = -1;
//temp->direction = ( (currentFloor < destinationFloor) ? 1 : 0 );
temp->calculate_priority();
if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);
request.push(*temp);//push nodes into the request bank
}
int i = 0;
while( !request.empty() )
{
cout << "Node " << i << " : " << request.front().timestamp << " " << request.front().start << " " << request.front().destination
<< " " << request.front().direction << endl;
//printf_s("%d\n", request.front().direction);
request.pop();//popping the request in order to test
i++;
}
//bool test = false;
//cout << test;
}
Elevator2.h ファイル:
#include <iostream>
#include "binaryHeap.h"
#include "reqnode.h"
#include <queue>
using namespace std;
class Elevator
{
public:
//Elevator();//Default constructor
Elevator(int initial_floor)//Each object starts with a initial floor aka the current floor.
:total_wait_time(0), total_cost(0), elevator_floor(initial_floor), direction(true)//initialize each object accordingly.
{
readRequests();//for each elevator object...created(i.e - request read, there is a node created).
}
void displayMessage();//display greeting text.
double get_total_cost();//returns the number of floors the elevator travels.
double get_total_wait_time();//returns the number of floors the elevator travels to get to user.
bool get_direction(); // get the current direction of the elevator.
void set_direction(bool); // sets direction to desired direction of the user.
void change_direction(); // reverses direction of the elevator.
int get_elevator_floor();// return the elevator floor(currently on).
void set_elevator_floor(int); // sets according to the user's request.
void increment_elevator_floor(); // moves elevator one floor determined by direction(dependent on the set direction bool).
protected://only calls from inherited/friendship classes are aloud. Never calls from the Driver.
queue<reqNode> waiting_queue;//queue stores reqNode objects, we name this queue - waiting queue.
queue<reqNode> request;// "" "" - request.
BinaryHeap<reqNode> service_queue;// "" "" - service queue.
queue<reqNode> finished;//"" "" - finished.
private:
double total_wait_time;//number of floors the elevator travels to get to user.
double total_cost;//number of floors the elevator travels while servicing.
int elevator_floor;//initial floor value.
bool direction;//iterating through the floors for the sabbath algorithm
void readRequests();//read the in the requests per user and store them(by way of the request queue/bank).
};
lazy.cpp ファイル:
#include "lazy.h"
void Lazy::setAttributes(reqNode Nodey)
{
int costToserve = abs(Nodey.start - Nodey.destination);//the floors between user's start_floor and user's destination_floor.
int directionOf = (Nodey.start - Nodey.destination) * -1;//the direction the elevator is heading.
int priority = abs(Nodey.destination - get_elevator_floor());//already in the servicing queue.
}
void Lazy::simulation()
{
time = 0;
while( !request.empty() ) // while request remain, do:
{
// check for and read new requests into incoming/waiting queue
while(request.front().timestamp == time) // schedule new requests
{
waiting_queue.push(request.front()); // places next request into waiting queue
request.pop();
}
scheduler(); // inputs waiting requests into servicing queue based on elevator's current state
if( !service_queue.isEmpty() ) // if( !service_queue.isEmpty() )
{
if( service_queue.findMin().direction != get_direction() ) // if(top node of service queue's direction == current direction of the elevator)
{
change_direction();//then we change the direction.
}// else set direction to top node's direction
increment_elevator_floor(); // increments/decrements elevator floor by one.
while( service_queue.findMin().destination == get_elevator_floor() )//check top node to see if it is done; current floor is destination
{
reqNode * temp;
service_queue.deleteMin(*temp); // min item is serviced; pop from heap
temp->finish_time = time;
finished.push(*temp); // place serviced node to finished bank
}
}
increment_elevator_floor();
time++;
}
}
void Lazy::scheduler()
{
// UPDATE: PRIORITY TO BE CALCULATED BY REFERENCE POINT (DETERMINED BY DIRECTION) IN DRIVER BY CALL TO REQNODE METHOD
int size = waiting_queue.size(); //set to wait queue's size
if( !service_queue.isEmpty() )
{
for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() ); //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}
}
else
{
if( !waiting_queue.empty() )//check waiting queue (if not empty)
{
/* Elevator should take the direction of next request waiting and begin moving in that direction.
Elevator should pick up requests that are on the way and in same direction as next waiting request
*/
set_direction(waiting_queue.front().direction);
for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() ); //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}
}
}
}
lazy.h ファイル:
#include "Elevator2.h"
class Lazy : public Elevator
{
public:
Lazy(int initial_floor)//initialize the same way you did your base class. same parameters.
:Elevator(initial_floor){}
void simulation();//carries out the normal elevator simulation
private:
void scheduler();//scheduler to handle.
void setAttributes(reqNode);//set attributes of each request node.
int time;
};
これですべてのファイルです。うわー、スペースバーのキーが 4 倍のスペースから壊れていると思います! もう。
最後に driver2.cpp: (すべての魔法が起こる場所)
#include <iostream>
#include <fstream>
#include <string>
#include "lazy.h"
#include <algorithm>
using namespace std;
void setNode(reqNode nizzode, int priority)
{
nizzode.priority = priority;
}
int main()
{
Lazy lazy(1);
}
それでおしまい。
ここで、私のコードは T1.txt ファイルから 10 個のユーザー リクエストを取り込み、属性 Timestamp (リクエストされたフロア) と Current floor (リクエストを行ったユーザーの元のフロア) を持つリクエスト ノードを生成します。 、そして最後に行き先階(ユーザーの希望する行き先)。また、現在のフロアと目的のフロアに基づいてユーザーの方向を決定するロジックがダウンしていると思いました。少年は私が間違っていた。助けていただければ幸いです。