Skip to content

Commit

Permalink
Finalização da simulação e controle de tempo
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseFilipi14 committed Jan 4, 2025
1 parent be09a58 commit 8eeaf9a
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 33 deletions.
2 changes: 1 addition & 1 deletion APDescription.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
( ap-description :name "172.28.144.1:1099/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://Kaua:7778/acc))))
( ap-description :name "172.25.0.1:1099/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://filipi-550xda:7778/acc))))
2 changes: 1 addition & 1 deletion MTPs-Main-Container.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
http://Kaua:7778/acc
http://filipi-550xda:7778/acc
Binary file modified bin/Agent/CellAgent$CellApoptoseBehaviour.class
Binary file not shown.
Binary file modified bin/Agent/CellAgent$CellDivisionBehaviour.class
Binary file not shown.
Binary file modified bin/Agent/CellAgent$CellInfo.class
Binary file not shown.
Binary file modified bin/Agent/CellAgent$CellRepairBehaviour.class
Binary file not shown.
Binary file modified bin/Agent/CellAgent.class
Binary file not shown.
92 changes: 61 additions & 31 deletions src/Agent/CellAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@


public class CellAgent extends Agent {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private int age = 1; // Idade da célula
// geracao, predisposicao inicial
private int generation = 0;;
private CellType celltype = new NormalCell(generation,10);;
private static int agentCount = 0; // Contador de agentes ativos
private static final int MAX_AGENTS = 100; // Limite máximo de agentes
private static int agentCount = 0;
private static final int MAX_AGENTS = 100;
private static final int DAYS_TO_DIVIDE = 4;
private static final int DAYS_TO_RUN = 10;
private static final ConcurrentLinkedQueue<CellInfo> cellRegistry = new ConcurrentLinkedQueue<>();

@Override
Expand All @@ -41,9 +42,10 @@ protected void setup() {
switch (cellType) {
case Normal:
celltype = new NormalCell(generation,10);
break;
case Damaged:
celltype = new DamagedCell(generation,30);
break;
break;
case PreCancerous:
celltype = new PreCancerousCell(generation,50);
break;
Expand All @@ -61,7 +63,7 @@ protected void setup() {

// Adicionar ao registro global
synchronized (cellRegistry) {
cellRegistry.add(new CellInfo(getLocalName(), celltype.getGeneration()));
cellRegistry.add(new CellInfo(getLocalName(), celltype.getGeneration(), celltype));
agentCount++;
}

Expand All @@ -85,30 +87,33 @@ protected void takeDown() {
}

private class CellDivisionBehaviour extends TickerBehaviour {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

public CellDivisionBehaviour(Agent a, long interval) {
public CellDivisionBehaviour(Agent a, long interval) {
super(a, (celltype.getcellState() == ECellState.Cancerous) ? Math.max(interval / 2, 500) : interval);
}

@Override
protected void onTick() {
try {
// Verificar se passou um ano (geração * 4 > x tempo)
if (celltype.getGeneration() * DAYS_TO_DIVIDE > DAYS_TO_RUN) {
finishSimulation();
return;
}

synchronized (cellRegistry) {
if (agentCount + 2 > MAX_AGENTS) {
System.out.println("Removendo 50% dos agentes!");
System.out.println("Removendo 50% dos agentes!");
System.out.println("Removendo 50% dos agentes!");
int cellsToRemove = MAX_AGENTS/2;
System.out.println("Removendo 50% dos agentes!");
int cellsToRemove = MAX_AGENTS / 2;
for (int i = 0; i < cellsToRemove; i++) {
CellInfo oldestCell = cellRegistry.poll();
if (oldestCell != null) {
jade.wrapper.AgentController agent = getContainerController().getAgent(oldestCell.name);
if (agent != null) {
agent.kill();
agentCount--;
agent.kill();
agentCount--;
}

}
}
}
Expand Down Expand Up @@ -136,10 +141,9 @@ protected void onTick() {

// Registra os agentes filhos e incrementa contador
synchronized (cellRegistry) {
cellRegistry.add(new CellInfo(newAgent1Name, celltype.getGeneration() + 1));
cellRegistry.add(new CellInfo(newAgent2Name, celltype.getGeneration() + 1));
cellRegistry.add(new CellInfo(newAgent1Name, celltype.getGeneration() + 1, celltype));
cellRegistry.add(new CellInfo(newAgent2Name, celltype.getGeneration() + 1, celltype));
agentCount += 1;

}

newAgent1.start();
Expand All @@ -156,44 +160,70 @@ protected void onTick() {
}

private class CellApoptoseBehaviour extends TickerBehaviour {
private static final long serialVersionUID = 1L;
private Agent agent;

private static final long serialVersionUID = 1L;
private Agent agent;

public CellApoptoseBehaviour(Agent a, long interval) {
public CellApoptoseBehaviour(Agent a, long interval) {
super(a, interval);
agent = a;
}

@Override
protected void onTick() {
if(celltype.apoptose(agent))
agentCount--;
if (celltype.apoptose(agent))
agentCount--;
}
}

private class CellRepairBehaviour extends TickerBehaviour {
private static final long serialVersionUID = 1L;
private Agent agent;
public CellRepairBehaviour(Agent a, long interval) {
private static final long serialVersionUID = 1L;
private Agent agent;

public CellRepairBehaviour(Agent a, long interval) {
super(a, interval);
agent = a;
}

@Override
protected void onTick() {
celltype.repair(agent);
celltype.repair(agent);
}
}


private static class CellInfo {
String name;
int generation;
CellType celltype;

CellInfo(String name, int generation) {
CellInfo(String name, int generation, CellType celltype) {
this.name = name;
this.generation = generation;
this.celltype = celltype;
}
}
}

private void finishSimulation() {
int normalCount = 0, damagedCount = 0, preCancerousCount = 0, cancerousCount = 0;

synchronized (cellRegistry) {
for (CellInfo cell : cellRegistry) {
if (cell.celltype instanceof NormalCell) normalCount++;
else if (cell.celltype instanceof DamagedCell) damagedCount++;
else if (cell.celltype instanceof PreCancerousCell) preCancerousCount++;
else if (cell.celltype instanceof CancerousCell) cancerousCount++;
}
}

int totalCells = normalCount + damagedCount + preCancerousCount + cancerousCount;
System.out.println("--------------------------------------------------------------");
System.out.println("Simulação finalizada. Percentual de células:");
System.out.println("Normal: " + (normalCount * 100.0 / totalCells) + "%");
System.out.println("Danificada: " + (damagedCount * 100.0 / totalCells) + "%");
System.out.println("Pré-Cancer: " + (preCancerousCount * 100.0 / totalCells) + "%");
System.out.println("Câncer: " + (cancerousCount * 100.0 / totalCells) + "%");
System.out.println("--------------------------------------------------------------");


System.exit(0);
}
}

0 comments on commit 8eeaf9a

Please sign in to comment.