Jun-17-2023, 03:27 AM
(This post was last modified: Jun-17-2023, 01:25 PM by deanhystad.)
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:
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
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
deanhystad write Jun-17-2023, 01:25 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.