BilHim / trafficSimulator

A microscopic traffic simulation in Python
MIT License
360 stars 131 forks source link

TypeError: Can't instantiate abstract class Segment with abstract methods compute_dx, compute_dy, compute_x, compute_y #19

Open srichakradhar opened 1 year ago

srichakradhar commented 1 year ago

Environment

Python version: Python3 Operating System: MacOS

Issue

Traceback (most recent call last):
  File "/Users/srichakradhar/Documents/GitHub/trafficSimulator/src/lane_change.py", line 12, in <module>
    sim.create_segment((lane_space/2, length+intersection_size/2), (lane_space/2, intersection_size/2))
  File "/Users/srichakradhar/Documents/GitHub/trafficSimulator/src/trafficSimulator/core/simulation.py", line 36, in create_segment
    seg = Segment(args)
          ^^^^^^^^^^^^^
TypeError: Can't instantiate abstract class Segment with abstract methods compute_dx, compute_dy, compute_x, compute_y

Since abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods, the Segment should stop extending from ABC and the abstract methods need to be removed.

ANGELOANTU7 commented 1 year ago

same error

FlussKobra55 commented 8 months ago

it worked when I deleted the '@abstractmethod' from lines 43, 46, 49 and 52 from segment.py

Xumou708822 commented 2 months ago

在geometry文件夹中增加一个LineSegment.py文件(LineSegment名称可以任意修改),代码为: import numpy as np from .segment import Segment

class LineSegment(Segment): def init(self, points): super().init(points)

确保传入的 points 是两个点(起点和终点)

    if len(points) != 2:
        raise ValueError("LineSegment requires exactly two points: start and end.")
    self.start = np.array(points[0])
    self.end = np.array(points[1])
    self.delta = self.end - self.start

def compute_x(self, t):
    return self.start[0] + t * self.delta[0]

def compute_y(self, t):
    return self.start[1] + t * self.delta[1]

def compute_dx(self, t):
    # 对 t 的一阶导数为 delta 的 x 分量,因为对于直线来说,这个值不变
    return self.delta[0]

def compute_dy(self, t):
    # 对 t 的一阶导数为 delta 的 y 分量,因为对于直线来说,这个值不变
    return self.delta[1]

然后在simulation.py中将from .geometry.segment import Segment修改为from .geometry.LineSegment import LineSegment as Segment