NS3 (3.37) Python

59 views
Skip to first unread message

tamuct ccilab

unread,
Oct 18, 2023, 6:06:38 PM10/18/23
to ns-3-users
I am trying to convert the manet-routing-compare.cc to a python code for educational purpose.
I am stuck here at line 306:
Ptr<PositionAllocator> taPositionAlloc = pos.Create()->GetObject<PositionAllocator>();

What i did is:
pos = ns.core.ObjectFactory()
pos.SetTypeId("ns3::RandomRectanglePositionAllocator")
pos.Set("X", ns.core.StringValue("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"))
pos.Set("Y", ns.core.StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"))

taPositionAlloc = pos.Create()

streamIndex += taPositionAlloc.AssignStreams(streamIndex)

ssSpeed = f"ns3::UniformRandomVariable[Min=0.0|Max={nodeSpeed}]"
ssPause = f"ns3::ConstantRandomVariable[Constant={nodePause}]"

mobilityAdhoc.SetMobilityModel("ns3::RandomWaypointMobilityModel",
"Speed",
ns.core.StringValue(ssSpeed),
"Pause",
ns.core.StringValue(ssPause),
"PositionAllocator",
ns.core.PointerValue(taPositionAlloc))
mobilityAdhoc.SetPositionAllocator(taPositionAlloc)


This gives me the error:
TypeError: Template method resolution failed:
  void ns3::MobilityHelper::SetPositionAllocator(ns3::Ptr<ns3::PositionAllocator> allocator) =>
    TypeError: could not convert argument 1
  Failed to instantiate "SetPositionAllocator(ns3::Ptr<ns3::Object>&)"
  Failed to instantiate "SetPositionAllocator(ns3::Ptr<ns3::Object>*)"
  Failed to instantiate "SetPositionAllocator(ns3::Ptr<ns3::Object>)"


I know Type conversion is not as straight forward in python. Is there any workaround?
How can i get the PositionAllocator object in python?

Gabriel Ferreira

unread,
Oct 18, 2023, 8:23:32 PM10/18/23
to ns-3-users
Use 

taPositionAlloc = ns.CreateObject("RandomRectanglePositionAllocator")
taPositionAlloc.SetAttribute("X", ns.StringValue("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"))
taPositionAlloc.SetAttribute("Y", ns.StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"))
mobilityAdhoc.SetPositionAllocator (taPositionAlloc)


You can see more examples in the following link
https://github.com/Gabrielcarvfer/ns3_for_education/blob/master/100_network_topologies_and_mobility.ipynb

tamuct ccilab

unread,
Oct 19, 2023, 11:11:41 AM10/19/23
to ns-3-users
Thank you.
Reply all
Reply to author
Forward
0 new messages