Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

This is the code of a pokemon fusion optimizer I found online, I would like to enhance…

  1. User

    This is the code of a pokemon fusion optimizer I found online, I would like to enhance it as there has to be some kind of underlying error with it. It sometimes generate fusions recommending pre-evolved forms over fully-evolved pokemon, which means that it must be filtering out the better pokemon in that case.

    import requests import json import os import streamlit as st import pandas as pd import numpy as np

    Function to get the list of all Pokémon

    @st.cache_data def get_pokemon_list(): url = 'https://pokeapi.co/api/v2/pokemon?limit=809' # Adjust the limit if needed response = requests.get(url) data = response.json() return data['results']

    Function to get detailed data for a single Pokémon

    def get_pokemon_data(pokemon_url): response = requests.get(pokemon_url) pokemon_data = response.json() return pokemon_data

    Function to fetch and store Pokémon data locally

    def fetch_and_store_pokemon_data(json_filename): st.info("Fetching Pokémon data from the API...") pokemon_list = get_pokemon_list() all_pokemon_data = []

    progress_bar = st.progress(0)
    total_pokemon = len(pokemon_list)
    
    # Fetch data for each Pokémon
    for index, pokemon in enumerate(pokemon_list, start=1):
        pokemon_name = pokemon['name']
        pokemon_url = pokemon['url']
        pokemon_data = get_pokemon_data(pokemon_url)
    
        # Get types
        types = [t['type']['name'] for t in pokemon_data['types']]
    
        # Get base stats
        stats = {stat['stat']['name']: stat['base_stat'] for stat in pokemon_data['stats']}
    
        # Get abilities
        abilities = [a['ability']['name'] for a in pokemon_data['abilities']]
    
        # Get moves
        moves = [move['move']['name'] for move in pokemon_data['moves']]
    
        # Create a Pokémon entry
        pokemon_entry = {
            'name': pokemon_name,
            'types': types,
            'stats': stats,
            'abilities': abilities,
            'moves': moves
        }
    
        all_pokemon_data.append(pokemon_entry)
    
        # Update progress bar
        progress_bar.progress(index / total_pokemon)
    
    # Save data to JSON file
    with open(json_filename, 'w') as json_file:
        json.dump(all_pokemon_data, json_file, indent=4)
    
    st.success(f"Data saved to '{json_filename}'")
    return all_pokemon_data
    

    Function to load data from the local JSON file

    @st.cache_data def load_pokemon_data(json_filename): with open(json_filename, 'r') as json_file: all_pokemon_data = json.load(json_file) return all_pokemon_data

    Function to load or fetch Pokémon data

    def get_all_pokemon_data(json_filename): if os.path.exists(json_filename): all_pokemon_data = load_pokemon_data(json_filename) # Check if 'moves' is present in the data; if not, refetch data if 'moves' not in all_pokemon_data[0]: st.warning("Existing data does not include Pokémon moves. Refetching data...") all_pokemon_data = fetch_and_store_pokemon_data(json_filename) else: all_pokemon_data = fetch_and_store_pokemon_data(json_filename) return all_pokemon_data

    def main(): st.title("Pokémon Fusion Optimizer")

    # Load data
    json_filename = 'pokemon_data.json'
    all_pokemon_data = get_all_pokemon_data(json_filename)
    
    # Convert data to DataFrame
    data_rows = []
    for pkmn in all_pokemon_data:
        row = {
            'Name': pkmn['name'],
            'Types': pkmn['types'],
            'Abilities': pkmn['abilities'],
            'Moves': pkmn['moves'],
        }
        # Add stats
        for stat_name, stat_value in pkmn['stats'].items():
            row[stat_name] = stat_value
        # Calculate Base Stat Total (BST)
        row['BST'] = sum(pkmn['stats'].values())
        data_rows.append(row)
    df = pd.DataFrame(data_rows)
    df.set_index('Name', inplace=True)
    
    # User input for stat weights
    st.sidebar.header("Stat Weights")
    stat_columns = ['hp', 'attack', 'defense', 'special-attack', 'special-defense', 'speed']
    stat_weights = {}
    for stat in stat_columns:
        stat_weights[stat] = st.sidebar.slider(f"Weight for {stat.replace('-', ' ').title()}:", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
    
    # Max BST filter for base Pokémon
    st.sidebar.header("Base Pokémon Filters")
    max_bst = st.sidebar.number_input("Maximum BST for Base Pokémon:", min_value=0, max_value=720, value=720, step=1)
    
    # Stat filters for base Pokémon
    st.sidebar.write("## Base Stat Filters")
    stat_filters = {}
    for stat in stat_columns:
        min_val = st.sidebar.number_input(f"Minimum {stat.replace('-', ' ').title()}:", min_value=0, max_value=255, value=0, step=1, key=f"min_{stat}")
        max_val = st.sidebar.number_input(f"Maximum {stat.replace('-', ' ').title()}:", min_value=0, max_value=255, value=255, step=1, key=f"max_{stat}")
        stat_filters[stat] = (min_val, max_val)
    
    # Type filters for base Pokémon
    st.sidebar.write("## Type Filters for Base Pokémon")
    types_list = sorted({t.title() for types in df['Types'] for t in types})
    selected_types = st.sidebar.multiselect("Select Base Pokémon Types:", types_list)
    
    # Number of top fusions to display
    st.sidebar.header("Fusion Options")
    top_k = st.sidebar.number_input("Number of Top Fusions to Display:", min_value=1, max_value=500, value=50, step=1)
    
    # Type filters for fusions (optional)
    st.sidebar.write("## Fusion Type Filters (Optional)")
    fusion_type_inputs = st.sidebar.multiselect("Select Desired Fusion Types (leave empty for no filter):", types_list)
    
    # Ability filters for fusions (optional)
    st.sidebar.write("## Fusion Ability Filters (Optional)")
    # Collect all abilities from the dataset
    abilities_list = sorted({ability.title() for abilities in df['Abilities'] for ability in abilities})
    fusion_ability_inputs = st.sidebar.multiselect("Select Desired Fusion Abilities (leave empty for no filter):", abilities_list)
    
    # Move filters for fusions (optional)
    st.sidebar.write("## Fusion Moveset Filters (Optional)")
    # Collect moves from the dataset
    moves_list = sorted({move.title() for moves in df['Moves'] for move in moves})
    fusion_move_inputs = st.sidebar.multiselect("Select Required Moves (leave empty for no filter):", moves_list)
    
    # Button to compute fusions
    if st.sidebar.button("Compute Best Fusions"):
        with st.spinner("Computing best fusions..."):
            # Filter base Pokémon based on BST
            filtered_df = df[df['BST'] <= max_bst]
    
            # Apply stat filters to base Pokémon
            for stat in stat_columns:
                min_val, max_val = stat_filters[stat]
                filtered_df = filtered_df[(filtered_df[stat] >= min_val) & (filtered_df[stat] <= max_val)]
    
            # Apply type filters to base Pokémon
            if selected_types:
                filtered_df = filtered_df[filtered_df['Types'].apply(lambda types: any(t.title() in selected_types for t in types))]
    
            if filtered_df.empty:
                st.warning("No base Pokémon match the specified filters.")
                return
    
            # Precompute head and body contributions for all filtered Pokémon
            head_contributions = {}
            body_contributions = {}
            for name, row in filtered_df.iterrows():
                stats = row[stat_columns]
                types = row['Types']
                abilities = row['Abilities']
                moves = set(row['Moves'])
    
                # Head contributions
                head_stats = {
                    'hp': (2/3) * stats['hp'],
                    'attack': (1/3) * stats['attack'],
                    'defense': (1/3) * stats['defense'],
                    'special-attack': (2/3) * stats['special-attack'],
                    'special-defense': (1/3) * stats['special-defense'],
                    'speed': (2/3) * stats['speed']
                }
                # Compute weighted head score
                head_score = sum(head_stats[stat] * stat_weights[stat] for stat in stat_columns)
                head_contributions[name] = {'stats': head_stats, 'score': head_score, 'types': types, 'abilities': abilities, 'moves': moves}
    
                # Body contributions
                body_stats = {
                    'hp': (1/3) * stats['hp'],
                    'attack': (2/3) * stats['attack'],
                    'defense': (2/3) * stats['defense'],
                    'special-attack': (1/3) * stats['special-attack'],
                    'special-defense': (2/3) * stats['special-defense'],
                    'speed': (1/3) * stats['speed']
                }
                # Compute weighted body score
                body_score = sum(body_stats[stat] * stat_weights[stat] for stat in stat_columns)
                body_contributions[name] = {'stats': body_stats, 'score': body_score, 'types': types, 'abilities': abilities, 'moves': moves}
    
            # Generate all possible head-body pairs
            heads_list = list(head_contributions.keys())
            bodies_list = list(body_contributions.keys())
    
            # Limit the number of pairs to process to prevent overloading
            max_pairs = 50000  # Adjust as needed
            total_pairs = len(heads_list) * len(bodies_list)
            if total_pairs > max_pairs:
                st.info(f"Processing a sample of {max_pairs} out of {total_pairs} possible fusion pairs to maintain performance.")
                np.random.seed(42)  # For reproducibility
                sample_heads = np.random.choice(heads_list, size=min(len(heads_list), int(np.sqrt(max_pairs))), replace=False)
                sample_bodies = np.random.choice(bodies_list, size=min(len(bodies_list), int(np.sqrt(max_pairs))), replace=False)
            else:
                sample_heads = heads_list
                sample_bodies = bodies_list
    
            possible_pairs = []
    
            # Prepare desired fusion types (if any)
            desired_fusion_types = set([t.title() for t in fusion_type_inputs])
    
            # Prepare desired fusion abilities (if any)
            desired_fusion_abilities = set([a.lower() for a in fusion_ability_inputs])
    
            # Prepare desired fusion moves (if any)
            desired_fusion_moves = set([m.lower() for m in fusion_move_inputs])
    
            # Generate all possible head-body pairs
            for head_name in sample_heads:
                head_data = head_contributions[head_name]
                head_types = head_data['types']
                fusion_t1 = head_types[0].title() if head_types else ''
                for body_name in sample_bodies:
                    body_data = body_contributions[body_name]
                    body_types = body_data['types']
                    # Fusion T2 calculation
                    if len(body_types) > 1:
                        fusion_t2 = body_types[1].title()
                    else:
                        fusion_t2 = body_types[0].title() if body_types else ''
                    # Avoid duplicate types
                    if fusion_t1 == fusion_t2:
                        fusion_types_set = {fusion_t1}
                        fusion_t2 = ''
                    else:
                        fusion_types_set = {fusion_t1, fusion_t2}
    
                    # Apply fusion type filters (if any)
                    if desired_fusion_types:
                        if desired_fusion_types.issubset(fusion_types_set):
                            pass
                        else:
                            continue  # Skip if the desired types are not a subset of fusion types
                    # Else: No fusion type filters applied
    
                    # Fusion Ability (inherited from body Pokémon)
                    fusion_abilities = [a.lower() for a in body_data['abilities']]
    
                    # Apply fusion ability filters (if any)
                    if desired_fusion_abilities:
                        if not desired_fusion_abilities.intersection(set(fusion_abilities)):
                            continue  # Skip if fusion abilities do not match desired abilities
                    # Else: No fusion ability filters applied
    
                    # Fusion Moveset (combined moves of head and body)
                    fusion_moves = head_data['moves'].union(body_data['moves'])
    
                    # Apply fusion move filters (if any)
                    if desired_fusion_moves:
                        if not desired_fusion_moves.issubset(set([m.lower() for m in fusion_moves])):
                            continue  # Skip if fusion moves do not include all desired moves
                    # Else: No fusion move filters applied
    
                    # Compute total fusion score
                    total_score = head_data['score'] + body_data['score']
    
                    # Collect the pair
                    possible_pairs.append((total_score, head_name, body_name, fusion_t1, fusion_t2, fusion_abilities))
    
            if not possible_pairs:
                st.warning("No fusions match the specified filters.")
                return
    
            # Sort possible pairs by total score
            possible_pairs.sort(reverse=True)
    
            # Get top K fusions
            top_fusions = []
            for idx, (fusion_score, head_name, body_name, fusion_t1, fusion_t2, fusion_abilities) in enumerate(possible_pairs):
                if idx >= top_k:
                    break
    
                # Compute fusion stats
                fusion_stats = {}
                for stat in stat_columns:
                    hs = head_contributions[head_name]['stats'][stat]
                    bs = body_contributions[body_name]['stats'][stat]
                    fusion_stats[stat] = hs + bs
    
                # Format abilities for display
                abilities_display = ', '.join([a.title() for a in fusion_abilities])
    
                fusion_entry = {
                    'Head': head_name.title(),
                    'Body': body_name.title(),
                    'Fusion Score': fusion_score,
                    'Type 1': fusion_t1,
                    'Type 2': fusion_t2,
                    'Abilities': abilities_display,
                }
                for stat in stat_columns:
                    fusion_entry[stat.replace('-', ' ').title()] = int(fusion_stats[stat])
                top_fusions.append(fusion_entry)
    
            if top_fusions:
                # Apply stat filters to fusions
                fusion_df = pd.DataFrame(top_fusions)
                for stat in stat_columns:
                    min_val, max_val = stat_filters[stat]
                    fusion_df = fusion_df[(fusion_df[stat.replace('-', ' ').title()] >= min_val) & (fusion_df[stat.replace('-', ' ').title()] <= max_val)]
    
                if fusion_df.empty:
                    st.warning("No fusions match the specified filters.")
                else:
                    fusion_df = fusion_df[['Head', 'Body', 'Type 1', 'Type 2', 'Abilities', 'Fusion Score'] + [stat.replace('-', ' ').title() for stat in stat_columns]]
                    st.write(f"### Top {len(fusion_df)} Fusions:")
                    st.dataframe(fusion_df.reset_index(drop=True))
            else:
                st.warning("No fusions match the specified filters.")
    else:
        st.write("Adjust the settings and click 'Compute Best Fusions' to find the optimal Pokémon fusions based on your preferences.")
    

    if name == "main": main()

  2. Assistant

    To enhance your Pokémon Fusion Optimizer and ensure it prioritizes fully-evolved Pokémon over their pre-evolved forms, you'll need to incorporate evolution stage data into your dataset. This will allow the optimizer to recognize and prefer fully-evolved forms during the fusion process.

    Here's a step-by-step guide to achieve this:

    1. Fetch Evolution Data from PokeAPI:

      • Purpose: Determine whether a Pokémon is fully evolved.
      • Method: Use the /pokemon-species/{name} endpoint to fetch evolution chains and identify the final evolution forms.
    2. Annotate Each Pokémon with Evolution Status:

      • Purpose: Add a flag (e.g., is_final_evolution) to indicate whether a Pokémon is fully evolved.
      • Method: Parse the evolution chain data and set the flag accordingly.
    3. Modify the Fusion Logic to Prefer Fully-Evolved Pokémon:

      • Purpose: Ensure the optimizer favors fully-evolved forms either by excluding pre-evolved forms or by boosting the scores of fusions involving evolved Pokémon.
      • Method: Adjust filters or scoring mechanisms based on the is_final_evolution flag.

    Step-by-Step Implementation

    1. Extend Data Fetching to Include Evolution Information

    Firstly, we'll modify the data fetching process to include the evolution status of each Pokémon.

    import requests
    import json
    import os
    import streamlit as st
    import pandas as pd
    import numpy as np
    from functools import lru_cache
    
    # Function to get the list of all Pokémon
    @st.cache_data
    def get_pokemon_list():
        url = 'https://pokeapi.co/api/v2/pokemon?limit=809'  # Adjust the limit if needed
        response = requests.get(url)
        data = response.json()
        return data['results']
    
    # Function to get detailed data for a single Pokémon
    @lru_cache(maxsize=None)
    def get_pokemon_data(pokemon_url):
        response = requests.get(pokemon_url)
        pokemon_data = response.json()
        return pokemon_data
    
    # Function to determine if a Pokémon is fully evolved
    @lru_cache(maxsize=None)
    def is_final_evolution(pokemon_name):
        species_url = f"https://pokeapi.co/api/v2/pokemon-species/{pokemon_name}/"
        response = requests.get(species_url)
        if response.status_code != 200:
            return False  # Default to False if any issue occurs
        data = response.json()
        evolution_chain_url = data['evolution_chain']['url']
        
        # Fetch evolution chain
        response_chain = requests.get(evolution_chain_url)
        if response_chain.status_code != 200:
            return False
        chain_data = response_chain.json()['chain']
        
        # Recursive function to traverse the evolution chain
        def traverse(chain, target):
            if chain['species']['name'] == target:
                return len(chain['evolves_to']) == 0
            for evo in chain['evolves_to']:
                result = traverse(evo, target)
                if result is not None:
                    return result
            return None
    
        result = traverse(chain_data, pokemon_name)
        if result is None:
            return False
        return result
    
    # Function to fetch and store Pokémon data locally
    def fetch_and_store_pokemon_data(json_filename):
        st.info("Fetching Pokémon data from the API...")
        pokemon_list = get_pokemon_list()
        all_pokemon_data = []
    
        progress_bar = st.progress(0)
        total_pokemon = len(pokemon_list)
    
        for index, pokemon in enumerate(pokemon_list, start=1):
            pokemon_name = pokemon['name']
            pokemon_url = pokemon['url']
            pokemon_data = get_pokemon_data(pokemon_url)
    
            # Get types
            types = [t['type']['name'] for t in pokemon_data['types']]
    
            # Get base stats
            stats = {stat['stat']['name']: stat['base_stat'] for stat in pokemon_data['stats']}
    
            # Get abilities
            abilities = [a['ability']['name'] for a in pokemon_data['abilities']]
    
            # Get moves
            moves = [move['move']['name'] for move in pokemon_data['moves']]
    
            # Determine if the Pokémon is fully evolved
            final_evo = is_final_evolution(pokemon_name)
    
            # Create a Pokémon entry
            pokemon_entry = {
                'name': pokemon_name,
                'types': types,
                'stats': stats,
                'abilities': abilities,
                'moves': moves,
                'is_final_evolution': final_evo
            }
    
            all_pokemon_data.append(pokemon_entry)
    
            # Update progress bar
            progress_bar.progress(index / total_pokemon)
    
        # Save data to JSON file
        with open(json_filename, 'w') as json_file:
            json.dump(all_pokemon_data, json_file, indent=4)
    
        st.success(f"Data saved to '{json_filename}'")
        return all_pokemon_data
    

    Notes:

    • Caching with lru_cache: To minimize the number of API calls, especially for evolution data, the lru_cache decorator caches the results of the get_pokemon_data and is_final_evolution functions.
    • Error Handling: The is_final_evolution function includes basic error handling to default to False if any API call fails.
    • Performance Considerations: Fetching evolution data for 809 Pokémon can be time-consuming and may hit PokeAPI's rate limits. To mitigate this:
      • Rate Limiting: Implement delays between API calls if necessary.
      • Caching: The use of lru_cache helps prevent redundant API calls during a session.
      • Pre-Fetched Data: Consider pre-fetching and storing evolution data if you plan to run the optimizer multiple times.

    2. Update the Data Processing to Include Evolution Status

    Modify the part of the code where you load and process the Pokémon data to include the is_final_evolution flag.

    def main():
        st.title("Pokémon Fusion Optimizer")
    
        # Load data
        json_filename = 'pokemon_data.json'
        all_pokemon_data = get_all_pokemon_data(json_filename)
    
        # Convert data to DataFrame
        data_rows = []
        for pkmn in all_pokemon_data:
            row = {
                'Name': pkmn['name'],
                'Types': pkmn['types'],
                'Abilities': pkmn['abilities'],
                'Moves': pkmn['moves'],
                'IsFinalEvolution': pkmn['is_final_evolution']
            }
            # Add stats
            for stat_name, stat_value in pkmn['stats'].items():
                row[stat_name] = stat_value
            # Calculate Base Stat Total (BST)
            row['BST'] = sum(pkmn['stats'].values())
            data_rows.append(row)
        df = pd.DataFrame(data_rows)
        df.set_index('Name', inplace=True)
    
        # ... [rest of your code remains unchanged]
    

    3. Modify the Fusion Logic to Prefer Fully-Evolved Pokémon

    Now, adjust the fusion logic to either exclude pre-evolved forms or to prioritize fusions involving fully-evolved Pokémon.

    Option A: Exclude Pre-Evolved Forms

    If you prefer to exclude pre-evolved forms entirely from being selected as head or body, apply filters based on the is_final_evolution flag.

    # Apply an additional filter to include only fully evolved Pokémon
    st.sidebar.header("Evolution Filters")
    evolution_filter = st.sidebar.selectbox(
        "Include Only Fully Evolved Pokémon:",
        options=["All", "Only Fully Evolved"]
    )
    
    # Inside the Compute Best Fusions block, before computing contributions:
    if evolution_filter == "Only Fully Evolved":
        filtered_df = filtered_df[filtered_df['IsFinalEvolution'] == True]
    

    Option B: Prioritize Fully-Evolved Pokémon in Scoring

    Alternatively, if you want to prioritize fully-evolved forms without excluding pre-evolved ones, adjust the fusion score to add a bonus for fusions involving evolved Pokémon.

    # Define a bonus score for final evolutions
    EVOLUTION_BONUS = 50  # Adjust the bonus value as needed
    
    # When computing head and body contributions:
    # In the head_contributions loop
    if head_data['is_final_evolution']:
        head_score += EVOLUTION_BONUS
    
    # In the body_contributions loop
    if body_data['is_final_evolution']:
        body_score += EVOLUTION_BONUS
    

    Implementing the Bonus:

    Modify the head and body contributions to include the evolution bonus.

    # Inside the Compute Best Fusions block
    EVOLUTION_BONUS = 50  # You can adjust this value
    
    # Precompute head and body contributions for all filtered Pokémon
    head_contributions = {}
    body_contributions = {}
    for name, row_p in filtered_df.iterrows():
        stats = row_p[stat_columns]
        types = row_p['Types']
        abilities = row_p['Abilities']
        moves = set(row_p['Moves'])
        is_final = row_p['IsFinalEvolution']
    
        # Head contributions
        head_stats = {
            'hp': (2/3) * stats['hp'],
            'attack': (1/3) * stats['attack'],
            'defense': (1/3) * stats['defense'],
            'special-attack': (2/3) * stats['special-attack'],
            'special-defense': (1/3) * stats['special-defense'],
            'speed': (2/3) * stats['speed']
        }
        # Compute weighted head score
        head_score = sum(head_stats[stat] * stat_weights[stat] for stat in stat_columns)
        if is_final:
            head_score += EVOLUTION_BONUS
        head_contributions[name] = {'stats': head_stats, 'score': head_score, 'types': types, 'abilities': abilities, 'moves': moves, 'is_final_evolution': is_final}
    
        # Body contributions
        body_stats = {
            'hp': (1/3) * stats['hp'],
            'attack': (2/3) * stats['attack'],
            'defense': (2/3) * stats['defense'],
            'special-attack': (1/3) * stats['special-attack'],
            'special-defense': (2/3) * stats['special-defense'],
            'speed': (1/3) * stats['speed']
        }
        # Compute weighted body score
        body_score = sum(body_stats[stat] * stat_weights[stat] for stat in stat_columns)
        if is_final:
            body_score += EVOLUTION_BONUS
        body_contributions[name] = {'stats': body_stats, 'score': body_score, 'types': types, 'abilities': abilities, 'moves': moves, 'is_final_evolution': is_final}
    

    Explanation:

    • Evolution Bonus: Adding a fixed bonus to the score of fusions involving fully-evolved Pokémon ensures they are ranked higher during the sorting process.
    • Flexibility: You can adjust the EVOLUTION_BONUS value based on how strongly you want to prioritize evolved forms.

    4. (Optional) Allow Users to Toggle Evolution Preferences

    To provide more flexibility, let users decide whether to include pre-evolved forms or prioritize evolved ones.

    # In the sidebar
    st.sidebar.header("Evolution Preferences")
    evolution_preference = st.sidebar.radio(
        "Select Evolution Preference:",
        options=["Include All Forms", "Only Fully Evolved", "Prioritize Fully Evolved"]
    )
    
    # Inside the Compute Best Fusions block
    if evolution_preference == "Only Fully Evolved":
        filtered_df = filtered_df[filtered_df['IsFinalEvolution'] == True]
    elif evolution_preference == "Prioritize Fully Evolved":
        EVOLUTION_BONUS = 50  # Adjust as needed
        # Apply bonuses as shown in Option B above
    

    5. Final Adjustments and Testing

    After implementing the above changes:

    1. Test the Optimizer:

      • Ensure that fusions now prefer or exclusively use fully-evolved Pokémon based on your chosen method.
      • Verify that performance remains acceptable, especially if handling a large number of API calls.
    2. Handle Rate Limits:

      • Consider Caching: Ensure that cached data (lru_cache) is effectively reducing redundant API calls.
      • Implement Delays: If necessary, add delays between API calls to comply with PokeAPI's rate limits.
    3. Optimize Data Fetching:

      • Batch Requests: If possible, fetch data in batches to reduce the number of HTTP requests.
      • Error Handling: Enhance error handling to manage any unexpected API responses gracefully.

    Complete Enhanced Code Snippet

    Below is the enhanced version of your original code with the above modifications integrated.

    import requests
    import json
    import os
    import streamlit as st
    import pandas as pd
    import numpy as np
    from functools import lru_cache
    
    # Function to get the list of all Pokémon
    @st.cache_data
    def get_pokemon_list():
        url = 'https://pokeapi.co/api/v2/pokemon?limit=809'  # Adjust the limit if needed
        response = requests.get(url)
        data = response.json()
        return data['results']
    
    # Function to get detailed data for a single Pokémon
    @lru_cache(maxsize=None)
    def get_pokemon_data(pokemon_url):
        response = requests.get(pokemon_url)
        pokemon_data = response.json()
        return pokemon_data
    
    # Function to determine if a Pokémon is fully evolved
    @lru_cache(maxsize=None)
    def is_final_evolution(pokemon_name):
        species_url = f"https://pokeapi.co/api/v2/pokemon-species/{pokemon_name}/"
        response = requests.get(species_url)
        if response.status_code != 200:
            return False  # Default to False if any issue occurs
        data = response.json()
        evolution_chain_url = data['evolution_chain']['url']
        
        # Fetch evolution chain
        response_chain = requests.get(evolution_chain_url)
        if response_chain.status_code != 200:
            return False
        chain_data = response_chain.json()['chain']
        
        # Recursive function to traverse the evolution chain
        def traverse(chain, target):
            if chain['species']['name'] == target:
                return len(chain['evolves_to']) == 0
            for evo in chain['evolves_to']:
                result = traverse(evo, target)
                if result is not None:
                    return result
            return None
    
        result = traverse(chain_data, pokemon_name)
        if result is None:
            return False
        return result
    
    # Function to fetch and store Pokémon data locally
    def fetch_and_store_pokemon_data(json_filename):
        st.info("Fetching Pokémon data from the API...")
        pokemon_list = get_pokemon_list()
        all_pokemon_data = []
    
        progress_bar = st.progress(0)
        total_pokemon = len(pokemon_list)
    
        for index, pokemon in enumerate(pokemon_list, start=1):
            pokemon_name = pokemon['name']
            pokemon_url = pokemon['url']
            pokemon_data = get_pokemon_data(pokemon_url)
    
            # Get types
            types = [t['type']['name'] for t in pokemon_data['types']]
    
            # Get base stats
            stats = {stat['stat']['name']: stat['base_stat'] for stat in pokemon_data['stats']}
    
            # Get abilities
            abilities = [a['ability']['name'] for a in pokemon_data['abilities']]
    
            # Get moves
            moves = [move['move']['name'] for move in pokemon_data['moves']]
    
            # Determine if the Pokémon is fully evolved
            final_evo = is_final_evolution(pokemon_name)
    
            # Create a Pokémon entry
            pokemon_entry = {
                'name': pokemon_name,
                'types': types,
                'stats': stats,
                'abilities': abilities,
                'moves': moves,
                'is_final_evolution': final_evo
            }
    
            all_pokemon_data.append(pokemon_entry)
    
            # Update progress bar
            progress_bar.progress(index / total_pokemon)
    
        # Save data to JSON file
        with open(json_filename, 'w') as json_file:
            json.dump(all_pokemon_data, json_file, indent=4)
    
        st.success(f"Data saved to '{json_filename}'")
        return all_pokemon_data
    
    # Function to load data from the local JSON file
    @st.cache_data
    def load_pokemon_data(json_filename):
        with open(json_filename, 'r') as json_file:
            all_pokemon_data = json.load(json_file)
        return all_pokemon_data
    
    # Function to load or fetch Pokémon data
    def get_all_pokemon_data(json_filename):
        if os.path.exists(json_filename):
            all_pokemon_data = load_pokemon_data(json_filename)
            # Check if 'moves' is present in the data; if not, refetch data
            if 'moves' not in all_pokemon_data[0]:
                st.warning("Existing data does not include Pokémon moves. Refetching data...")
                all_pokemon_data = fetch_and_store_pokemon_data(json_filename)
        else:
            all_pokemon_data = fetch_and_store_pokemon_data(json_filename)
        return all_pokemon_data
    
    def main():
        st.title("Pokémon Fusion Optimizer")
    
        # Load data
        json_filename = 'pokemon_data.json'
        all_pokemon_data = get_all_pokemon_data(json_filename)
    
        # Convert data to DataFrame
        data_rows = []
        for pkmn in all_pokemon_data:
            row = {
                'Name': pkmn['name'],
                'Types': pkmn['types'],
                'Abilities': pkmn['abilities'],
                'Moves': pkmn['moves'],
                'IsFinalEvolution': pkmn['is_final_evolution']
            }
            # Add stats
            for stat_name, stat_value in pkmn['stats'].items():
                row[stat_name] = stat_value
            # Calculate Base Stat Total (BST)
            row['BST'] = sum(pkmn['stats'].values())
            data_rows.append(row)
        df = pd.DataFrame(data_rows)
        df.set_index('Name', inplace=True)
    
        # User input for stat weights
        st.sidebar.header("Stat Weights")
        stat_columns = ['hp', 'attack', 'defense', 'special-attack', 'special-defense', 'speed']
        stat_weights = {}
        for stat in stat_columns:
            stat_weights[stat] = st.sidebar.slider(f"Weight for {stat.replace('-', ' ').title()}:", min_value=0.0, max_value=1.0, value=0.5, step=0.01)
    
        # Max BST filter for base Pokémon
        st.sidebar.header("Base Pokémon Filters")
        max_bst = st.sidebar.number_input("Maximum BST for Base Pokémon:", min_value=0, max_value=720, value=720, step=1)
    
        # Stat filters for base Pokémon
        st.sidebar.write("## Base Stat Filters")
        stat_filters = {}
        for stat in stat_columns:
            min_val = st.sidebar.number_input(f"Minimum {stat.replace('-', ' ').title()}:", min_value=0, max_value=255, value=0, step=1, key=f"min_{stat}")
            max_val = st.sidebar.number_input(f"Maximum {stat.replace('-', ' ').title()}:", min_value=0, max_value=255, value=255, step=1, key=f"max_{stat}")
            stat_filters[stat] = (min_val, max_val)
    
        # Type filters for base Pokémon
        st.sidebar.write("## Type Filters for Base Pokémon")
        types_list = sorted({t.title() for types in df['Types'] for t in types})
        selected_types = st.sidebar.multiselect("Select Base Pokémon Types:", types_list)
    
        # Evolution Preferences
        st.sidebar.header("Evolution Preferences")
        evolution_preference = st.sidebar.radio(
            "Select Evolution Preference:",
            options=["Include All Forms", "Only Fully Evolved", "Prioritize Fully Evolved"]
        )
    
        # Number of top fusions to display
        st.sidebar.header("Fusion Options")
        top_k = st.sidebar.number_input("Number of Top Fusions to Display:", min_value=1, max_value=500, value=50, step=1)
    
        # Type filters for fusions (optional)
        st.sidebar.write("## Fusion Type Filters (Optional)")
        fusion_type_inputs = st.sidebar.multiselect("Select Desired Fusion Types (leave empty for no filter):", types_list)
    
        # Ability filters for fusions (optional)
        st.sidebar.write("## Fusion Ability Filters (Optional)")
        # Collect all abilities from the dataset
        abilities_list = sorted({ability.title() for abilities in df['Abilities'] for ability in abilities})
        fusion_ability_inputs = st.sidebar.multiselect("Select Desired Fusion Abilities (leave empty for no filter):", abilities_list)
    
        # Move filters for fusions (optional)
        st.sidebar.write("## Fusion Moveset Filters (Optional)")
        # Collect moves from the dataset
        moves_list = sorted({move.title() for moves in df['Moves'] for move in moves})
        fusion_move_inputs = st.sidebar.multiselect("Select Required Moves (leave empty for no filter):", moves_list)
    
        # Button to compute fusions
        if st.sidebar.button("Compute Best Fusions"):
            with st.spinner("Computing best fusions..."):
                # Filter base Pokémon based on BST
                filtered_df = df[df['BST'] <= max_bst]
    
                # Apply stat filters to base Pokémon
                for stat in stat_columns:
                    min_val, max_val = stat_filters[stat]
                    filtered_df = filtered_df[(filtered_df[stat] >= min_val) & (filtered_df[stat] <= max_val)]
    
                # Apply type filters to base Pokémon
                if selected_types:
                    filtered_df = filtered_df[filtered_df['Types'].apply(lambda types: any(t.title() in selected_types for t in types))]
    
                # Apply evolution preference
                if evolution_preference == "Only Fully Evolved":
                    filtered_df = filtered_df[filtered_df['IsFinalEvolution'] == True]
                elif evolution_preference == "Prioritize Fully Evolved":
                    Evolution_BONUS = 50  # Adjust as needed
    
                if filtered_df.empty:
                    st.warning("No base Pokémon match the specified filters.")
                    return
    
                # Precompute head and body contributions for all filtered Pokémon
                head_contributions = {}
                body_contributions = {}
                for name, row_p in filtered_df.iterrows():
                    stats = row_p[stat_columns]
                    types = row_p['Types']
                    abilities = row_p['Abilities']
                    moves = set(row_p['Moves'])
                    is_final = row_p['IsFinalEvolution']
    
                    # Head contributions
                    head_stats = {
                        'hp': (2/3) * stats['hp'],
                        'attack': (1/3) * stats['attack'],
                        'defense': (1/3) * stats['defense'],
                        'special-attack': (2/3) * stats['special-attack'],
                        'special-defense': (1/3) * stats['special-defense'],
                        'speed': (2/3) * stats['speed']
                    }
                    # Compute weighted head score
                    head_score = sum(head_stats[stat] * stat_weights[stat] for stat in stat_columns)
                    if evolution_preference == "Prioritize Fully Evolved" and is_final:
                        head_score += EVOLUTION_BONUS
                    head_contributions[name] = {'stats': head_stats, 'score': head_score, 'types': types, 'abilities': abilities, 'moves': moves, 'is_final_evolution': is_final}
    
                    # Body contributions
                    body_stats = {
                        'hp': (1/3) * stats['hp'],
                        'attack': (2/3) * stats['attack'],
                        'defense': (2/3) * stats['defense'],
                        'special-attack': (1/3) * stats['special-attack'],
                        'special-defense': (2/3) * stats['special-defense'],
                        'speed': (1/3) * stats['speed']
                    }
                    # Compute weighted body score
                    body_score = sum(body_stats[stat] * stat_weights[stat] for stat in stat_columns)
                    if evolution_preference == "Prioritize Fully Evolved" and is_final:
                        body_score += EVOLUTION_BONUS
                    body_contributions[name] = {'stats': body_stats, 'score': body_score, 'types': types, 'abilities': abilities, 'moves': moves, 'is_final_evolution': is_final}
    
                # Generate all possible head-body pairs
                heads_list = list(head_contributions.keys())
                bodies_list = list(body_contributions.keys())
    
                # Limit the number of pairs to process to prevent overloading
                max_pairs = 50000  # Adjust as needed
                total_pairs = len(heads_list) * len(bodies_list)
                if total_pairs > max_pairs:
                    st.info(f"Processing a sample of {max_pairs} out of {total_pairs} possible fusion pairs to maintain performance.")
                    np.random.seed(42)  # For reproducibility
                    sample_size = min(len(heads_list), int(np.sqrt(max_pairs)))
                    sample_heads = np.random.choice(heads_list, size=sample_size, replace=False)
                    sample_bodies = np.random.choice(bodies_list, size=sample_size, replace=False)
                else:
                    sample_heads = heads_list
                    sample_bodies = bodies_list
    
                possible_pairs = []
    
                # Prepare desired fusion types (if any)
                desired_fusion_types = set([t.title() for t in fusion_type_inputs])
    
                # Prepare desired fusion abilities (if any)
                desired_fusion_abilities = set([a.lower() for a in fusion_ability_inputs])
    
                # Prepare desired fusion moves (if any)
                desired_fusion_moves = set([m.lower() for m in fusion_move_inputs])
    
                # Generate all possible head-body pairs
                for head_name in sample_heads:
                    head_data = head_contributions[head_name]
                    head_types = head_data['types']
                    fusion_t1 = head_types[0].title() if head_types else ''
                    for body_name in sample_bodies:
                        body_data = body_contributions[body_name]
                        body_types = body_data['types']
                        # Fusion T2 calculation
                        if len(body_types) > 1:
                            fusion_t2 = body_types[1].title()
                        else:
                            fusion_t2 = body_types[0].title() if body_types else ''
                        # Avoid duplicate types
                        if fusion_t1 == fusion_t2:
                            fusion_types_set = {fusion_t1}
                            fusion_t2 = ''
                        else:
                            fusion_types_set = {fusion_t1, fusion_t2}
    
                        # Apply fusion type filters (if any)
                        if desired_fusion_types:
                            if desired_fusion_types.issubset(fusion_types_set):
                                pass
                            else:
                                continue  # Skip if the desired types are not a subset of fusion types
                        # Else: No fusion type filters applied
    
                        # Fusion Ability (inherited from body Pokémon)
                        fusion_abilities = [a.lower() for a in body_data['abilities']]
    
                        # Apply fusion ability filters (if any)
                        if desired_fusion_abilities:
                            if not desired_fusion_abilities.intersection(set(fusion_abilities)):
                                continue  # Skip if fusion abilities do not match desired abilities
                        # Else: No fusion ability filters applied
    
                        # Fusion Moveset (combined moves of head and body)
                        fusion_moves = head_data['moves'].union(body_data['moves'])
    
                        # Apply fusion move filters (if any)
                        if desired_fusion_moves:
                            if not desired_fusion_moves.issubset(set([m.lower() for m in fusion_moves])):
                                continue  # Skip if fusion moves do not include all desired moves
                        # Else: No fusion move filters applied
    
                        # Compute total fusion score
                        total_score = head_data['score'] + body_data['score']
    
                        # Collect the pair
                        possible_pairs.append((total_score, head_name, body_name, fusion_t1, fusion_t2, fusion_abilities))
    
                if not possible_pairs:
                    st.warning("No fusions match the specified filters.")
                    return
    
                # Sort possible pairs by total score
                possible_pairs.sort(reverse=True)
    
                # Get top K fusions
                top_fusions = []
                for idx, (fusion_score, head_name, body_name, fusion_t1, fusion_t2, fusion_abilities) in enumerate(possible_pairs):
                    if idx >= top_k:
                        break
    
                    # Compute fusion stats
                    fusion_stats = {}
                    for stat in stat_columns:
                        hs = head_contributions[head_name]['stats'][stat]
                        bs = body_contributions[body_name]['stats'][stat]
                        fusion_stats[stat] = hs + bs
    
                    # Format abilities for display
                    abilities_display = ', '.join([a.title() for a in fusion_abilities])
    
                    fusion_entry = {
                        'Head': head_name.title(),
                        'Body': body_name.title(),
                        'Fusion Score': fusion_score,
                        'Type 1': fusion_t1,
                        'Type 2': fusion_t2,
                        'Abilities': abilities_display,
                    }
                    for stat in stat_columns:
                        fusion_entry[stat.replace('-', ' ').title()] = int(fusion_stats[stat])
                    top_fusions.append(fusion_entry)
    
                if top_fusions:
                    # Convert to DataFrame
                    fusion_df = pd.DataFrame(top_fusions)
                    
                    # Apply stat filters to fusions
                    for stat in stat_columns:
                        min_val, max_val = stat_filters[stat]
                        fusion_df = fusion_df[
                            (fusion_df[stat.replace('-', ' ').title()] >= min_val) &
                            (fusion_df[stat.replace('-', ' ').title()] <= max_val)
                        ]
    
                    if fusion_df.empty:
                        st.warning("No fusions match the specified filters after applying stat filters.")
                    else:
                        fusion_df = fusion_df[['Head', 'Body', 'Type 1', 'Type 2', 'Abilities', 'Fusion Score'] + [stat.replace('-', ' ').title() for stat in stat_columns]]
                        st.write(f"### Top {len(fusion_df)} Fusions:")
                        st.dataframe(fusion_df.reset_index(drop=True))
                else:
                    st.warning("No fusions match the specified filters.")
        else:
            st.write("Adjust the settings and click 'Compute Best Fusions' to find the optimal Pokémon fusions based on your preferences.")
    
    if __name__ == "__main__":
        main()
    

    Additional Recommendations

    1. Optimize API Calls:

      • Caching: Ensure that repeated runs do not re-fetch unchanged data by effectively using caching mechanisms.
      • Batch Processing: If possible, process Pokémon in batches to adhere to PokeAPI's rate limits.
    2. Enhance User Interface:

      • Loading Indicators: Provide clear feedback during long data-fetching operations.
      • Filter Options: Allow users to toggle evolution preferences dynamically.
    3. Performance Improvements:

      • Data Storage: Consider storing the enriched Pokémon data (including evolution status) persistently to avoid repeated API calls.
      • Asynchronous Calls: Implement asynchronous API requests to speed up data fetching (advanced).
    4. Error Handling:

      • Robustness: Enhance error handling to manage scenarios where API responses are incomplete or inconsistent.
      • User Feedback: Inform users of any issues encountered during data fetching or processing.

    By incorporating evolution stage data and adjusting the fusion logic accordingly, your optimizer will more reliably recommend fully-evolved Pokémon, enhancing the quality and relevance of the fusion suggestions.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.