Example #1
0
 public void resetNodesWithoutReservation() {
   for (Node n : noRes) {
     List<Integer> THS = n.getTHS();
     for (int t : THS) {
       if (t != n.getID()) {
         Node removeFrom = nodes.get(t);
         removeFrom.removeNeighbour(n.getID());
       }
     }
     n.clearReservation();
   }
 }
Example #2
0
  public void findTHS() {
    int doubleRange = this.range * 2;

    for (int i = 0; i < numNodes; i++) {
      Node active = nodes.get(i);
      for (int j = 0; j < numNodes; j++) {
        Node toCheck = nodes.get(j);
        if (i != j) {
          // System.out.println("active x: " + active.getXLanePosition() + " active y: " +
          // active.getyLanePosition());
          // System.out.println("check  y : " + toCheck.getXLanePosition() + " check y:  " +
          // toCheck.getyLanePosition());
          int activeX = active.getXLanePosition();
          int activeY = active.getyLanePosition();

          int checkX = toCheck.getXLanePosition();
          int checkY = toCheck.getyLanePosition();

          double distance =
              Math.sqrt(Math.pow((activeX - checkX), 2) + Math.pow((activeY - checkY), 2));
          if (distance <= doubleRange) {
            active.addToTHS(toCheck.getID());
          }
        }
      }
    }
  }
Example #3
0
  public void reserveTimeSlots() {
    List<Integer> hasSlot = new ArrayList<Integer>();
    while (hasSlot.size() < this.numNodes) {
      int nodeToAttempt = r.nextInt(this.numNodes);
      while (hasSlot.contains(nodeToAttempt)) {
        nodeToAttempt = r.nextInt(this.numNodes);
      }

      Node toAttemptReservation = nodes.get(nodeToAttempt);
      if (toAttemptReservation.getReservation() == -1) // Node doesn't have a slot yet
      {
        boolean canAdd = true;
        int slotToReserve = r.nextInt(this.numSlots);
        List<Integer> THSToCheck = toAttemptReservation.getTHS();

        for (int n : THSToCheck) {
          Node toCheck = nodes.get(n);
          ReservationBean[] toCheckSlots = toCheck.getTimeSlots();
          if (toCheckSlots[slotToReserve].getHolder() != -1) {
            canAdd = false;
            break;
          }
        }
        if (canAdd) {
          toAttemptReservation.setReservation(slotToReserve);

          for (int n : THSToCheck) {
            Node toUpdate = nodes.get(n);
            // set reservation in all nodes in THS
            toUpdate.setTimeSlotReservation(
                slotToReserve,
                toAttemptReservation.getID(),
                toAttemptReservation.getRemainingReservationDuration());
            // Set initial FI in all nodes in THS
            toUpdate.setFI(
                slotToReserve,
                toAttemptReservation.getID(),
                toAttemptReservation.getRemainingReservationDuration());
          }
          hasSlot.add(toAttemptReservation.getID());
        }
      }
    }
  }
Example #4
0
  public void sendJamInRange(int senderID, JamMessage m) {
    Node active = nodes.get(senderID);
    int activeLane = active.getLane();
    // int activeLanePosition = active.getLanePosition();
    // System.out.println(field.length+ "   x     " + field[0].length);

    // Left most lane with which communication may occur
    int leftLimit = ((activeLane * this.width) - this.range) / this.width;
    if (leftLimit < 0) leftLimit = 0;

    // Right most lane with which communication may occur
    int rightLimit = ((activeLane * this.width) + this.range) / this.width;
    if (rightLimit > field.length) rightLimit = field.length - 1;

    int upperLimit = active.getyLanePosition() + this.range;
    if (upperLimit >= field[0].length) upperLimit = field[0].length - 1;

    int lowerLimit = active.getyLanePosition() - this.range;
    if (lowerLimit < 0) lowerLimit = 0;

    int activeX = active.getXLanePosition();
    int activeY = active.getyLanePosition();

    for (int j = leftLimit; j <= rightLimit; j++) {
      for (int k = lowerLimit; k <= upperLimit; k++) {
        Node toCheck = field[j][k];
        if (toCheck != null && toCheck.getID() != active.getID()) {

          int toCheckX = toCheck.getXLanePosition() * this.width;
          int toCheckY = toCheck.getyLanePosition();
          double distance =
              Math.sqrt(Math.pow(toCheckX - activeX, 2) + Math.pow(toCheckY - activeY, 2));
          // System.out.println(distance);
          if (distance < this.range) {
            toCheck.recJam(m);
          }
        }
      }
    }
  }
Example #5
0
  public AbrahamGAValue getABGroup(ChemGraph p_chemGraph) {
    // #[ operation getGAGroup(ChemGraph)

    AbramData result_abram = new AbramData();
    Graph g = p_chemGraph.getGraph();
    HashMap oldCentralNode = (HashMap) (p_chemGraph.getCentralNode()).clone();

    // satuate radical site
    int max_radNum_molecule = ChemGraph.getMAX_RADICAL_NUM();
    int max_radNum_atom = Math.min(8, max_radNum_molecule);
    int[] idArray = new int[max_radNum_molecule];
    Atom[] atomArray = new Atom[max_radNum_molecule];
    Node[][] newnode = new Node[max_radNum_molecule][max_radNum_atom];

    int radicalSite = 0;
    Iterator iter = p_chemGraph.getNodeList();
    FreeElectron satuated = FreeElectron.make("0");
    while (iter.hasNext()) {
      Node node = (Node) iter.next();
      Atom atom = (Atom) node.getElement();
      if (atom.isRadical()) {
        radicalSite++;
        // save the old radical atom
        idArray[radicalSite - 1] = node.getID().intValue();
        atomArray[radicalSite - 1] = atom;
        // new a satuated atom and replace the old one
        Atom newAtom = new Atom(atom.getChemElement(), satuated);
        node.setElement(newAtom);
        node.updateFeElement();
      }
    }

    // add H to satuate chem graph
    Atom H = Atom.make(ChemElement.make("H"), satuated);
    Bond S = Bond.make("S");
    for (int i = 0; i < radicalSite; i++) {
      Node node = p_chemGraph.getNodeAt(idArray[i]);
      Atom atom = atomArray[i];
      int HNum = atom.getRadicalNumber();
      for (int j = 0; j < HNum; j++) {
        newnode[i][j] = g.addNode(H);
        g.addArcBetween(node, S, newnode[i][j]);
      }
      node.updateFgElement();
    }

    // find all the thermo groups
    iter = p_chemGraph.getNodeList();
    while (iter.hasNext()) {
      Node node = (Node) iter.next();
      Atom atom = (Atom) node.getElement();
      if (!(atom.getType().equals("H"))) {
        if (!atom.isRadical()) {

          p_chemGraph.resetThermoSite(node);
          AbrahamGAValue thisAbrahamValue = thermoLibrary.findAbrahamGroup(p_chemGraph);

          if (thisAbrahamValue == null) {
            System.err.println("Abraham group not found: " + node.getID());
          } else {
            // System.out.println(node.getID() + " " + thisGAValue.getName()+ "
            // "+thisGAValue.toString());

            result_abram.plus(thisAbrahamValue);
          }
        } else {
          System.err.println("Error: Radical detected after satuation!");
        }
      }
    }

    //        // find the BDE for all radical groups
    //        for (int i=0; i<radicalSite; i++) {
    //          	int id = idArray[i];
    //           	Node node = g.getNodeAt(id);
    //           	Atom old = (Atom)node.getElement();
    //           	node.setElement(atomArray[i]);
    //           	node.updateFeElement();
    //
    //            // get rid of the extra H at ith site
    //          	int HNum = atomArray[i].getRadicalNumber();
    //           	for (int j=0;j<HNum;j++) {
    //           		g.removeNode(newnode[i][j]);
    //           	}
    //           	node.updateFgElement();
    //
    //           	p_chemGraph.resetThermoSite(node);
    //           	ThermoGAValue thisGAValue = thermoLibrary.findRadicalGroup(p_chemGraph);
    //           	if (thisGAValue == null) {
    //           		System.err.println("Radical group not found: " + node.getID());
    //           	}
    //           	else {
    //           		//System.out.println(node.getID() + " radical correction: " +
    // thisGAValue.getName() + "  "+thisGAValue.toString());
    //           		result.plus(thisGAValue);
    //            }
    //
    //            //recover the satuated site for next radical site calculation
    //          	node.setElement(old);
    //          	node.updateFeElement();
    //           	for (int j=0;j<HNum;j++) {
    //           		newnode[i][j] = g.addNode(H);
    //           		g.addArcBetween(node,S,newnode[i][j]);
    //           	}
    //           	node.updateFgElement();
    //
    //         }
    //
    //         // recover the chem graph structure
    //         // recover the radical
    //         for (int i=0; i<radicalSite; i++) {
    //           	int id = idArray[i];
    //           	Node node = g.getNodeAt(id);
    //           	node.setElement(atomArray[i]);
    //           	node.updateFeElement();
    //           	int HNum = atomArray[i].getRadicalNumber();
    //         	//get rid of extra H
    //           	for (int j=0;j<HNum;j++) {
    //          	g.removeNode(newnode[i][j]);
    //           	}
    //           	node.updateFgElement();
    //         }
    //
    //         // substrate the enthalphy of H from the result
    //         int rad_number = p_chemGraph.getRadicalNumber();
    //         ThermoGAValue enthalpy_H = new ThermoGAValue(ENTHALPY_HYDROGEN * rad_number,
    // 0,0,0,0,0,0,0,0,0,0,0,null);
    //         result.minus(enthalpy_H);
    //
    //         // make the symmetric number correction to entropy
    //
    //         if (p_chemGraph.isAcyclic()){
    //			 int sigma = p_chemGraph.getSymmetryNumber();
    //	         ThermoGAValue symmtryNumberCorrection = new
    // ThermoGAValue(0,GasConstant.getCalMolK()*Math.log(sigma),0,0,0,0,0,0,0,0,0,0,null);
    //			 result.minus(symmtryNumberCorrection);
    //         }

    p_chemGraph.setCentralNode(oldCentralNode);

    return result_abram;

    // #]
  }