Ejemplo n.º 1
0
  @Test
  public void testMultipleRemoval() {
    bag.add(test, 5);

    Assert.assertTrue(bag.remove(test, 3));
    Assert.assertEquals(2, bag.size());
    Assert.assertTrue(bag.remove(test));
    Assert.assertEquals(1, bag.size());
  }
Ejemplo n.º 2
0
  @Test
  public void testSingularRemoval() {
    bag.add(test, 5);

    Assert.assertTrue(bag.remove(test));
    Assert.assertEquals(4, bag.size());
  }
Ejemplo n.º 3
0
  @Test
  public void testRemoveAll() {
    addStringsToCollection(bag);

    Assert.assertTrue(bag.remove(test));
  }
Ejemplo n.º 4
0
 @Test
 public void testRemovalOfNotAddedObject() {
   bag.add("Added");
   Assert.assertFalse(bag.remove("Not Added"));
 }
Ejemplo n.º 5
0
  // main CELL CA loop************
  public boolean iterateCells() {
    /*
    	// modify consumption matrix
    	    	for (int i=0;i<size;i++)
    	        for (int j=0;j<size;j++) consumption[i][j] = consumptionBasal[Cells[i][j]];
    */
    //
    if (cellList == null) cellList = new Bag(size * size);
    for (int i = 0; i < size; i++)
      for (int j = 0; j < size; j++) {
        if (Cells[i][j]
            < 4) { // All tumour cell types have Cell > 0, now 0 corresponds to 'healthy cells' that
          // consume at basal rate only
          int[] p = new int[2];
          p[0] = i;
          p[1] = j;
          cellList.add(p);
          if (Cells[i][j] == 1) {
            stem_cells_this_TS++;
          } else if (Cells[i][j] == 2 || Cells[i][j] == 3) {
            non_stem_cells_this_TS++;
          }
        }
      }

    while (cellList.size() != 0) {
      // Select the next lattice element at random
      int randomElemIndex = 0;
      if (cellList.size() > 1) randomElemIndex = random.nextInt(cellList.size() - 1);
      int[] point = (int[]) cellList.get(randomElemIndex);
      int rI = point[0];
      int rJ = point[1];

      cellList.remove(randomElemIndex); // Remove it from the cell list
      int cell = Cells[rI][rJ];

      // Cell death
      // if ((Oxygen[rI][rJ]<hypoxia)) {
      if ((random.nextFloat() < deathprob) && Cells[rI][rJ] > 0) { // x% chances of dying
        Age[rI][rJ] = 0;
        if (Cells[rI][rJ] == 1) stemDeathCounter[rI][rJ]++;
        if (Cells[rI][rJ] < 4) {
          // TACDeathCounter[rI][rJ]++;
          Cells[rI][rJ] = 4; // was 0, now making necrotic area (truly empty)
          deaths++;
          stemBirthCounter[rI][rJ] = 0;
          carriedmutation[rI][rJ] = 0; // empty space now has no mutations
          carriedGenome[rI][rJ] = initGenome; // empty space now has no mutations
        }
      } else if ((cell == 3)
          && (Age[rI][rJ]
              > 100 * maxMatureCellAge)) { // added * to allow for an update each celltimestep/x
        // **************************
        Age[rI][rJ] = 0;
        Cells[rI][rJ] = 4; // was 0, now making necrotic area (truly empty)
        // TACDeathCounter[rI][rJ]++;
        deaths++;
      } else if ((radiotherapy) && (cell == 2) && (random.nextFloat() > Oxygen[rI][rJ])) {
        // Radiotherapy
        Age[rI][rJ] = 0;
        if (Cells[rI][rJ] == 1) stemDeathCounter[rI][rJ]++;
        if ((Cells[rI][rJ] == 2) || (Cells[rI][rJ] == 3))
          // TACDeathCounter[rI][rJ]++;
          Cells[rI][rJ] = 4; // make necrotic
        stemBirthCounter[rI][rJ] = 0;
        deaths++;
      }

      // healthy division
      else if ((cell == 0) && (vacantSites(rI, rJ) > 0)) {
        if (proliferation[cell]
            >= random.nextFloat()) { // If tossing the coin we are to proliferate...
          // if (Oxygen[rI][rJ]>prolifThreshold) { // AND the oxygen concentration is enough for
          // division..
          // consumption[rI][rJ]=consumptionDivision[Cells[rI][rJ]];
          int[] daughter = findEmptySite(rI, rJ);
          births++;
          Cells[daughter[0]][daughter[1]] = 0;
          carriedmutation[daughter[0]][daughter[1]] = 0;
          carriedGenome[daughter[0]][daughter[1]] =
              initGenome; // resetting space to healthy cell with no mutations
        }
      }
      // }

      // cancer division
      else if ((vacantSitesCancer(rI, rJ) > 0) && (cell > 0))
        if (proliferation[cell]
            >= random.nextFloat()) { // If tossing the coin we are to proliferate...
          if ((cell == 1)
              || ((cell == 2)
                  && (Age[rI][rJ] < maxProDivisions))) { // AND the cell is stem or TAC ...
            // if (Oxygen[rI][rJ]>prolifThreshold) { // AND the oxygen concentration is enough for
            // division..
            // consumption[rI][rJ]=consumptionDivision[Cells[rI][rJ]];
            int[] daughter = findEmptySiteCancer(rI, rJ); // and there is space (for cancer)
            //    if ((daughter[0]==0) || (daughter[0]==size) ||
            // (daughter[1]==0)||(daughter[1]==size)) {simulationFinished=true;} // stop sim if a
            // cell hits the edge
            births++;
            if (cell == 1) { // stem cell
              stemBirthsTotal[rI][rJ]++;
              stemBirthCounter[rI][rJ]++;
              if (asymmetricRatio > random.nextFloat()) {
                Cells[daughter[0]][daughter[1]] = 1; // placing the stem daughter
                stemBirthCounter[daughter[0]][daughter[1]] =
                    stemBirthCounter[rI][rJ]; // update stem birth counter
                carriedmutation[daughter[0]][daughter[1]] =
                    carriedmutation[rI][rJ]; // inherit mutational status of parent
                carriedGenome[daughter[0]][daughter[1]] =
                    carriedGenome[rI][rJ]; // inherit mutational status of parent
                if (mutfreq > random.nextFloat()) { // small chance of mutation
                  mutationNum++; // advance mutation number
                  System.out.println(
                      +carriedmutation[rI][rJ]
                          + ", "
                          + mutationNum
                          + ", "
                          + stem_cells_this_TS
                          + ", "
                          + non_stem_cells_this_TS
                          + ", "
                          + timestep); // print (parent,child) pair
                  // tree.put(carriedmutation[rI][rJ], mutationNum);
                  // timeTree.put(mutationNum, timestep); // hash table stuff
                  if (0.5 > random.nextFloat()) {
                    carriedmutation[daughter[0]][daughter[1]] = mutationNum;
                    genomeToMod = new StringBuilder(carriedGenome[daughter[0]][daughter[1]]);
                    genomeToMod.setCharAt(
                        mutationNum - 1, '1'); //  daughter carries new carriedGenome
                    carriedGenome[daughter[0]][daughter[1]] = genomeToMod;
                    // System.out.println (carriedGenome[rI][rJ]);
                    // System.out.println (carriedGenome[daughter[0]][daughter[1]]);
                  } // 50:50 mutate new position daughter
                  else {
                    carriedmutation[rI][rJ] = mutationNum; // else mutate original position daughter
                    genomeToMod = new StringBuilder(carriedGenome[rI][rJ]);
                    // genomeToMod = carriedGenome[rI][rJ];
                    carriedGenome[rI][rJ] = genomeToMod;
                    genomeToMod.setCharAt(
                        mutationNum - 1, '1'); //  original carries new carriedGenome
                    // System.out.println (carriedGenome[rI][rJ]);
                    // System.out.println (carriedGenome[daughter[0]][daughter[1]]);
                  }
                }
              } else {
                Cells[daughter[0]][daughter[1]] = 2; // asymmetric division, daughter is TAC
                stemBirthCounter[daughter[0]][daughter[1]] = 0; // reset stem counter
                carriedmutation[daughter[0]][daughter[1]] =
                    carriedmutation[rI][rJ]; // TAC carries parental mutation flag
                carriedGenome[daughter[0]][daughter[1]] = carriedGenome[rI][rJ];
              } // TAC carries parental genome
              // } // redundant from above

              // else { // Only if there's hypoxia induced change of symmetric division ratio
              //    float newASR=asymmetricRatio+0*(hypoxia-Oxygen[rI][rJ]);
              // currently OFF by way of 0 the ^^ multiplier.
              //    if (newASR>random.nextFloat())
              // {Cells[daughter[0]][daughter[1]]=1;stemBirthCounter[daughter[0]][daughter[1]]=stemBirthCounter[rI][rJ];}
              //    else
              // {Cells[daughter[0]][daughter[1]]=2;stemBirthCounter[daughter[0]][daughter[1]]=0;}
              // // Otherwise differentiate
              // }
            } else if (cell == 2) { // non-stem division
              // TACBirthCounter[rI][rJ]++;
              if (Age[rI][rJ] < maxProDivisions - 1) {
                Cells[daughter[0]][daughter[1]] = 2;
                Age[rI][rJ]++;
                Age[daughter[0]][daughter[1]] = Age[rI][rJ];
                carriedmutation[daughter[0]][daughter[1]] =
                    carriedmutation[rI][rJ]; // TAC carries parental mutation flag
                carriedGenome[daughter[0]][daughter[1]] =
                    carriedGenome[rI][rJ]; // TAC carries parental genome
              } else {
                Cells[daughter[0]][daughter[1]] = 3;
                Cells[rI][rJ] = 3;
                Age[rI][rJ] = 0;
                Age[daughter[0]][daughter[1]] = Age[rI][rJ];
                carriedmutation[daughter[0]][daughter[1]] =
                    carriedmutation[rI][rJ]; // TAC carries parental mutation flag
                carriedGenome[daughter[0]][daughter[1]] =
                    carriedGenome[rI][rJ]; // TAC carries parental genome
              }
            }
          }
        } else if (pMotility > random.nextFloat()) { // Migration = not in use
          int[] daughter = findEmptySite(rI, rJ);
          Cells[daughter[0]][daughter[1]] = cell;
          Cells[rI][rJ] = 0;
          Age[daughter[0]][daughter[1]] = Age[rI][rJ];
          Age[rI][rJ] = 0;
          System.err.println("moving " + rI + ", " + rJ);
        }
      // Aging for mature cells
      if (cell == 3) Age[rI][rJ]++;
    }
    return true;
  }
Ejemplo n.º 6
0
  /* Test the ArrayBag implementation. */
  public static void main(String[] args) {

    // Create a Scanner object for user input.
    Scanner in = new Scanner(System.in);

    // Create an ArrayBag named bag1.
    System.out.print("Size of bag 1: ");
    int size = in.nextInt();
    Bag bag1 = new ArrayBag(size);
    in.nextLine(); // consume the rest of the line

    // ****** Additions *****

    // Display capacity of new bag
    System.out.println("Bag 1 can hold up to " + bag1.capacity() + " items.");

    // Determine if ArrayBag is full
    if (bag1.isFull()) System.out.println("Bag is full.");
    else System.out.println("Bag is NOT full.");

    // Read in strings, add them to bag1, and print out bag1.
    String itemStr;
    for (int i = 0; i < size; i++) {
      System.out.print("item " + i + ": ");
      itemStr = in.nextLine();
      bag1.add(itemStr);
    }
    System.out.println("bag 1 = " + bag1);
    System.out.println();

    // Increase capacity by 2
    System.out.println("Increasing Bag 1 capacity by 2.");
    bag1.increaseCapacity(2);
    System.out.println("Bag 1 can hold up to " + bag1.capacity() + " items.");

    // Select a random item and print it.
    Object item = bag1.grab();
    System.out.println("grabbed " + item);
    System.out.println();

    // Iterate over the objects in bag1, printing them one per
    // line.
    Object[] items = bag1.toArray();
    for (int i = 0; i < items.length; i++) System.out.println(items[i]);
    System.out.println();

    // Get an item to remove from bag1, remove it, and reprint the bag.
    System.out.print("item to remove: ");
    itemStr = in.nextLine();
    if (bag1.contains(itemStr)) bag1.remove(itemStr);
    System.out.println("bag 1 = " + bag1);
    System.out.println();

    // Remove all items from bag1 and reprint the bag.
    Bag other1 = new ArrayBag(2);
    other1.add("ff");
    other1.add("aa");
    System.out.println("Removing all items {aa, ff}");
    bag1.removeItems(other1);
    System.out.println("bag 1 = " + bag1);
    System.out.println();

    // Create two ArrayBags and return their union (with no duplicates)
    // {2, 2, 3, 5, 7, 7, 7, 8}
    Bag b1 = new ArrayBag(8);
    b1.add("2");
    b1.add("2");
    b1.add("3");
    b1.add("5");
    b1.add("7");
    b1.add("7");
    b1.add("7");
    b1.add("8");
    System.out.println("Bag b1 is " + b1);

    // {2, 3, 4, 5, 5, 6, 7}
    Bag b2 = new ArrayBag(7);
    b2.add("2");
    b2.add("3");
    b2.add("4");
    b2.add("5");
    b2.add("5");
    b2.add("6");
    b2.add("7");
    System.out.println("Bag b2 is " + b2);

    // Be sure to test case with zero items
    Bag b3 = new ArrayBag();
    b3 = b1.unionWith(b2);
    System.out.println("Union of b1 & b2 is " + b3);
    System.out.println("b3 capacity is " + b3.capacity());
    System.out.println();
  }
  /**
   * Assign the set of students (presumably non-incoming-freshmen, but this is not checked) passed
   * to their dorm rooms, in a way that does not take race into account. (compare {@link
   * #assignByRace}.) As a precursor (side effect) to this, any students already existing in
   * upperclass dorms will be removed.
   */
  public void assign(Bag students) {

    emptyDorms();

    // Purge all freshmen. (We're not assigning those.)
    Bag upperclassmen = null;
    try {
      upperclassmen = (Bag) students.clone();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
    for (int x = upperclassmen.size() - 1; x >= 0; x--) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        upperclassmen.remove(s);
      }
    }

    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (s.getGrade() < 2) {
        // We're only assigning upperclassmen here.
        continue;
      }
      s.leaveRoom();
      for (int y = 0; y < dorms.size(); y++) {
        if (s.hasRoom()) {
          break;
        }
        Dorm d = (Dorm) dorms.get(y);
        if (d.isFull()) {
          continue;
        }
        if (d.isFemaleOnly() && s.getGender() == Student.Gender.MALE) {
          continue;
        }
        int i = 0;
        while (i < d.getNumRooms()) {
          if (s.hasRoom()) {
            break;
          }
          try {
            d.getRoomByIndex(i).addResident(s);
          } catch (IllegalArgumentException e) { // System.out.println(e);
          }
          i++;
        }
      }
    }

    // Error testing to see if there are upperclassmen who don't have a
    //  room/dorms that aren't full
    // int q=0;
    for (int x = 0; x < upperclassmen.size(); x++) {
      Student s = (Student) upperclassmen.get(x);
      if (!s.hasRoom()) {
        System.out.println(s + " has no upperclass dorm room!");
      }
    }
  }