Simulating One-Dimensional Movement of Colored Robots with Random Iterations and Boundary Handling in Python Code

Simulating One-Dimensional Movement of Colored Robots with Random Iterations and Boundary Handling in Python Code

Question: Create a class where the object represents a robot, which moves in one dimension within a line segment of 100 meters. Such robots can have two colors, red and yellow.

  • Robots move by iterations. The length of their movement in one iteration is a random number, which is between 20 and 40 meters for red robots and between 30 and 50 meters for yellow robots.
  • On each iteration, once a robot, while moving in a certain direction, reaches an end of the line segment, it turns in the opposite direction and moves the rest of its movement length (selected for the iteration) in that direction.
  • Create an array of 150 robots, 50 of which are red and 100, yellow. The initial location of each robot and movement direction are selected at random.
    Make 200 iterations, where on each iteration all the 150 robots move as described above.
  • After that, select all robots that are located in the first half of the segment, i.e., in the subsegment [0, 50]. Count them and remove all other robots from the array. Save location of all remaining robots in a text file.
  • Use a lambda function when convenient; at least once.
  • Write as much code as you can in 25 minutes.

Python Code:

				
					import random

class Robot:
    def __init__(self, color, position, direction):
        self.color = color
        self.position = position
        self.direction = direction

    def move(self):
        if self.color == "red":
            move_distance = random.uniform(20, 40)
        else:  # yellow robot
            move_distance = random.uniform(30, 50)
        
        new_position = self.position + self.direction * move_distance

        # Handle boundary conditions
        if new_position > 100:  # reached the end of the segment
            move_overflow = new_position - 100
            new_position = 100 - move_overflow
            self.direction = -1  # change direction
        elif new_position < 0:  # reached the beginning of the segment
            move_overflow = 0 - new_position
            new_position = move_overflow
            self.direction = 1  # change direction

        self.position = new_position

def create_robots():
    robots = []
    for _ in range(50):
        robots.append(Robot("red", random.uniform(0, 100), random.choice([-1, 1])))
    for _ in range(100):
        robots.append(Robot("yellow", random.uniform(0, 100), random.choice([-1, 1])))
    return robots

def run_simulation(robots, iterations=200):
    for _ in range(iterations):
        for robot in robots:
            robot.move()

def filter_robots(robots):
    return list(filter(lambda r: r.position <= 50, robots))

def save_remaining_robots(robots, filename="remaining_robots.txt"):
    with open(filename, "w") as f:
        for robot in robots:
            f.write(f"Color: {robot.color}, Position: {robot.position}\n")

# Main execution
robots = create_robots()  # Step 1: Create the robots

run_simulation(robots, 200)  # Step 2: Run the simulation for 200 iterations

robots_in_first_half = filter_robots(robots)  # Step 3: Filter robots in the first half

save_remaining_robots(robots_in_first_half)  # Step 4: Save remaining robots' positions


# done
 
				
			

Explanation:

  1. Robot Class: The Robot class has attributes for color, position, and direction of movement. It has a move method, which moves the robot and handles boundary conditions.

  2. create_robots: Initializes an array of 150 robots, with 50 red and 100 yellow. Positions and movement directions are randomly assigned.

  3. run_simulation: Runs 200 iterations, where all robots move.

  4. filter_robots: Uses a lambda function to filter robots whose positions are less than or equal to 50 meters.

  5. save_remaining_robots: Saves the positions of the remaining robots in a text file.

The code uses a lambda function for filtering robots based on position.