How do I create a loop in omnet++? - c++

How do I create a loop in omnet ++ with a user defined variable.
I tried the following but it gave me an error:
network Network{
parameters:
int n #prompt("enter number") = default(2);
connections:
for i=0..n do { // here the n gives me a syntax error (unexpected NAME, expected {
//do things
}

Have a look at the OMNeT++ manual. The for syntax is without do, e.g.
for i = 0..count-2 {
node[i].port[1] <--> node[i+1].port[0];
}

Related

Module not found on path for interface table module

I am getting the following error
Module not found on path 'GPSSim.satellite[0].interfaceTable' defined by par 'GPSSim.satellite[0].wlan[0].interfaceTableModule' -- in module (inet::InterfaceEntry) GPSSim.satellite[0].wlan[0] (id=51), during network initialization
There are a lot of code but i will show the ones that i believe is important. If more code is required do let me know, thanks!
GPSSIM.ned This file is the network file
network GPSSim
{
parameters:
int numOfSats; // Number of satellites
submodules:
satellite[numOfSats]: GPSSatellite {
parameters:
#display("p=505.835,100.085;r=10,,#707070;i=device/satellite_l");
}
}
GPSSatellite.ned
module GPSSatellite extends StandardSatellite
{
submodules:
gpsApp[numGpsApps]: <default("GPSApp")> like IGPSApp { //default("UdpApp")
parameters:
#display("p=100,284,row,60");
}
}
StandardSatellite.ned
module StandardSatellite extends Satellite
{
parameters:
#node; //because of MobilityBase initialization'
#networkNode();
int numRadios = default(1);
**.interfaceTableModule = default(absPath(".interfaceTable"));
//wlan[*].mgmt.typename = default("Ieee80211MgmtAp");
//wlan[*].interfaceTableModule = default(absPath(".interfaceEntry"));
//wlan[*].mgmtType = default("Ieee80211MgmtAdhoc"); // use adhoc management
submodules:
interfaceTable: InterfaceTable {
parameters:
#display("p=407,69");
}
routingTable: Ipv4RoutingTable {
parameters:
#display("p=45,178");
//IPForward = IPForward;
//forwardMulticast = forwardMulticast;
routingFile = routingFile;
}
wlan[numRadios]: <default("Ieee80211Interface")> like IWirelessInterface {
parameters:
#display("p=301.35,379.05;q=queue");
}
ext[numExtInterfaces]: <default("ExtInterface")> like IExternalInterface {
parameters:
#display("p=217,421,row,10;q=txQueue;i=block/ifcard");
}
networkLayer: NetworkLayerNodeBase {
parameters:
#display("p=226.8,198.45;q=queue");
}
lo0: LoopbackInterface {
#display("p=78,406");
}
App: <default("UdpBasicBurst")> like IApp {
#display("p=469.35,198.45");
}
connections allowunconnected:
wlan[0].upperLayerOut --> networkLayer.radioIn[0];
//ext[0].upperLayerOut --> networkLayer.radioIn[0];
//lo0.upperLayerOut --> networkLayer.radioIn[0];
}
When i put the interface table module in the top level network (GPSSim) instead, i don't get this error but instead i get another error where the interface is already registered which the cause is precisely because i did that, since im telling each instance of satellite to use the same interface table.
I dont' know where to go from here. Any help would be greatly appreciated! Thanks in advance.
Try change the line
**.interfaceTableModule = default(absPath(".interfaceTable"));
into:
interfaceTableModule = default("interfaceTable");
Parameter interfaceTableModule should point to the interface table in that node.

Vibe.D - undefinded identifier (Dlang)

I'm trying to create simple REST api, but when i try to compile my code im getting
frontpage.d(15,3): Error: undefined identifier 'tmp', did you mean
alias 'cmp'?
Here is my code:
module service.frontpage;
import vibe.d;
#path("/api")
interface IFrontPageAPI
{
Json getHome();
}
class FrontPageAPI : IFrontPageAPI
{
this(auto tmp)
{
auto collect = tmp;
}
Json getHome()
{
logInfo("Getting HomePage from DB");
Bson query = Bson(["_id" : Bson("homepage")]);
auto result = collect.find(query);
logInfo("Iterating results...");
foreach (i, doc; result.byPair)
logInfo("Item %d: %s", i, doc.toJson().toString());
return result.toJson();
}
}
Can someone help me with it? tmp is temporary variable to pass mongoDB collection handler.
Same answer as on DLearn.
You need to
- use Class variables
- use types instead auto (here Mongo collection)
- return a proper Json
Have a look at this interactive example - and feel free to play with it. No output means no compilation error.

Indentifier not found in non-program loop script

I have tried to look up the "identifier not found" error only to find posts where the function must be moved outside the main loop or should use a forward declaration. However, my script is used as a module for a game engine thus has no real main loop and the function in question is called by a different function above the failing line with no issues:
// Create a Steam ID
CSteamID Steam::createSteamID(uint32 steamID, int accountType){
CSteamID cSteamID;
if(accountType < 0 || accountType >= k_EAccountTypeMax){
accountType = 1;
}
cSteamID.Set(steamID, EUniverse(k_EUniversePublic), EAccountType(accountType));
return cSteamID;
}
// Set a Steam user as someone recently played with
void Steam::setPlayedWith(int steamID){
if(SteamFriends() == NULL){
return;
}
CSteamID friendID = createSteamID(steamID);
SteamFriends()->SetPlayedWith(friendID);
}
// Get friend's Steam username
String getFriendPersonaName(int steamID){
if(SteamFriends() == NULL || steamID == 0){
return "";
}
CSteamID friendID = createSteamID(steamID);
bool isDataLoading = SteamFriends()->RequestUserInformation(friendID, true);
if(!isDataLoading){
return SteamFriends()->GetFriendPersonaName(friendID);
}
}
The ID creation function sits at the very top and these two functions come much later. The first one (setPlayedWith) succeeds no problem but the second one (getFriendPersonaName) fails with: 'createSteamID': identifier not found when compiling the script.
I'm kind of at a loss and hopefully someone can point me in the right direction.
if getFriendPersonaName() is a member function then you have forgotten to define it the correct way so it will look like:
string Steam::getFriendPersonaName(int steamID)...
if it is not a member then you can't access it. however you can Only access it if getFriendPersonaName() is a friend function where you should edit the signature to:
String getFriendPersonaName(int steamID, const steam& rhs);

Assign randomly chosen, but matching parameters for connected OMNeT++ gates?

I'm developing a network model in OMNeT++ in which I have introduced a custom channel type to represent links in my network. For one property of this channel type's instances, I'd like to assign a random parameter. However, the random number should be the same for connected gates.
My node definition has the following gates definition:
simple GridAgent
{
/* ... other paramters/definitions omitted ... */
gates:
inout agentConnections[];
}
In my network configuration, I connect nodes using the simple <--> syntax, e.g.:
someSwitchyard.agentConnections++ <--> AgentConnectionChannel <--> someWindfarm.agentConnections++;
Now, this AgentConnectionChannel has a property called impedance, which I'd like to randomly assign. This impedance property should be the same for both A -> B and B -> A. I have tried to add { impedance = default(unitform(1, 10)) } to the network definition, as well as putting **.agentConnections$o[*].channel.impedance = uniform(1, 10) into omnetpp.ini. In both cases, however, A -> B has a different value assigned than B -> A.
As indicated on the OMNet++ mailing list, this happens because the <--> syntax is actually a shorthand for creating two distinct connections, hence two drawings from the random number distribution happen.
How can I assign a random parameter to a connection's property and have the same value for both directions of two connected gates? Is there a way to do this in the omnetpp.ini file, or do I need to create a script in, e.g., Perl, Ruby, or Python to generate the omnetpp.ini for my runs?
There is no simple solution of your problem, and it could not be resolved manipulating omnetpp.ini file merely.
I propose manual rewriting a parameter value for the second direction. It requires preparing a C++ class for a channel (which you have probably done).
Assuming that your channel definition in NED is following:
channel AgentConnectionChannel extends ned.DatarateChannel {
#class(AgentConnectionChannel);
double impedance;
}
and in omnetpp.ini you has:
**.agentConnections$o[*].channel.impedance = uniform(1, 10)
you should prepare C++ class AgentConnectionChannel:
class AgentConnectionChannel: public cDatarateChannel {
public:
AgentConnectionChannel() : parAlreadyRewritten(false) {}
void setParAlreadyRewritten() {parAlreadyRewritten=true;}
protected:
virtual void initialize();
private:
bool parAlreadyRewritten;
private:
double impedance;
};
Define_Channel(AgentConnectionChannel);
void AgentConnectionChannel::initialize() {
if (parAlreadyRewritten == false) {
parAlreadyRewritten = true;
cGate * srcOut = this->getSourceGate();
cModule *owner = srcOut->getOwnerModule();
int index = srcOut->isVector() ? srcOut->getIndex() : -1;
cGate *srcIn = owner->gateHalf(srcOut->getBaseName(), cGate::INPUT,
index);
cChannel * channel = srcIn->findIncomingTransmissionChannel();
AgentConnectionChannel * reverseChan =
dynamic_cast<AgentConnectionChannel*>(channel);
if (reverseChan) {
reverseChan->setParAlreadyRewritten();
// assigning a value from forward direction channel
reverseChan->par("impedance") = this->par("impedance");
}
}
// and now read a parameter as usual
impedance = par("impedance").doubleValue();
EV << getFullPath() << ", impedance=" << impedance << endl;
}

How to access decision variables: OPL/Cplex && C++

I am using OPL/Cplex libraries in my c++ code,
In my file.mod I decalred this decision variable:
dvar int+ x[nodes][nodes][1..nb_max][lambdas];
Now I that Cplex solved the model I successfully recuperate the objective value as follows:
try {
IloCplex cplex(env);
cplex.setOut(env.getNullStream());
IloOplErrorHandler handler(env,cout);
IloOplModelSource modelSource(env, "file.mod");
IloOplSettings settings(env,handler);
IloOplModelDefinition def(modelSource,settings);
IloOplModel opl(def,cplex);
IloOplDataSource dataSource(env, "file2.dat");
opl.addDataSource(dataSource);
opl.generate();
if ( cplex.solve() ) {
cout<< opl.getCplex().getObjValue()<< endl;
}
}
My question is how can I recuperate the multidimentional array "x"?
I tried with
IloIntVar x = opl.getElement("x").asIntVar;
IloIntVar xvar =x.get(0);//first item
but the following errors occur!
error: conversion from '' to non-scalar type 'IloIntVar' requested
error: 'class IloIntVar' has no member named 'get'
I am really a beginner in OPL,
Thanks in advance!
you have an answer to this at https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014387796&ps=25
Let me quote David Gravot from ROSTUDEL
dvar float+ Commande[Items,Periodes];
Now, in my JAVA code, I wrote the following to access all solution values for these variables as following :
IloNumMap commandesMap = opl.getElement("Commande").asNumMap();
for(int i=0;i<params.getNbItems();i++) {
for(int t=0;t<params.getNbPeriodes();t++) {
double solValue = commandesMap.getSub(i+1).get(t+1);
log.info("at item "+(i+1)+" periode "+(t+1)+" : "+solValue );
}
}
regards