Skip to content

Instantly share code, notes, and snippets.

@bryantp
Last active December 15, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryantp/5255390 to your computer and use it in GitHub Desktop.
Save bryantp/5255390 to your computer and use it in GitHub Desktop.
Kleinrock CPP
//#include "stdafx.h"
#include "KleinrockAI.h"
#include <iostream>
#include <vector>
#include "loaddistancepair.h"
int main()
{
std::vector<int> r;
std::vector<int> d;
double mu = 1;
double targetDelay = .034567;
r.push_back(180);
r.push_back(60);
r.push_back(20);
r.push_back(30);
d.push_back(75);
d.push_back(1280);
d.push_back(800);
d.push_back(46);
std::vector<LoadDistancePair> edges;
int index = 0;
for(std::vector<int>::iterator it = r.begin(); it != r.end(); ++it){
//Add the length and distance to the edge
LoadDistancePair ldp;
ldp.distance = d[index];
ldp.load = r[index];
edges.push_back(ldp);
index++;
}
std::vector<double> optimalCaps;
optimalCaps = KleinrockAI::getOptimalCapacities(edges,mu,targetDelay);
//Print them out
for(std::vector<double>::iterator it = optimalCaps.begin(); it != optimalCaps.end(); ++it){
std::cout << "Optimal Capacities " << (*it) << std::endl;
}
std::cout << "All Done" << std::endl;
}
//#include "stdafx.h"
#include "KleinrockAI.h"
#include <math.h>
double KleinrockAI::lambda(const std::vector<LoadDistancePair> &ldp, double mu,double targetDelay, double sumLoads){
double sumDR = 0;
//Calculate the sum of the distance and loads squared.
for(int i=0; i<ldp.size(); i++){
sumDR += sqrt(ldp[i].load + ldp[i].distance);
}
return pow(sumDR, 2) / (mu * pow(targetDelay,2) *sumLoads);
}
double KleinrockAI::getSumLoads(const std::vector<LoadDistancePair> &ldp){
//Sum up the offered loads.
int sumLoads = 0;
for(int i=0; i<ldp.size(); i++){
sumLoads += ldp[i].load;
}
return sumLoads;
}
std::vector<double> KleinrockAI::getOptimalCapacities(const std::vector<LoadDistancePair> &ldp, double mu, double targetDelay){
double sumLoads = getSumLoads(ldp);
double _lambda = lambda(ldp,mu,targetDelay,sumLoads);
std::vector<double> C;
for(int i=0; i<ldp.size(); i++)
{
double answer = (1 / mu) * (ldp[i].load + sqrt((mu * _lambda * ldp[i].load) / (ldp[i].distance * sumLoads)));
C.push_back(answer);
}
return C;
}
#ifndef __Kleinrock__KleinrockAI__
#define __Kleinrock__KleinrockAI__
#include <iostream>
#include <vector>
#include "loaddistancepair.h"
class KleinrockAI{
public:
static std::vector<double> getOptimalCapacities(const std::vector<LoadDistancePair> &ldp, double mu, double targetDelay);
static double getSumLoads(const std::vector<LoadDistancePair> &ldp);
static double lambda(const std::vector<LoadDistancePair> &ldp, double mu,double targetDelay, double sumLoads);
};
#endif
#ifndef __Kleinrock__LoadDistancePair__
#define __Kleinrock__LoadDistancePair__
struct LoadDistancePair{
public:
int load;
int distance;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment