Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
srichakradhar opened this issue Sep 18, 2023 · 3 comments

Comments

@srichakradhar
Copy link

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
Copy link

same error

@FlussKobra55
Copy link

FlussKobra55 commented Feb 26, 2024

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

@Xumou708822
Copy link

在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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants