Python Forum
problem with a formula 1 kind of homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with a formula 1 kind of homework
#1
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
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.
Reply
#2
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.
Reply
#3
(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:
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.

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:
Error:
Traceback (most recent call last): File "<tmp 11>", line 36, in <module> race_data.load() AttributeError: 'Weekend' object has no attribute 'load'
when i replace it with:
laps=race_data.load_laps()

i get the following error message
Error:
Traceback (most recent call last): File "<tmp 12>", line 47, in <module> laps = race_data.load_laps() File "c:\users\windownet\appdata\local\programs\python\python311\Lib\site-packages\fastf1\core.py", line 1234, in load_laps if any((laps, telemetry, weather, messages)): File "c:\users\windownet\appdata\local\programs\python\python311\Lib\site-packages\fastf1\core.py", line 1311, in load_telemetry result['New'] = False TypeError: DataFrame.drop() takes from 1 to 2 positional arguments but 3 were given
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 version
thanks in advance for taking time to help me.
buran write Jun-20-2023, 04:51 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Bakery Problem.. Can you Help My Homework Cemvrs 1 2,362 Jan-14-2022, 05:47 PM
Last Post: Yoriz
  Can someone find out what kind of histogram is it? J_tin 1 1,807 Apr-26-2020, 05:23 PM
Last Post: buran
  Need help with a homework problem on logical operators voodoo 3 4,595 Jun-28-2019, 03:45 PM
Last Post: jefsummers
  Homework Problem csascott 1 2,278 Oct-27-2018, 04:44 AM
Last Post: scidam
  Homework Problem Travisbulls34 1 2,950 Sep-11-2018, 04:04 PM
Last Post: ichabod801
  Help with homework problem - iterating a function midnitetots12 4 3,505 Feb-21-2018, 10:51 PM
Last Post: nilamo
  Problem with a homework Samuelcu 2 2,838 Feb-03-2018, 01:39 PM
Last Post: Samuelcu
  Homework problem Dem_Prorammer 1 3,800 May-20-2017, 07:49 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020