Пример #1
0
int main(int ac, char **av) {
  el::Configurations confFromFile("./rpdemo-logger.conf");
  el::Loggers::reconfigureAllLoggers(confFromFile);

  using KBase::ReportingLevel;
  using KBase::PRNG;
  using KBase::MtchPstn;
  using KBase::dSeed;
  using RfrmPri::RPModel;
  using RfrmPri::RPState;
  using RfrmPri::RPActor;
  //using RfrmPri::printPerm;

  auto sTime = KBase::displayProgramStart(RfrmPri::appName, RfrmPri::appVersion);
  uint64_t seed = dSeed; // arbitrary;
  bool siP = true;
  bool cpP = false;
  bool runP = true;
  unsigned int sNum = 1;
  bool xmlP = false;
  bool rp2P = false;
  std::string inputXML = "";

  auto showHelp = [sNum]() {
    printf("\n");
    printf("Usage: specify one or more of these options\n");
    printf("\n");
    printf("--cp              start all actors from the central position \n");
    printf("--rp2             create RP2 objects \n");
    printf("--si              start each actor from their most self-interested position \n");
    printf("                  If neither si nor cp are specified, it will use si. \n");
    printf("                  If both si and cp are specified, it will use second specified. \n");
    printf("--help            print this message and exit \n");
    printf("--seed <n>        set a 64bit seed \n");
    printf("                  0 means truly random \n");
    printf("                  default: %020llu \n", dSeed);
    printf("--sNum <n>        choose a scenario number \n");
    printf("                  default: %u \n", sNum);
    printf("                  0: random example, potentially VERY large \n");
    printf("                  1: hard-coded example, moderate size \n");
    printf("--xml <f>         read XML scenario from a given file \n");
    printf("\n");
    printf("For example, rpdemo --si  --xml rpdata.xml, would read the file rpdata.xml \n");
    printf("and start all actors from self-interested positions.\n");
    printf("\n");
    printf("If both scenario number and XML file are specified, it will use only the XML.\n");
    printf("\n");
    printf("If neither scenario number nor XML file are specified, \n");
    printf("it will run a hard-coded example, as if --sNum 1 had been specified.\n");
    printf("\n");
  };

  // a list of <keyword, description, λ-fn>
  // might be enough to do this - except for the arguments to options.
  if (ac > 1) {
    for (int i = 1; i < ac; i++) {
      if (strcmp(av[i], "--seed") == 0) {
        i++;
        seed = std::stoull(av[i]);
      }
      else if (strcmp(av[i], "--sNum") == 0) {
        i++;
        sNum = atoi(av[i]);
      }
      else if (strcmp(av[i], "--xml") == 0) {
        xmlP = true;
        i++;
        inputXML = av[i];
      }
      else if (strcmp(av[i], "--rp2") == 0) {
        rp2P = true;
      }
      else if (strcmp(av[i], "--si") == 0) {
        cpP = false;
        siP = true;
      }
      else if (strcmp(av[i], "--cp") == 0) {
        siP = false;
        cpP = true;
      }
      else if (strcmp(av[i], "--help") == 0) {
        runP = false;
      }
      else {
        runP = false;
        printf("Unrecognized argument: %s\n", av[i]);
      }
    }
  }
  else {
    runP = false;
  }

  if (!runP) {
    showHelp();
    return 0;
  }


  if (0 == seed) {
    PRNG * rng = new PRNG();
    seed = rng->setSeed(0); // 0 == get a random number
    delete rng;
    rng = nullptr;
  }
  LOG(INFO) << KBase::getFormattedString("Using PRNG seed:  %020llu", seed);
  LOG(INFO) << KBase::getFormattedString("Same seed in hex:   0x%016llX", seed);
  // Unix correctly prints all digits with lu, lX, llu, and llX.
  // Windows only prints part, with lu, lX, llu, and llX.


  const bool parP = KBase::testMultiThreadSQLite(false, KBase::ReportingLevel::Medium);  
    if (parP) {
      LOG(INFO) << "Can continue with multi-threaded execution";
    }
    else {
      LOG(INFO) << "Must continue with single-threaded execution";
    }

  if (rp2P) {
    RfrmPri2::rp2Creation(seed);
    return 0;
  }

  auto rpm = new RPModel("", seed);
  if (xmlP) {
    rpm->readXML(inputXML);
    LOG(INFO) << "done reading XML";
  }
  else {
    switch (sNum) {
    case 0:
    case 1:
      rpm->initScen(sNum);
      break;

    default:
      LOG(INFO) << "Unrecognized scenario number" << sNum;
      assert(false);
      break;
    }
  }

  unsigned int numA = rpm->numAct; // the actors are as yet unnamed
  unsigned int numR = rpm->numItm;
  //unsigned int numC = rpm->numCat;

  auto rps0 = new RPState(rpm);
  rpm->addState(rps0);

  // NOTE WELL: Records each actor's most self-interested position, but does not set them.
  // Further, it appends Central Position after the last actor position
  auto siPstns = RfrmPri::scanAllPossiblePositions(rpm);
  assert(numA + 1 == siPstns.size());
  const KBase::VUI bestPerm = siPstns[numA];
  assert(numR == bestPerm.size());
  assert(numA == rps0->pstns.size()); // pre-allocated by constructor, all nullptr's

  // Move them all to either the CP or to SI positions which
  // maximize their direct utility, regardless of expected utility.
  for (unsigned int i = 0; i < numA; i++) {
    auto pi = new  MtchPstn();
    pi->numCat = numR;
    pi->numItm = numR;
    if (cpP) {
      pi->match = bestPerm;
    }
    else {
      pi->match = siPstns[i];
    }
    rps0->pstns[i] = pi;
  }
  assert(numA == rps0->pstns.size());

  rps0->step = [rps0]() {
    return rps0->stepSUSN();
  };
  unsigned int maxIter = 100;
  rpm->stop = [maxIter, rpm](unsigned int iter, const KBase::State * s) {
    bool doneP = iter > maxIter;
    if (doneP) {
      LOG(INFO) << "Max iteration limit of" << maxIter << "exceeded";
    }
    auto s2 = ((const RPState *)(rpm->history[iter]));
    for (unsigned int i = 0; i < iter; i++) {
      auto s1 = ((const RPState *)(rpm->history[i]));
      if (RPModel::equivStates(s1, s2)) {
        doneP = true;
        LOG(INFO) << "State number" << iter << "matched state number" << i;
      }
    }

    return doneP;
  };

  rps0->setUENdx();
  rpm->run();

  // we already displayed each state as it was processed,
  // so there is no need to show it again
  //rpm->showHist();


  delete rpm; // and actors, and states
  rpm = nullptr;
  rps0 = nullptr;

  KBase::displayProgramEnd(sTime);

  return 0;
}
Пример #2
0
int main(int ac, char **av) {
  using KBase::ReportingLevel;
  using KBase::PRNG;
  using KBase::MtchPstn;
  using KBase::dSeed;
  using RfrmPri::RPModel;
  using RfrmPri::RPState;
  using RfrmPri::RPActor;
  using RfrmPri::printPerm;

  auto sTime = KBase::displayProgramStart(RfrmPri::appName, RfrmPri::appVersion);
  uint64_t seed = dSeed; // arbitrary;
  bool siP = true;
  bool cpP = false;
  bool runP = true;
  unsigned int sNum = 1;
  bool xmlP = false;
  bool rp2P = false;
  string inputXML = "";

  auto showHelp = [sNum]() {
    printf("\n");
    printf("Usage: specify one or more of these options\n");
    printf("\n");
    printf("--cp              start all actors from the central position \n");
    printf("--rp2             create RP2 objects \n");
    printf("--si              start each actor from their most self-interested position \n");
    printf("                  If neither si nor cp are specified, it will use si. \n");
    printf("                  If both si and cp are specified, it will use second specified. \n");
    printf("--help            print this message and exit \n");
    printf("--seed <n>        set a 64bit seed \n");
    printf("                  0 means truly random \n");
    printf("                  default: %020llu \n", dSeed);
    printf("--sNum <n>        choose a scenario number \n");
    printf("                  default: %u \n", sNum);
    printf("--xml <f>         read XML scenario from a given file \n");
    printf("\n");
    printf("For example, rpdemo --si  --xml rpdata.xml, would read the file rpdata.xml \n");
    printf("and start all actors from self-interested positions.\n");
    printf("\n");
    printf("If both scenario number and XML file are specified, it will use only the XML.\n");
    printf("\n");
    printf("If neither scenario number nor XML file are specified, \n");
    printf("it will run a hard-coded example, as if --sNum 1 had been specified.\n");
    printf("\n");
  };

  // a list of <keyword, description, λ-fn>
  // might be enough to do this - except for the arguments to options.
  if (ac > 1) {
    for (int i = 1; i < ac; i++) {
      if (strcmp(av[i], "--seed") == 0) {
        i++;
        seed = std::stoull(av[i]);
      }
      else if (strcmp(av[i], "--sNum") == 0) {
        i++;
        sNum = atoi(av[i]);
      }
      else if (strcmp(av[i], "--xml") == 0) {
        xmlP = true;
        i++;
        inputXML = av[i];
      }
      else if (strcmp(av[i], "--rp2") == 0) {
        rp2P = true;
      }
      else if (strcmp(av[i], "--si") == 0) {
        cpP = false;
        siP = true;
      }
      else if (strcmp(av[i], "--cp") == 0) {
        siP = false;
        cpP = true;
      }
      else if (strcmp(av[i], "--help") == 0) {
        runP = false;
      }
      else {
        runP = false;
        printf("Unrecognized argument: %s\n", av[i]);
      }
    }
  }
  else {
    runP = false;
  }

  if (!runP) {
    showHelp();
    return 0;
  }


  if (0 == seed) {
    PRNG * rng = new PRNG();
    seed = rng->setSeed(0); // 0 == get a random number
    delete rng;
    rng = nullptr;
  }
  printf("Using PRNG seed:  %020llu \n", seed);
  printf("Same seed in hex:   0x%016llX \n", seed);
  // Unix correctly prints all digits with lu, lX, llu, and llX.
  // Windows only prints part, with lu, lX, llu, and llX.

  if (rp2P) {
    RfrmPri2::rp2Creation(seed);
    return 0;
  }

  auto rpm = new RPModel("", seed);
  if (xmlP) {
    rpm->readXML(inputXML);
    cout << "done reading XML" << endl << flush;
  }
  else {
    switch (sNum) {
    case 0:
      rpm->initScen(sNum);
      break;
    case 1:
      rpm->initScen(sNum);
      break;

    default:
      cout << "Unrecognized scenario number " << sNum << endl << flush;
      assert(false);
      break;
    }
  }

  unsigned int numA = rpm->numAct; // the actors are as yet unnamed
  unsigned int numR = rpm->numItm;
  //unsigned int numC = rpm->numCat;

  auto rps0 = new RPState(rpm);
  rpm->addState(rps0);

  auto pstns = RfrmPri::scanPositions(rpm);
  KBase::VUI bestPerm = pstns[numA];
  assert(numR == bestPerm.size());

  // Either start them all at the CP or have each to choose an initial position which
  // maximizes their direct utility, regardless of expected utility.
  for (unsigned int i = 0; i < numA; i++) {
    auto pi = new  MtchPstn();
    pi->numCat = numR;
    pi->numItm = numR;
    if (cpP) {
      pi->match = bestPerm;
    }
    if (siP) {
      pi->match = pstns[i];
    }
    rps0->addPstn(pi);
  }
  assert(numA == rps0->pstns.size());

  rps0->step = [rps0]() {
    return rps0->stepSUSN();
  };
  unsigned int maxIter = 100;
  rpm->stop = [maxIter, rpm](unsigned int iter, const KBase::State * s) {
    bool doneP = iter > maxIter;
    if (doneP) {
      printf("Max iteration limit of %u exceeded \n", maxIter);
    }
    auto s2 = ((const RPState *)(rpm->history[iter]));
    for (unsigned int i = 0; i < iter; i++) {
      auto s1 = ((const RPState *)(rpm->history[i]));
      if (RPModel::equivStates(s1, s2)) {
        doneP = true;
        printf("State number %u matched state number %u \n", iter, i);
      }
    }

    return doneP;
  };

  rps0->setUENdx();
  rpm->run();

  // we already displayed each state as it was processed,
  // so there is no need to show it again
  //rpm->showHist();


  delete rpm; // and actors, and states
  rpm = nullptr;
  rps0 = nullptr;

  KBase::displayProgramEnd(sTime);

  return 0;
}