コード例 #1
0
  private static void initFrequencys(Model parent, SuperFrequencyTrace frequencys) {
    // assume frequecys is set to end.
    PopulationSizeModel[] sizes = parent.getPopulationSizeModels();
    double total = 0;
    for (int d = 0; d < sizes.length; d++) {
      total += sizes[d].populationSize(0); // assuming homogenous time
      // model.
    }
    double pick = RandomGenerator.getRandom().nextDouble() * total;
    int d = 0;
    total = 0;
    for (int i = 0; i < sizes.length; i++) {
      total += sizes[d].populationSize(0);
      if (total >= pick) {
        d = i;
        break;
      }
    }
    int choosen = parent.getModelHistory().getSeedDeme();
    if (choosen > -1) d = choosen;
    double[] freqs = new double[sizes.length]; // demeCount;
    freqs[d] =
        1.0 / (parent.getPopulationSizeModels()[d].populationSize(parent.getEndTime())); // FIXME

    frequencys.setFrequencys(freqs);
    // System.out.println("INITF:"+Arrays.toString(freqs));
  }
コード例 #2
0
  public List<ModelEvent> forwardSimulator(
      Model model,
      ModelHistroy modelHistory,
      SelectionStrengthModel[] ssm,
      SuperFrequencyTrace frequencys) {
    // first init.
    // System.err.println("Did we even call me?");
    //		if (model.getEndTime() == Long.MAX_VALUE) {
    //			// for now no initial frequencys are set.
    //			initFrequencys(model, frequencys);
    //		}
    List<ModelEvent> events = new ArrayList<ModelEvent>();
    ndeme = model.getDemeCount();
    // int end=frequencys[0][0].length - 1;
    // int currentGen =frequencys[0][0].length - 1;// we count down: Always.
    // int genCount=0;

    afterSelectionA = new double[ndeme];
    afterSelectiona = new double[ndeme];

    totalMRates = model.getTotalMigrationRates();
    migrationDirections = model.getMigrationDirectionsByDeme();
    migrationRates = model.getMigrationRatesByDeme();
    mu = modelHistory.getForwardAlleleMutationRate();
    nu = modelHistory.getBackAlleleMutationRate();
    sizes = model.getPopulationSizeModels();

    FrequencyCondition stopping = modelHistory.getTimedCondition();
    RestartCondition restartCondition = modelHistory.getRestartCondtion();
    // System.err.println("Stop:"+stopping+"\tstart:"+restartCondition);
    // boolean zeroFlag =true;
    // boolean fixFlag =true;

    // so we have 2 basic "modes" Condtion on fixation in time homegenous mode. Start with
    // defined conditions.
    // in the first case time is not important and we can assume time always ==
    // model.getStartTime();
    // in the second case we are bounded on the interval start time to end time. We assume that
    // our lovely
    // frequency trace data struct is allocated in such a way that this will work in both cases.
    // in both cases we have just one important difference. We don't add a selection end event
    // till we know about the
    // selelction startEvent for the first case. Otherwise we do everything quite linear like.
    // for now lets just fork to 2 different methods
    // right here....
    frequencys.reset();
    boolean fixationConidtion = model.getEndTime() == Long.MAX_VALUE;
    if (fixationConidtion) {
      // System.err.println("We are doing it baby");
      double time = model.getStartTime(); // we use this time for the whole thing. Time
      // invaraint right!
      frequencys.setIndexMostPastward(); // start at the very back.
      initFrequencys(model, frequencys);
      double[] thisGen = frequencys.getFrequencys(null);
      double[] nextGen = new double[thisGen.length];
      while (true) {
        // System.err.println("Oh yea!"+Arrays.toString(thisGen));
        selectionSimulationStep(thisGen, nextGen, ssm, time);
        if (restartCondition.isMeet(nextGen, ndeme)) {
          // System.out.println("Restart?");
          return null; // bubble up the call stack to where we need it.
        }
        // copy data back.
        frequencys.moveForward();
        frequencys.setFrequencys(nextGen);
        // check if we are done.
        // System.err.println("Stopping?"+stopping.isMeet(nextGen));
        if (stopping.isMeet(nextGen)) {
          // System.err.println("We Stoped Doing it:"+Arrays.toString(nextGen)+"\t"+stopping);
          // shift and stop
          frequencys.setCurrentIndexAsStart(); // good luck with that!
          // add events
          // System.err.println("TRACE:"+frequencys);
          events.add(new SelectionEndTimeEvent(frequencys.getTimeMostPastward()));
          events.add(
              new SelectionStartEvent(stopping.getTime(), nextGen)); // well we already new That!
          frequencys.setEndTime();
          // System.err.println(events+"\t"+frequencys.getEndTime()+"\tPastward:"+frequencys.getTimeMostPastward());
          return events; // get me out here.
        }
        if (restartCondition.isMeet(nextGen, ndeme)) {
          frequencys.setIndexMostPastward(); // start at the very back.
          initFrequencys(model, frequencys);
          frequencys.getFrequencys(thisGen);
          // System.err.println("START AGAIN:"+Arrays.toString(nextGen)+"\t"+stopping);
          continue;
        }
        double[] tmp = thisGen;
        thisGen = nextGen; // no need for a copy
        nextGen = tmp; // copied over. We hope!
      }
    }
    // so now we have a much simpler task of starting and finishing things at fixed times.
    // first we assume that inital conditions are specified and already done.
    frequencys.setIndexMostPastward();
    long time = frequencys.getIndexTime();

    assert time == model.getEndTime() : time + "\t" + model.getEndTime();
    double[] thisGen = frequencys.getFrequencys(null);
    double[] nextGen = new double[thisGen.length];
    // System.out.println("StartLoop 2 "+restartCondition.isMeet(thisGen, ndeme));
    while (frequencys.hasMoreForward()) {
      selectionSimulationStep(thisGen, nextGen, ssm, time);
      frequencys.moveForward();
      frequencys.setFrequencys(nextGen);
      time = frequencys.getIndexTime();

      // System.out.println("restart.."+restartCondition.isMeet(nextGen,
      // ndeme)+"\t"+Arrays.toString(nextGen)+"\t"+ndeme+"\ttime:"+time);
      if (restartCondition != null && restartCondition.isMeet(nextGen, ndeme)) {
        return null; // bubble  the restart up the stack.
      }
      // System.err.println("Stopping?"+stopping.isMeet(nextGen));
      if (stopping != null && stopping.isMeet(nextGen)) {
        events.add(new SelectionStartEvent(time, thisGen));
        // System.out.println("Stop:"+events+"\t"+Arrays.toString(nextGen)+"\t"+time);
        frequencys.setEndTime();
        return events;
      }
      if (restartCondition != null && restartCondition.isMeet(nextGen, ndeme)) {
        frequencys.setIndexMostPastward(); // start at the very back.
        frequencys.getFrequencys(thisGen);
        continue;
      }

      double[] tmp = thisGen;
      thisGen = nextGen; // no need for a copy
      nextGen = tmp; // copied over. We hope!
      // now what about stopping conditions?
      // meh..just add a start for now.
    }
    assert time == model.getStartTime() : time + "\t" + model.getStartTime();
    if (time == 0) events.add(new SelectionStartEvent(model.getStartTime(), thisGen));
    // System.out.println("FALLThrough:"+events);
    return events;
  }