如何从随机路点模型理解这段代码

how to understand this code from Random Waypoint model

我是 C++ 的新手,但这里有一段来自 Random waypoint.This 的代码是它的参考:http://netdb.cis.upenn.edu/rapidnet/doxygen/html/classns3_1_1_random_waypoint_mobility_model.html#a2b8fc7b2cf1e2ffec7e6c6a9d5f7404

Says:This method returns the TypeId associated to ns3::RandomWaypointMobilityModel.

但是我不知道怎么用(更新:这个模型的属性怎么调用)?

TypeId
RandomWaypointMobilityModel::GetTypeId (void)
{
   static TypeId tid = TypeId ("ns3::RandomWaypointMobilityModel")
   .SetParent<MobilityModel> () 
   .SetGroupName ("Mobility") 
   .AddConstructor<RandomWaypointMobilityModel> ()
   .AddAttribute ("Speed",  //name
               "A random variable used to pick the speed of a random waypoint model.",  //help context
               StringValue ("ns3::UniformRandomVariable[Min=0.3|Max=0.7]"),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_speed), //setup(initailize m_speed)
               MakePointerChecker<RandomVariableStream> ()) 
   .AddAttribute ("Pause",  
               "A random variable used to pick the pause of a random waypoint model.", //help context
               StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_pause),
               MakePointerChecker<RandomVariableStream> ())
  .AddAttribute ("PositionAllocator",
               "The position model used to pick a destination point.",
               PointerValue (),
               MakePointerAccessor (&RandomWaypointMobilityModel::m_position),
               MakePointerChecker<PositionAllocator> ());

return tid;
}

既然你说你想理解代码,我想告诉你 ns-3 教程,它解释了他们的属性系统是如何工作的 (https://www.nsnam.org/docs/tutorial/html/index.html),但我会尽量给你一个要点并指出一个例子:

如何使用ns3属性系统

  1. 正如您所做的那样,要设置属性值的第一步是查找 class 文档,该文档告诉您如何使用标记为 'Set with class' 的要点设置属性(即 RandomVariableValue 在你的情况下)
  2. 然后您可以通过 ns3 Helper 创建您 class 感兴趣的对象(此处:RandomWaypointMobilityModel),您可以在其中借助您查找的信息设置属性在第一步中。

一个简短的例子

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

using namespace ns3;

static void 
CourseChange (std::string foo, Ptr<const MobilityModel> mobility)
{
  Vector pos = mobility->GetPosition ();
  Vector vel = mobility->GetVelocity ();
  std::cout << Simulator::Now () << ", model=" << mobility << ", POS: x=" << pos.x << ", y=" << pos.y
            << ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
            << ", z=" << vel.z << std::endl;
}

int main (int argc, char *argv[])
{

  CommandLine cmd;
  cmd.Parse (argc, argv);

  NodeContainer c;
  c.Create (1);

  // Position allocator for the start of the simulation
  MobilityHelper mobility;
  mobility.SetPositionAllocator ("ns3::RandomDiscPositionAllocator",
                                 "X", StringValue ("100.0"),
                                 "Y", StringValue ("100.0"),
                                 "Rho", StringValue ("ns3::UniformRandomVariable[Min=0|Max=30]"));

  // The position allocator that will be used by the RandomWaypointMobilityModel
  GridPositionAllocator posAllocator;
  posAllocator.SetMinX(0.0);
  posAllocator.SetMinY(0.0);
  posAllocator.SetDeltaX(5.0);
  posAllocator.SetDeltaY(5.0);
  posAllocator.SetLayoutType(ns3::GridPositionAllocator::ROW_FIRST);

  mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
                             "PositionAllocator", PointerValue(&posAllocator));

  mobility.InstallAll ();
  Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
                   MakeCallback (&CourseChange));

  Simulator::Stop (Seconds (500.0));

  Simulator::Run ();

  Simulator::Destroy ();
  return 0;
}

在这里您可以看到一个很长的例子,其中完整描述了这些概念是如何结合在一起的: https://www.nsnam.org/docs/tutorial/html/building-topologies.html#building-a-wireless-network-topology