You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NS3仿真运行报错:ns3::UniformRandomRectanglePositionAllocator未找到求助

问题:NS3运行报错“ns3::UniformRandomRectanglePositionAllocator not found”

我运行一段NS3代码时触发断言错误,错误信息如下:

assert failed. cond="uid != 0", msg="Assert in TypeId::LookupByName: ns3::UniformRandomRectanglePositionAllocator not found", +0.000000000s -1 file=../src/core/model/type-id.cc, line=834

对应的代码:

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"

using namespace ns3;

int main (int argc, char *argv[])
{
  // Create nodes
  uint32_t nNodes = 10; // Number of nodes
  NodeContainer nodes;
  nodes.Create (nNodes);

  // Create a 2D space for the nodes
  MobilityHelper mobility;
  mobility.SetPositionAllocator ("ns3::UniformRandomRectanglePositionAllocator",
                                 "X", DoubleValue (0.0),
                                 "Y", DoubleValue (0.0),
                                 "Width", DoubleValue (100.0),
                                 "Height", DoubleValue (100.0));
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (nodes);

  // Create the CSMA/CA protocol
  CsmaHelper csma;
  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));

  // Install the CSMA/CA protocol on the nodes
  NetDeviceContainer devices = csma.Install (nodes);

  // Run the simulation
  Simulator::Stop (Seconds (10.0));
  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}

解决方法

  • 版本适配:替换位置分配器名称
    UniformRandomRectanglePositionAllocator 在NS3 3.35及以后的版本中已被重命名为 UniformRandomBoxPositionAllocator,且需要新增Z轴相关参数。修改后的位置分配器代码如下:

    mobility.SetPositionAllocator ("ns3::UniformRandomBoxPositionAllocator",
                                   "X", DoubleValue (0.0),
                                   "Y", DoubleValue (0.0),
                                   "Z", DoubleValue (0.0),
                                   "Width", DoubleValue (100.0),
                                   "Height", DoubleValue (100.0),
                                   "Depth", DoubleValue (0.0));
    
  • 确认mobility模块已启用
    如果使用旧版本NS3,需确保编译时已启用mobility模块。执行以下命令重新配置编译:

    ./waf configure --enable-modules=mobility
    ./waf build
    
  • 检查编译依赖
    手动编译时,确保链接阶段正确关联了mobility模块的库文件,避免因依赖缺失导致类型无法找到。

内容的提问来源于stack exchange,提问作者gushu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 16:52:37