See

<project-file type=“source”/> <content> #include “netprocess.h” #include “simpathogen.h” #include “node.h” #include “ke_index.h” #include <algorithm>

NetProcess::NetProcess(std::ostream &log, float beta_household, float beta_external,

                     SimDistribution *tlat, SimDistribution *tinf, bool has_immunity,
                     const NetworkStructure &network)
  : SimProcess(log), net(network)

{

  BetaHousehold = beta_household / 2;
  BetaExternal = beta_external / 2;
  HasImmunity = has_immunity;
  Pathogen = new SimPathogen(0);
  SimInfectionStage *latent = NULL;
  if (tlat != NULL) {
      latent = new SimInfectionStage(1, 0, 0, NULL);
      Pathogen->addStage(latent);
  }
  SimInfectionStage *infectious = new SimInfectionStage(2, 1, 0, NULL);
  Pathogen->addStage(infectious);
  if (tlat != NULL)
      Pathogen->setInitialStage(latent, 1);
  else
      Pathogen->setInitialStage(infectious, 1);
  if (tlat != NULL)
      latent->jumpTo(infectious, 1, tlat);
  infectious->jumpTo(NULL, 1, tinf);
  readNetwork();

}

void NetProcess::reset() {

  int N = agents();
  for (int j = 0; j < N; j++)
      node(j)->reset();
  for (int i = 0; i < InitialInfections.size(); i++)
      node(InitialInfections[i])->infect(Pathogen, 0, this);

}

void NetProcess::initialInfections(const std::vector<int> & I0) {

  int N = agents(), nI0 = I0.size();
  InitialInfections = I0;
  for (int i = 0; i < nI0; i++) {
      node(I0[i])->infect(Pathogen, 0, this);
  }

}

void NetProcess::dumpEffectiveDegrees() {

  int M = net.MaxExternalDegree, N = agents();
  if (M == 0) return;
  int m = M * (M + 3) / 2 + 1;
  std::vector<int> S(m), I(m);
  for (int i = 0; i < m; i++) S[i] = I[i] = 0;
  for (int i = 0; i < N; i++) {
      Node *p = node(i);
      int k = p->degree();
      int e = 0;
      int household = net.InHousehold[i];
      int household_size = net.HouseholdSizes[household] - 1;
      for (int j = household_size; j < k; j++)
          if (p->neighbor(j)->infectious())
              e++;
          if (p->infectious())
              I[ke_index(k - household_size, e)]++;
          else
              S[ke_index(k - household_size, e)]++;
  }
  for (int index = 0, i = 0; i <= M; i++)
      for (int j = 0; j <= i; j++)
          logger() << "Initial S : " << i << " : " << j << " : " << S[index++] << std::endl;
  for (int index = 0, i = 0; i <= M; i++)
      for (int j = 0; j <= i; j++)
          logger() << "Initial I : " << i  << " : " << j << " : " << I[index++] << std::endl;

}

void NetProcess::dumpHouseholdCompartments() {

  int M = net.MaxHouseholdSize, N = agents();
  int households = net.HouseholdSizes.size();
  if (M == 0) return;
  int m = M * (M + 3) / 2 + 1;
  std::vector<int> H(m);
  std::vector<int> infected_in_household(households, 0);
  for (int i = 0; i < m; i++) H[i] = 0;
  for (int i = 0; i < N; i++) {
      Node *p = node(i);
      int hh = net.InHousehold[i];
      int household = net.InHousehold[i];
      if (p->infectious()) ++infected_in_household[hh];
  }
  for (int i = 0; i < households; i++)
      H[ke_index(net.HouseholdSizes[i], infected_in_household[i])]++;
  for (int index = 0, i = 0; i <= M; i++)
      for (int j = 0; j <= i; j++)
          logger() << "Initial H : " << i << " : " << j << " : " << H[index++] << std::endl;

}

void NetProcess::dumpNet() {

  int N = agents(), i, j, M = net.MaxDegree;
  Node *n;
  for (i = 0; i < N; i++) {
      n = node(i);
      int k = n->degree();
      logger() << "node : " << n->id() << " :";
      std::vector<int> neighbors;
      for (int j = 0; j < k; j++)
          neighbors.push_back(n->neighbor(j)->id());
      std::sort(neighbors.begin(), neighbors.end());
      for (std::vector<int>::iterator ineigbor = neighbors.begin(); ineigbor != neighbors.end(); ineigbor++)
          logger()  << " " << *ineigbor;
      for (int j = k; j < M; j++)
          logger()  << " " << 0;
      logger() << std::endl;
  }

}

void NetProcess::readNetwork() {

  Agents.reserve(net.nodes());
  Node *p;
  int i, j, k;
  int N = net.nodes();
  for (i = 0; i < N; i++) {
      p = new Node(i+1, HasImmunity);
      registerAgent(p);
      addAgent(p);
  }
  int neighbor;
  for (i = 0; i < N; i++) {
      k = net.degree(i);
      for (j = 0; j < k; j++) {
          neighbor = net.neighbor(i, j);
          node(i)->link(node(neighbor), 0, (net.isInternal(i, j)) ? BetaHousehold : BetaExternal);
      }
  }

}

void NetProcess::countAffectedHouseholds() {

  int N = agents(), m = net.HouseholdStarts.size();
  std::vector<int> hh(m, 0);
  for (int i = 0; i < N; i++)
      if (node(i)->infected()) hh[net.InHousehold[i]] = 1;
  int count = 0;
  for (int i = 0; i < m; i++)
      count += hh[i];
  logger() << "Count : "<<count<<std::endl;

} </content> <use name=“netprocess.h”/> <use name=“node.h”/>