Subversion Repositories programming

Rev

Rev 334 | Rev 336 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 334 Rev 335
Line 11... Line 11...
11
    for (int i=0; i<num_elevators; i++)
11
    for (int i=0; i<num_elevators; i++)
12
    {
12
    {
13
        elevator.push_back (new Elevator (num_floors));
13
        elevator.push_back (new Elevator (num_floors));
14
        elevator.at(i) -> set_controller (this);
14
        elevator.at(i) -> set_controller (this);
15
    }
15
    }
-
 
16
 
-
 
17
    // Seed the RNG
-
 
18
    srandom (time(NULL));
16
}
19
}
17
 
20
 
18
Controller::~Controller ()
21
Controller::~Controller ()
19
{
22
{
20
}
23
}
Line 32... Line 35...
32
    // to this floor. If there is already one, ignore this
35
    // to this floor. If there is already one, ignore this
33
    // request.
36
    // request.
34
    if (floor_already_requested (on_floor))
37
    if (floor_already_requested (on_floor))
35
        return;
38
        return;
36
 
39
 
37
    puts("2");
40
    //puts("2");
38
 
41
 
39
    // find elevator that is closest, AND NOT moving away
42
    // find elevator that is closest, AND NOT moving away
40
    // "push" it's button
43
    // "push" it's button
41
    elevator_number = find_closest_elevator (on_floor, direction);
44
    elevator_number = find_closest_elevator (on_floor, direction);
42
    elevator.at(elevator_number) -> push_button (on_floor);
45
    elevator.at(elevator_number) -> push_button (on_floor);
Line 70... Line 73...
70
int Controller::find_closest_elevator (int to_floor, int in_direction)
73
int Controller::find_closest_elevator (int to_floor, int in_direction)
71
{
74
{
72
    int i;
75
    int i;
73
    float distance = INT_MAX;
76
    float distance = INT_MAX;
74
    float temp_distance;
77
    float temp_distance;
75
    int answer;
78
    int answer = -1;
76
 
79
 
77
    float cur_floor;
80
    float cur_floor;
78
    int direction;
81
    int   cur_direction;
-
 
82
 
-
 
83
    printf ("finding closest elevator: to_floor=%d -- in_direction=%d\n", to_floor, in_direction);
79
 
84
 
80
    for (i=0; i<num_elevators; i++)
85
    for (i=0; i<num_elevators; i++)
81
    {
86
    {
82
        cur_floor = elevator.at(i) -> get_current_floor ();
87
        cur_floor = elevator.at(i) -> get_current_floor ();
83
        direction = elevator.at(i) -> get_direction ();
88
        cur_direction = elevator.at(i) -> get_direction ();
84
        temp_distance = fabsf (cur_floor - to_floor);
89
        temp_distance = fabsf (cur_floor - to_floor);
85
 
90
 
86
        printf ("cur_floor=%e -- direc=%d -- t_dist=%e\n", cur_floor, direction, temp_distance);
91
        printf ("cur_floor=%e -- direc=%d -- t_dist=%e\n", cur_floor, cur_direction, temp_distance);
87
 
92
 
88
        // Automatically discard elevators that are moving in the wrong
93
        // Automatically discard elevators that are moving in the wrong
89
        // direction, but DO consider ones that are idle.
94
        // direction, but DO consider ones that are idle.
90
        if (direction == MOVE_DOWN && in_direction == MOVE_UP)
95
        if (cur_direction == MOVE_DOWN && in_direction == MOVE_UP)
-
 
96
        {
-
 
97
            printf ("type 1: skipped elevator[%d]\n", i);
91
            continue;
98
            continue;
-
 
99
        }
92
 
100
 
93
        if (direction == MOVE_UP && in_direction == MOVE_DOWN)
101
        if (cur_direction == MOVE_UP && in_direction == MOVE_DOWN)
-
 
102
        {
-
 
103
            printf ("type 2: skipped elevator[%d]\n", i);
94
            continue;
104
            continue;
-
 
105
        }
95
 
106
 
96
        // Check that this elevator is closer
-
 
97
        if (temp_distance < distance)
107
        if (cur_floor < to_floor && cur_direction == MOVE_UP)
98
        {
108
        {
99
            // If we are below the floor we want to go to and we
109
            printf ("good cantidate type 1\n");
100
            // are moving up, then this is an ok choice
110
            // GOOD CANTIDATE
101
            if (cur_floor < to_floor && direction == MOVE_UP)
111
            if (temp_distance < distance)
102
            {
112
            {
103
                answer = i;
113
                answer = i;
104
                distance = temp_distance;
114
                distance = temp_distance;
105
            }
115
            }
106
            // If we are above the floor we want to go to and we
-
 
107
            // are moving down, then this is an ok choice
116
        }
-
 
117
 
108
            else if (cur_floor > to_floor && direction == MOVE_DOWN)
118
        if (cur_floor > to_floor && cur_direction == MOVE_DOWN)
109
            {
119
        {
110
                answer = i;
-
 
111
                distance = temp_distance;
120
            printf ("good cantidate type 2\n");
112
            }
121
            // GOOD CANTIDATE
113
            // If we are closest, but idle, then this is an ok choice
122
            if (temp_distance < distance)
114
            else
-
 
115
            {
123
            {
116
                answer = i;
124
                answer = i;
117
                distance = temp_distance;
125
                distance = temp_distance;
118
            }
126
            }
119
        }
127
        }
-
 
128
 
-
 
129
        if (cur_direction == IDLE && temp_distance < distance)
-
 
130
        {
-
 
131
            printf ("idle cantidate\n");
-
 
132
            answer = i;
-
 
133
            distance = temp_distance;
-
 
134
        }
-
 
135
    }
-
 
136
 
-
 
137
    // If we failed to find an elevator, then we need to
-
 
138
    // pick at random.
-
 
139
    if (answer == -1)
-
 
140
    {
-
 
141
        // Pick a random number in the range [0,num_elevators-1]
-
 
142
        answer = (int) (0.0 + ((num_elevators - 1.0) * (random() / (RAND_MAX + 1.0))));
-
 
143
 
-
 
144
        printf ("picked at random: %d\n", answer);
-
 
145
 
-
 
146
        assert (answer >= 0);
-
 
147
        assert (answer < num_elevators);
120
    }
148
    }
121
 
149
 
122
    return answer;
150
    return answer;
123
}
151
}
124
 
152