File size: 10,992 Bytes
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from tools.flights.apis import Flights
from tools.accommodations.apis import Accommodations
from tools.restaurants.apis import Restaurants
from tools.googleDistanceMatrix.apis import GoogleDistanceMatrix
from tools.googlePlaces.apis import GooglePlaces
from tools.attractions.apis import Attractions
from evaluation.hardConstriant import extract_from_to,get_valid_name_city
import math



    

class ReactEnv:
    def __init__(self):
        
        self.flight = Flights()
        self.accommodation = Accommodations()
        self.restaurants = Restaurants()
        self.googleDistanceMatrix = GoogleDistanceMatrix()
        self.googlePlaces = GooglePlaces()
        self.attractions = Attractions()
    
    def run(self, tested_data):

        total_cost = 0
        unit = tested_data
        people_number = tested_data['people_number']
        returned_info = []

        if 'transportation' in unit and unit['transportation'] and  unit['transportation'] != '-':
            value = unit['transportation']
            org_city, dest_city = extract_from_to(value)
            if org_city == None or dest_city == None:
                org_city, dest_city = extract_from_to(unit['current_city'])
            if 'flight number' in value.lower():
                    try:
                        res = self.flight.data[self.flight.data['Flight Number'] == value.split('Flight Number: ')[1].split(',')[0]]
                        if len(res) > 0:
                            total_cost += res['Price'].values[0] * people_number
                        else:
                            returned_info.append('The filght information is not valid')
                    except:
                        returned_info.append('The filght information is not valid')

            elif 'self-driving' in value.lower() or 'taxi' in value.lower():
                try:
                    if 'self-driving' in value.lower():
                        # print(org_city,dest_city)
                        cost = self.googleDistanceMatrix.run_for_evaluation(org_city,dest_city,'self-driving')['cost']
                        if cost == None:
                            returned_info.append('The transporation information is not valid, please check.')
                        else:
                            total_cost += cost * math.ceil(people_number * 1.0 / 5)
                    else:
                        cost = self.googleDistanceMatrix.run_for_evaluation(org_city,dest_city,'taxi')['cost']
                        if cost == None:
                            returned_info.append('The transporation information is not valid, please check.')
                        else:
                            total_cost += cost * math.ceil(people_number * 1.0 / 4)
                except:
                    returned_info.append('The transporation information is not valid, please check. You have to make sure there are two cities (from A to B) in your transportation plan.')

        if 'breakfast' in unit and unit['breakfast'] and unit['breakfast'] != '-':
            name, city = get_valid_name_city(unit['breakfast'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The breakfase information is not valid, please check.')

        if 'lunch' in unit and  unit['lunch'] and unit['lunch'] != '-':
            name, city = get_valid_name_city(unit['lunch'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The lunch information is not valid, please check.')

        if 'dinner' in unit and unit['dinner'] and unit['dinner'] != '-':
            name, city = get_valid_name_city(unit['dinner'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The dinner information is not valid, please check.')

        if 'accommodation' in unit and unit['accommodation'] and unit['accommodation'] != '-':
            name, city = get_valid_name_city(unit['accommodation'])
            if name != '-' and city != '-':
                res = self.accommodation.data[(self.accommodation.data['NAME'] == name) & (self.accommodation.data['city'] == city)]
                if len(res) > 0:
                    total_cost += res['price'].values[0] * math.ceil(people_number * 1.0 / res['maximum occupancy'].values[0])
                else:
                    returned_info.append('The accommodation information is not valid, please check.')
        
        if len(returned_info) == 0:
            return "The cost of your plan is " + str(total_cost) + " dollars."
        else:
            message = "Sorry, the cost of your plan is not available because of the following reasons:"
            for idx, info in enumerate(returned_info):
                message += str(idx + 1) + ". " + info + " " + '\t'
            return message
        
class ReactReflectEnv(ReactEnv):
    def __init__(self):
        super().__init__()
        self.is_terminated = False
        self.max_retry_step = 3
        self.retry_step = 0

    def reset(self):
        self.is_terminated = False
        self.retry_step = 0

    def run(self, tested_data):
        total_cost = 0
        unit = tested_data
        people_number = tested_data['people_number']
        returned_info = []

        if 'transportation' in unit and unit['transportation'] and  unit['transportation'] != '-':
            value = unit['transportation']
            org_city, dest_city = extract_from_to(value)
            if org_city == None or dest_city == None:
                org_city, dest_city = extract_from_to(unit['current_city'])
                
            
            if org_city == None or dest_city == None:
                returned_info.append('The transporation information is not valid, please check.')

            else:    
                if 'flight number' in value.lower():
                        try:
                            res = self.flight.data[self.flight.data['Flight Number'] == value.split('Flight Number: ')[1].split(',')[0]]
                            if len(res) > 0:
                                total_cost += res['Price'].values[0] * people_number
                            else:
                                returned_info.append('The filght information is not valid')
                        except:
                            returned_info.append('The filght information is not valid')

                elif 'self-driving' in value.lower() or 'taxi' in value.lower():
                        if 'self-driving' in value.lower():
                            cost = self.googleDistanceMatrix.run_for_evaluation(org_city,dest_city,'self-driving')['cost']
                            if cost == None:
                                returned_info.append('The transporation information is not valid, please check.')
                            else:
                                total_cost += cost * math.ceil(people_number * 1.0 / 5)
                        else:
                            cost = self.googleDistanceMatrix.run_for_evaluation(org_city,dest_city,'taxi')['cost']
                            if cost == None:
                                returned_info.append('The transporation information is not valid, please check.')
                            else:
                                total_cost += cost * math.ceil(people_number * 1.0 / 4)

        if 'breakfast' in unit and unit['breakfast'] and unit['breakfast'] != '-':
            name, city = get_valid_name_city(unit['breakfast'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The breakfase information is not valid, please check.')

        if 'lunch' in unit and  unit['lunch'] and unit['lunch'] != '-':
            name, city = get_valid_name_city(unit['lunch'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The lunch information is not valid, please check.')

        if 'dinner' in unit and unit['dinner'] and unit['dinner'] != '-':
            name, city = get_valid_name_city(unit['dinner'])
            if name != '-' and city != '-':
                res = self.restaurants.data[(self.restaurants.data['Name'] == name) & (self.restaurants.data['City'] == city)]
                if len(res) > 0:
                    total_cost += res['Average Cost'].values[0] * people_number
                else:
                    returned_info.append('The dinner information is not valid, please check.')

        if 'accommodation' in unit and unit['accommodation'] and unit['accommodation'] != '-':
            name, city = get_valid_name_city(unit['accommodation'])
            if name != '-' and city != '-':
                res = self.accommodation.data[(self.accommodation.data['NAME'] == name) & (self.accommodation.data['city'] == city)]
                if len(res) > 0:
                    total_cost += res['price'].values[0] * math.ceil(people_number * 1.0 / res['maximum occupancy'].values[0])
                else:
                    returned_info.append('The accommodation information is not valid, please check.')
        
        if len(returned_info) == 0:
            self.retry_step = 0
            self.is_terminated = False
            return "The cost of your plan is " + str(total_cost) + " dollars."
        else:
            message = "Sorry, the cost of your plan is not available because of the following reasons:"
            for idx, info in enumerate(returned_info):
                message += str(idx + 1) + ". " + info + " " + '\t'
            self.retry_step += 1
            if self.retry_step >= self.max_retry_step:
                self.is_terminated = True
            return message