A new method in pest control

import matplotlib.pyplot as plt
import numpy as np

def create_forest(rows, cols, pest_ratios):
“”“Create a forest with pests based on specified ratios.”“”
forest = np.zeros((rows, cols))

total_cells = rows * cols

for pest_type, ratio in pest_ratios.items():
    num_pests = int(total_cells * ratio)
    pest_locations = np.random.choice(total_cells, num_pests, replace=False)
    for loc in pest_locations:
        row, col = divmod(loc, cols)
        forest[row, col] = pest_type

return forest

def plot_forest(forest, title=‘Forest Scenario’, cmap=‘tab10’):
“”“Plot the forest using Matplotlib.”“”
plt.imshow(forest, cmap=cmap, interpolation=‘nearest’)
plt.title(title)
plt.xlabel(‘Columns’)
plt.ylabel(‘Rows’)

# Add legend
legend_labels = {1: 'Possum', 2: 'Rat', 3: 'Stoat'}
unique_labels = np.unique(forest)
legend_elements = [plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=plt.cm.tab10(i),
                              markersize=10, label=legend_labels[i]) for i in unique_labels if i in legend_labels]

plt.legend(handles=legend_elements, title='Pest Types')

plt.show()

def simulate_content_frequency(forest, mean=0, std=1):
“”“Simulate content frequency to scramble the forest.”“”
scramble_frequency = np.random.normal(mean, std, forest.shape)
return forest + scramble_frequency

def catch_pests(forest, trap_ranges, bait_location=None, trap_open=True):
“”“Implement multiple trapping methods with changing bait frequency.”“”
caught_pests = np.zeros_like(forest)

# Use bait if specified
if bait_location:
    bait_row, bait_col = bait_location
    caught_pests[bait_row, bait_col] = 3  # Mark the bait location

# Check if the trap is open
if trap_open:
    for trap_range in trap_ranges:
        # Iterate through each cell in the forest
        for row in range(forest.shape[0]):
            for col in range(forest.shape[1]):
                if forest[row, col] in [1, 2]:  # Mark possums and rats
                    # Mark the caught pests within the trapping range
                    caught_pests[max(0, row - trap_range):min(forest.shape[0], row + trap_range + 1),
                                 max(0, col - trap_range):min(forest.shape[1], col + trap_range + 1)] = 1

                elif forest[row, col] == 3:  # Mark stoats
                    # Mark the caught stoats within the trapping range
                    caught_pests[max(0, row - trap_range):min(forest.shape[0], row + trap_range + 1),
                                 max(0, col - trap_range):min(forest.shape[1], col + trap_range + 1)] = 2

return caught_pests

Define forest dimensions

rows, cols = 20, 20

Specify the desired pest ratios (adjust as needed)

pest_ratios = {1: 0.05, 2: 0.02, 3: 0.01} # 1: Possum, 2: Rat, 3: Stoat

Create and plot the original forest based on specified ratios

forest_scenario = create_forest(rows, cols, pest_ratios)
plot_forest(forest_scenario, title=‘Original Forest Scenario’, cmap=‘tab10’)

Simulate content frequency to scramble (modify as needed)

forest_with_scrambled_content = simulate_content_frequency(forest_scenario)
print(“Modified Forest with Scrambled Content Frequency:”)
print(forest_with_scrambled_content)

Plot the modified forest scenario

plot_forest(forest_with_scrambled_content, title=‘Modified Forest Scenario’, cmap=‘tab10’)

Implement multiple trapping methods with bait and plot the result

trap_ranges = [3, 4, 5] # Specify the trapping ranges for each trap
bait_location = (10, 10) # Specify the location of the bait

Use multiple trapping methods with changing bait frequency

caught_pests = catch_pests(forest_with_scrambled_content, trap_ranges, bait_location=bait_location)
plot_forest(caught_pests, title=‘Pests Caught by Multiple Trapping Methods with Changing Bait Frequency’, cmap=‘tab10’)