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


Messages In This Thread
problem with a formula 1 kind of homework - by claroque - Jun-17-2023, 03:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Bakery Problem.. Can you Help My Homework Cemvrs 1 2,406 Jan-14-2022, 05:47 PM
Last Post: Yoriz
  Can someone find out what kind of histogram is it? J_tin 1 1,835 Apr-26-2020, 05:23 PM
Last Post: buran
  Need help with a homework problem on logical operators voodoo 3 4,636 Jun-28-2019, 03:45 PM
Last Post: jefsummers
  Homework Problem csascott 1 2,307 Oct-27-2018, 04:44 AM
Last Post: scidam
  Homework Problem Travisbulls34 1 2,981 Sep-11-2018, 04:04 PM
Last Post: ichabod801
  Help with homework problem - iterating a function midnitetots12 4 3,562 Feb-21-2018, 10:51 PM
Last Post: nilamo
  Problem with a homework Samuelcu 2 2,874 Feb-03-2018, 01:39 PM
Last Post: Samuelcu
  Homework problem Dem_Prorammer 1 3,840 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