Exemple #1
0
  private void backtrack(CPA cpa) throws InterruptedException {
    if (IA()) {
      Stop();
    } else {
      System.out.println(
          ID
              + "is running and AV is "
              + AgentView
              + " and cpa  is "
              + cpa
              + " from func backtrack");
      Assignment last = cpa.Last_Assignment();
      if (last != null) {
        cpa = cpa.shortest_inconsistent_partail_assignment(AgentView);
        System.out.println(
            ID
                + "is running and AV is "
                + AgentView
                + " and cpa  is "
                + cpa
                + " from func backtrack");
        int backto = last.getID();
        AgentView_consistent = false;
        initilize_domain();

        send(new Message("Backtrack_CPA", cpa), backto);
      } else {
        System.out.println("last message is null and got backtrack - SHOULDNT HAPPEN");
      }
    }
  }
Exemple #2
0
  private void Recieve_CPA(Message message) throws Exception {

    CPA cpa = message.cpa();
    System.out.println(
        ID + "is running and AV is " + AgentView + " and AV_consi is " + AgentView_consistent);

    if (!AgentView_consistent) {
      if (cpa.contains(AgentView)) {

        backtrack(cpa);
      } else {
        AgentView_consistent = true;
      }
    }
    if (AgentView_consistent) {
      if (message.type() == "Backtrack_CPA") {
        cpa.remove_assignment();
        assignCPA(cpa);
      } else {
        if (updateAgentView(cpa)) {

          assignCPA(cpa);
        } else {
          System.out.println(
              ID
                  + "is running and AV is "
                  + AgentView
                  + " and cpa  is "
                  + cpa
                  + " func recieve cpa");
          backtrack(cpa);
        }
      }
    }
  }
Exemple #3
0
  private boolean is_assigned(CPA cpa) throws Exception {
    boolean assignmets_constraied = false;
    int k = -1;
    for (int i = 0; i < Domain.length; i++) {
      k++;
      k = find_next_possible_assignment(k);
      if (k == -1) {
        emptyDomain();
        return false;
      }
      Assignment assignment = new Assignment(ID, k);
      for (int j = 0; j < cpa.length() && !assignmets_constraied; j++) {
        if (constrained(assignment, cpa.getAssignment(j))) {
          assignmets_constraied = true;
        }
      }
      if (!assignmets_constraied) {
        Domain[k] = 0;
        cpa.add_assignment(assignment);

        AgentView = cpa;

        return true;
      }
      assignmets_constraied = false;
    }
    return false;
  }
Exemple #4
0
 private void Process_NOT_OK(Message message) {
   CPA Not_OK = message.cpa();
   if (AgentView.contains(Not_OK)) {
     AgentView = Not_OK;
     AgentView_consistent = false;
   } else if (!AgentView.contains(Not_OK)) {
     if (message.SC() > AgentView.SC()) {
       AgentView = Not_OK;
       AgentView_consistent = false;
     }
   }
 }
Exemple #5
0
 private void ForwardCheck(Message message) throws InterruptedException {
   System.out.println("HELLO");
   CPA fc_cpa = message.cpa();
   if (message.SC() > AgentView.SC()) {
     if (!AgentView_consistent) {
       if (!fc_cpa.contains(new CPA(AgentView))) {
         AgentView_consistent = true;
       }
     }
     if (AgentView_consistent) {
       if (!updateAgentView(fc_cpa)) {
         send(new Message("Not_OK", AgentView), other_unassigned_agents_excep(ID, AgentView));
       }
     }
   }
 }
Exemple #6
0
  private int[] other_unassigned_agents_excep(
      int k,
      CPA
          cpa) { // returns an array. array[i]=1 if a message needs to be sent to i  and 0
                 // otherwise.
    int[] unassigned_agents = new int[num_of_agents];
    for (int i = 0; i < unassigned_agents.length; i++) {
      unassigned_agents[i] = 1;
    }
    unassigned_agents[k] = 0;
    for (int i = 0; i < cpa.length() && cpa.getAssignment(i) != null; i++) {
      unassigned_agents[cpa.getID(i)] = 0;
    }

    return unassigned_agents;
  }
Exemple #7
0
  private boolean updateAgentView(CPA cpa) {

    CPA shortest_inconsistent_partail_assignment =
        cpa.shortest_inconsistent_partail_assignment(AgentView);
    adjustAgentView(cpa);
    if (isEmptyDomain()) {
      AgentView = shortest_inconsistent_partail_assignment;
      return false;
    }
    return true;
  }
Exemple #8
0
 private void setDomain(CPA cpa) {
   for (int i = 0; i < cpa.length(); i++) {
     Assignment ass = cpa.getAssignment(i);
     if (ass != null) {
       int agentID = ass.getID();
       int assValue = ass.getValue();
       if (constrained_agents(agentID, this.ID)) {
         int[][] values = constraints[agentID][this.ID];
         System.out.println("#### the value is    " + values[4][4]);
         for (int j = 0; j < Domain.length; j++) {
           if (values[j][assValue] == 1) { // values are constrained
             Domain[j] = 0;
           }
         }
       }
     } else {
       break;
     }
   }
 }
Exemple #9
0
  private void assignCPA(CPA cpa) throws Exception {

    if (is_assigned(cpa)) {
      if (isfull(cpa)) {
        report_solution(cpa);
        Stop();
      } else {

        cpa.increas_counter();
        send(new Message("CPA", cpa), next(cpa));
        System.out.println("######" + next(cpa));
        send(new Message("FC_CPA", cpa), other_unassigned_agents_excep(next(cpa), cpa));
        System.out.println("######" + next(cpa));
      }
    } else {

      AgentView = cpa.shortest_inconsistent_partail_assignment(AgentView);
      backtrack(AgentView);
    }
  }
Exemple #10
0
 private void setAgentView(CPA cpa) {
   for (int i = 0; i < AgentView.length(); i++) {
     AgentView.setAssignment(cpa.getAssignment(i), i);
   }
 }
Exemple #11
0
 private boolean isfull(CPA cpa) {
   return cpa.isfull();
 }