Project

General

Profile

Revision 2003

Added the helper classes for the warehouse code that we are also going to have to port over. Things are looking grim boys

View differences:

trunk/code/projects/warehouse/WH_Robot.h
5 5
#define TASK_COMPLETED 0
6 6
#define TASK_FAILED -1
7 7

  
8
#include "../Behavior.h"
9 8
#include "navigationMap.h"
10 9
#include "Scheduler.h" 
11
#include "../helper_classes/Order.h"
10
#include "/helper_classes/Order.h"
12 11
#include <assert.h>
13 12
#include <stdlib.h>
14 13

  
15
class WH_Robot : Behavior{
16 14

  
17
        Order* curr_task;
18
        navigationMap nav_map;
19
        Scheduler scheduler;
15
Order* curr_task;
16
navigationMap nav_map;
17
Scheduler scheduler;
20 18

  
21
        Time get_wc_time(State dest);
22
        int exec_task();
19
Time get_wc_time(State dest);
20
int exec_task();
23 21

  
24
    public:
25
        WH_Robot(std::string scoutname);
26
        ~WH_Robot();
27
        void run();
22
WH_Robot(std::string scoutname);
23
~WH_Robot();
24
void run();
28 25

  
29
        void set_task(Order order);
26
void set_task(Order order);
30 27

  
31
};
32

  
33 28
#endif
trunk/code/projects/warehouse/helper_classes/Order.h
1
#include "Order.h"
2

  
3
using namespace std;
4

  
5
/** @Brief: Regular order constructor */
6
Order::Order(int order_id, Address order_source, Address order_dest, 
7
    Time order_start_time, Path order_path, Time order_est_time) 
8
{ 
9
    id = order_id;
10
    source = order_source;
11
    dest = order_dest;
12
    start_time = order_start_time;
13
    path = order_path;
14
    est_time = order_est_time;
15
}
16

  
17
/** @Brief: Get order ID */
18
int Order::getid() const 
19
{
20
    return id;
21
}  
22

  
23
Address Order::get_source() const
24
{
25
    return source;
26
}
27

  
28
Address Order::get_dest() const
29
{
30
    return dest;
31
}
32

  
33
Time Order::get_start_time() const
34
{
35
    return start_time;
36
}
37

  
38
Path Order::get_path() const
39
{
40
    return path;
41
}
42

  
43
Time Order::get_est_time() const
44
{
45
    return est_time;
46
}
47

  
48
bool Order::operator==(Order& o1, Order& o2)
49
{
50
  return o1.id == o2.id;
51
}
52

  
53
void Order::set_path(Path order_path)
54
{
55
    path = order_path;
56
    return;
57
}
58
/** @Brief: Order comparison function for PQWrapper 
59
 *  NOTE: In order to have a min priority queue, using c++'s pq
60
 *  implementation, the compare function must return true if
61
 *  o1 is greater than o2.
62
 */
63
bool CompareOrder::operator()(Order& o1, Order& o2) 
64
{
65
  int pq_value1 = o1.get_start_time + MAX_WAIT_TIME - o1.get_est_time();
66
  int pq_value2 = o2.get_start_time + MAX_WAIT_TIME - o2.get_est_time();
67
  return pq_value1 > pq_value2;
68
}
69

  
trunk/code/projects/warehouse/helper_classes/PQWrapper.cpp
1
#include "PQWrapper.h"
2

  
3
using namespace std;
4

  
5
/** @Brief: Initialize PQWrapper data structures. */
6
PQWrapper::PQWrapper(unsigned int numElems)
7
{
8
    minElems = new Order[numElems];
9
    arrCapacity = numElems;
10
    arrContents = 0;
11
}
12

  
13
/** @Brief: Free allocatetd memory. */
14
PQWrapper::~PQWrapper()
15
{
16
    while (!pq.empty()) pq.pop();
17
}
18

  
19
/** @Brief: Insert an order into the PQWrapper. */
20
void PQWrapper::insert(Order o)
21
{
22

  
23
    int position=0;
24
    bool foundPosition = false;
25
    while (!foundPosition && position < arrContents)
26
    {
27
        if (o.getpriority() < minElems[position].getpriority()) 
28
        {
29
            foundPosition=true;
30
        } 
31
        else 
32
        {
33
            position++;
34
        }
35
    }
36
    if (foundPosition)
37
    {
38
        if (arrContents<arrCapacity)
39
        {
40
            for (int i = arrContents; i >=position+1; i--) 
41
            {
42
                minElems[i] = minElems[i-1];
43
            }            
44
            minElems[position] = o;
45
            arrContents++;
46
        } 
47
        else 
48
        {
49
            pq.push(minElems[arrCapacity-1]);
50
            for (int i = arrContents-1; i >= position+1; i--) 
51
            {
52
                minElems[i] = minElems[i-1];
53
            }            
54
            minElems[position] = o;
55
        }
56
    } 
57
    else 
58
    {     
59
        if (arrContents<arrCapacity) 
60
        {
61
            minElems[arrContents] = o;
62
            arrContents++;
63
        } else {
64
            pq.push(o);
65
        }
66
    }
67
}
68

  
69
/** @Brief: Remove and return an Order from the array if 0 <= index < arrContents. */
70
Order PQWrapper::remove(unsigned int index)
71
{
72
    if (index >= arrContents) return minElems[-1];//TODO: can I return null?
73
    Order toDelete = minElems[index];
74
    for (int i = index; i < arrContents - 1; i++) 
75
    {
76
        minElems[i] = minElems[i+1];
77
    }
78
    if (!pq.empty()) 
79
    {
80
        minElems[arrContents-1] = pq.top();
81
        pq.pop();
82
    } 
83
    else 
84
    {
85
        arrContents--;
86
    }
87
    return toDelete;
88
}
89

  
90
/** @Brief: Return an Order from the array if 0 <= index < arrContents */
91
Order PQWrapper::peek(unsigned int index) const
92
{
93
    if (index >= arrContents)
94
    {
95
        return minElems[-1];//TODO: can I return null?
96
    } 
97
    else 
98
    {
99
        return minElems[index];
100
    }
101
}
102

  
103
/** @Brief: Return the size of the minElems array */
104
unsigned int PQWrapper::arraySize() const
105
{
106
    return arrContents;
107
}
108

  
109

  
110
/*
111
//For testing purposes only
112
int main() {
113
    Order a(1,0,0,101,0,0,0);
114
    Order b(2,0,0,11,0,0,0);
115
    Order c(3,0,0,91,0,0,0);
116
    Order d(4,0,0,21,0,0,0);
117
    Order e(5,0,0,41,0,0,0);
118
    Order f(6,0,0,81,0,0,0);
119
    Order g(7,0,0,31,0,0,0);
120
    Order h(8,0,0,71,0,0,0);
121
    Order i(9,0,0,51,0,0,0);
122
    Order j(10,0,0,61,0,0,0);
123

  
124
    PQWrapper pqw(5);
125

  
126
    pqw.insert(a);
127
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
128
    cout << endl; 
129

  
130

  
131
    pqw.insert(b);
132
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
133
    cout << endl; 
134

  
135
    pqw.insert(c);
136
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
137
    cout << endl; 
138

  
139
    pqw.insert(d);
140
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
141
    cout << endl; 
142

  
143
    pqw.insert(e);
144
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
145
    cout << endl; 
146

  
147
    pqw.insert(f);
148
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
149
    cout << endl; 
150

  
151
    pqw.insert(g);
152
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
153
    cout << endl; 
154

  
155
    pqw.insert(h);
156
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
157
    cout << endl; 
158

  
159
    pqw.insert(i);
160
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
161
    cout << endl; 
162

  
163
    pqw.insert(j);
164
    for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
165
    cout << endl; 
166

  
167
    for (int i=0;i<10;i++) 
168
    {
169
        pqw.remove(0);
170
        for (int i=0;i<pqw.arraySize();i++) cout << pqw.peek(i).getid() << " ";
171
        cout << endl; 
172
    }
173

  
174
    return 0;
175
}
176
*/
trunk/code/projects/warehouse/helper_classes/PQWrapper.h
1
#ifndef _PQ_WRAPPER_
2
#define _PQ_WRAPPER_
3

  
4
#include <cstdlib>
5
#include <queue>
6
#include <iostream>
7
#include "Order.h"
8

  
9
class PQWrapper
10
{
11
    public:
12
        PQWrapper(unsigned int numElems);
13
        ~PQWrapper();
14
        
15
        void insert(Order o);
16
        Order remove(unsigned int index);
17
        Order peek(unsigned int index) const;
18
        unsigned int arraySize() const;
19

  
20
    private:
21
        Order* minElems;
22
        unsigned int arrCapacity, arrContents;
23
        std::priority_queue<Order,std::vector<Order>, CompareOrder> pq;
24
};
25
#endif
trunk/code/projects/warehouse/helper_classes/Order.cpp
1
#include "Order.h"
2

  
3
using namespace std;
4

  
5
/** @Brief: Regular order constructor */
6
Order::Order(int order_id, Address order_source, Address order_dest, 
7
    Time order_start_time, Path order_path, Time order_est_time) 
8
{ 
9
    id = order_id;
10
    source = order_source;
11
    dest = order_dest;
12
    start_time = order_start_time;
13
    path = order_path;
14
    est_time = order_est_time;
15
}
16

  
17
/** @Brief: Get order ID */
18
int Order::getid() const 
19
{
20
    return id;
21
}  
22

  
23
Address Order::get_source() const
24
{
25
    return source;
26
}
27

  
28
Address Order::get_dest() const
29
{
30
    return dest;
31
}
32

  
33
Time Order::get_start_time() const
34
{
35
    return start_time;
36
}
37

  
38
Path Order::get_path() const
39
{
40
    return path;
41
}
42

  
43
Time Order::get_est_time() const
44
{
45
    return est_time;
46
}
47

  
48
bool Order::operator==(Order& o1, Order& o2)
49
{
50
  return o1.id == o2.id;
51
}
52

  
53
void Order::set_path(Path order_path)
54
{
55
    path = order_path;
56
    return;
57
}
58
/** @Brief: Order comparison function for PQWrapper 
59
 *  NOTE: In order to have a min priority queue, using c++'s pq
60
 *  implementation, the compare function must return true if
61
 *  o1 is greater than o2.
62
 */
63
bool CompareOrder::operator()(Order& o1, Order& o2) 
64
{
65
  int pq_value1 = o1.get_start_time + MAX_WAIT_TIME - o1.get_est_time();
66
  int pq_value2 = o2.get_start_time + MAX_WAIT_TIME - o2.get_est_time();
67
  return pq_value1 > pq_value2;
68
}
69

  

Also available in: Unified diff