problem with a formula 1 kind of homework - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: problem with a formula 1 kind of homework (/thread-40191.html) |
problem with a formula 1 kind of homework - claroque - Jun-17-2023 in preparatory classes we have something called tipe it is basically a kind of presentation that we need to prepare and we have to put in some added value to a certain "scientific" subject that relates to a theme chosen one year in advance i chose to talk about the optimization of race performance through the use of mathematics and python. now the added value that i consider doing is a python program that determines the best race strategy (which tire compound, the lap times, pit stops , etc) to use in order to get the best result. and the issue is that i am a beginner in this coding stuff and i do not know much about how to explore the different attributes available in packages and how to retrieve the data i need from those same packages. the thing is i am maybe halfway through it but i am stuck. this is the code: import fastf1 import numpy as np import pandas as pd def evaluate_strategy(strategy, data, weather_data, car_data, pos_data): """ Evaluate a race strategy based on lap times, tire compounds, weather data, car data, and pos data. Args: strategy (tuple): A tuple of (number of laps, tire compound). data (pandas.DataFrame): A DataFrame of lap times and tire compounds. weather_data (fastf1.WeatherData): A WeatherData object containing weather data for the race. car_data (fastf1.CarData): A CarData object containing car data for all drivers. pos_data (fastf1.PosData): A PosData object containing position data for all drivers. Returns: float: The score for the strategy. """ lap_time_sum = strategy[0] * data['lap_time'].sum() tire_compound_score = len(data[data['tire_compound'] == strategy[1]]) weather_score = weather_data.get_score(strategy[1]) car_score = car_data.get_score(strategy[1]) pos_score = pos_data.get_score(strategy[1]) score = lap_time_sum + tire_compound_score + weather_score + car_score + pos_score return score # Load the race data race_data = fastf1.get_session(2023, 1,) # Load the lap data laps = race_data.get_laps(include=['LapTime', 'TireCompound']) lap_times = [lap['LapTime'] for lap in laps] tire_compounds = [lap['TireCompound'] for lap in laps] # Create a DataFrame of the lap times and tire compounds df = pd.DataFrame({'lap_time': lap_times, 'tire_compound': tire_compounds}) # Get the weather data for the race weather_data = race_data.get_weather_data() # Get the car data for all drivers car_data = race_data.get_car_data() # Get the pos data for all drivers pos_data = race_data.get_pos_data() # Create a list of all possible strategies strategies = [] num_laps = df['lap_time'].count() for i in range(1, num_laps + 1): for tire_compound in ['Soft', 'Medium', 'Hard']: strategies.append((i, tire_compound)) # Evaluate each strategy and find the most optimized one best_strategy = None best_score = np.inf for strategy in strategies: score = evaluate_strategy(strategy, df, weather_data, car_data, pos_data) if score < best_score: best_strategy = strategy best_score = score # Print the most optimized strategy print(best_strategy)now the issue is in the load lap data step as it generates an error message stating that the get_lap method is nowhere to be found. if you notice some possible improvements to be made do not hesitate to suggest them. hope you'll manage to find the solution for the problem RE: problem with a formula 1 kind of homework - deanhystad - Jun-17-2023 I am not seeing the error you describe. I get a TypeError when getting the session. The error cleared up when I changed line 28 to: race_data = fastf1.get_session(2023, 1, "Race")After you get a session, you need to load the session data. This loads the data into DataFrames, so there is no reason to create them yourself. race_data = fastf1.get_session(2023, 1, "Race") race_data.load() # Load the lap data df = pd.DataFrame(race_data.laps[["Time", "Compound"]])race_data.laps is a dataframe of lap data. df is the same data, but only for two columns (Time and Compound). What version of fastf1 are you using? Almost none of your code is correct for the most recent version (3.0.4). Looking at the release notes it looks like you are using something pre-3.0. I suggest you get the newest version. RE: problem with a formula 1 kind of homework - claroque - Jun-20-2023 (Jun-17-2023, 02:19 PM)deanhystad Wrote: I am not seeing the error you describe. I get a TypeError when getting the session. The error cleared up when I changed line 28 to: to be frank i am really new to this kind of stuff and i am using chatgpt/bard to execute the ideas i have in mind. i also do not get the concept of dataframes (the way they work) so i will try to get more informations on these matters and then i will try to update my code. after few attempts and after making the changes you told me to make, i get this new error message: when i replace it with:laps=race_data.load_laps() i get the following error message by the way i checked and i found that i have the latest version of the package. the abnormalitites are certainly due to the fact that chatgpt uses an older version of the package and since i told it to fix the code before i came to the forum it appears that it worked with some older versionthanks in advance for taking time to help me. |