Log In | Get Help   
Home My Page Projects Code Snippets Project Openings Mareframe
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files
[mareframe] View of /trunk/gadget/migrationpenalty.cc
[mareframe] / trunk / gadget / migrationpenalty.cc Repository:
ViewVC logotype

View of /trunk/gadget/migrationpenalty.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (download) (annotate)
Mon Feb 10 17:09:07 2014 UTC (10 years, 4 months ago) by agomez
File size: 2857 byte(s)
Initial version based on Gadget 2.2.00
#include "migrationpenalty.h"
#include "errorhandler.h"
#include "readword.h"
#include "gadget.h"
#include "global.h"

MigrationPenalty::MigrationPenalty(CommentStream& infile, double weight, const char* name)
  : Likelihood(MIGRATIONPENALTYLIKELIHOOD, weight, name), powercoeffs(2) {

  char text[MaxStrLength];
  stockname = new char[MaxStrLength];
  strncpy(text, "", MaxStrLength);
  strncpy(stockname, "", MaxStrLength);

  readWordAndValue(infile, "stockname", stockname);
  readWordAndTwoVariables(infile, "powercoeffs", powercoeffs[0], powercoeffs[1]);

  //prepare for next likelihood component
  infile >> ws;
  if (!infile.eof()) {
    infile >> text >> ws;
    if (strcasecmp(text, "[component]") != 0)
      handle.logFileUnexpected(LOGFAIL, "[component]", text);
  }
}

void MigrationPenalty::addLikelihood(const TimeClass* const TimeInfo) {
  if (TimeInfo->getTime() != TimeInfo->numTotalSteps())
    return;

  if (handle.getLogLevel() >= LOGMESSAGE)
    handle.logMessage(LOGMESSAGE, "Calculating likelihood score for migrationpenalty component", this->getName());

  int i;
  double l = 0.0;
  if (stock->doesMigrate()) {
    DoubleVector penalty(stock->getMigration()->getPenalty());
    for (i = 0; i < penalty.Size(); i++)
      l += pow(penalty[i], powercoeffs[0]);
    l = pow(l, powercoeffs[1]);
    likelihood += l;
  }
  if (handle.getLogLevel() >= LOGMESSAGE)
    handle.logMessage(LOGMESSAGE, "The likelihood score for this component on this timestep is", l);
}

void MigrationPenalty::setFleetsAndStocks(FleetPtrVector& Fleets, StockPtrVector& Stocks) {
  int i;
  int found = 0;
  for (i = 0; i < Stocks.Size(); i++)
    if (strcasecmp(stockname, Stocks[i]->getName()) == 0) {
      found++;
      stock = Stocks[i];
    }

  if (found != 1)
    handle.logMessage(LOGFAIL, "Error in migrationpenalty - failed to match stock", stockname);
  if (!(stock->doesMigrate()))
    handle.logMessage(LOGWARN, "Warning in migrationpenalty - stock doesnt migrate");
}

MigrationPenalty::~MigrationPenalty() {
  delete[] stockname;
}

void MigrationPenalty::Reset(const Keeper* const keeper) {
  Likelihood::Reset(keeper);
  if (handle.getLogLevel() >= LOGMESSAGE)
    handle.logMessage(LOGMESSAGE, "Reset migrationpenalty component");
}

void MigrationPenalty::printSummary(ofstream& outfile) {
  //JMB there is only one likelihood score here ...
  if (!(isZero(likelihood))) {
    outfile << "all   all        all" << sep << setw(largewidth) << this->getName() << sep
      << setprecision(smallprecision) << setw(smallwidth) << weight << sep
      << setprecision(largeprecision) << setw(largewidth) << likelihood << endl;
    outfile.flush();
  }
}

void MigrationPenalty::Print(ofstream& outfile) const {
  outfile << "\nMigration Penalty " << this->getName() << " - likelihood value "
    << likelihood << endl;
  outfile.flush();
}

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge