Skip to content

Commit

Permalink
Merge pull request CST-Group#60 from CST-Group/smallcorrections
Browse files Browse the repository at this point in the history
Smallcorrections
  • Loading branch information
rgudwin authored Sep 25, 2024
2 parents 5429afa + 5e4fdc2 commit deb39d9
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Note: This library is still under development, and some concepts or features mig
```
dependencies {
...
implementation 'com.github.CST-Group:cst:1.4.1'
implementation 'com.github.CST-Group:cst:1.4.2'
}
```

Expand All @@ -53,7 +53,7 @@ Sometimes, the version number (tag) in this README gets out of date, as maintain
<dependency>
<groupId>com.github.CST-Group</groupId>
<artifactId>cst</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ java {
}


version = '1.4.0'
version = '1.4.2'

repositories {
mavenCentral()
Expand Down
77 changes: 68 additions & 9 deletions src/main/java/br/unicamp/cst/core/entities/MemoryContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class MemoryContainer implements Memory {
*/
private String name;

public enum Policy {MAX, MIN, RANDOM_FLAT, RANDOM_PROPORTIONAL, ITERATE};
public enum Policy {MAX, MIN, RANDOM_FLAT, RANDOM_FLAT_STABLE, RANDOM_PROPORTIONAL, RANDOM_PROPORTIONAL_STABLE, ITERATE};

/**
* Policy used for selecting a MemoryObject at the MemoryContainer
Expand All @@ -56,6 +56,7 @@ public enum Policy {MAX, MIN, RANDOM_FLAT, RANDOM_PROPORTIONAL, ITERATE};
private volatile Memory last;
private volatile int lasti=0;
private transient Random rand = new Random();
private transient int randchoice = -1;

/**
* Creates a MemoryContainer.
Expand Down Expand Up @@ -142,8 +143,8 @@ else if (memoryEval == maxEval) {
}
}
if (allmax.size() > 1) {
int i = rand.nextInt(allmax.size());
last = allmax.get(i);
if (randchoice < 0) randchoice = rand.nextInt(allmax.size());
last = allmax.get(randchoice);
return(last.getI());
}
else {
Expand Down Expand Up @@ -176,8 +177,8 @@ else if (memoryEval == minEval) {
}
}
if (allmin.size() > 1) {
int i = rand.nextInt(allmin.size());
last = allmin.get(i);
if (randchoice < 0) randchoice = rand.nextInt(allmin.size());
last = allmin.get(randchoice);
return(last.getI());
}
else {
Expand All @@ -201,8 +202,21 @@ private synchronized Object getIRandomFlat() {

/**
* Gets the info of a random memory from within the MemoryContainer.
* In this case, the choice will only change after a change in the container
*
* @return the info of a random memory within the MemoryContainer
* @return the info of a random memory within the MemoryContainer (stable version)
*/
private synchronized Object getIRandomFlatStable() {

if (randchoice < 0) randchoice = rand.nextInt(memories.size());
last = memories.get(randchoice);
return(last.getI());
}

/**
* Gets the info of a random memory from within the MemoryContainer using eval as a weight.
*
* @return the info of a random memory within the MemoryContainer using eval as a weight
*/
private synchronized Object getIRandomProportional() {

Expand Down Expand Up @@ -232,9 +246,42 @@ private synchronized Object getIRandomProportional() {
}

/**
* Gets the info of a random memory from within the MemoryContainer.
* Gets the info of a random memory from within the MemoryContainer using eval as a weight.
*
* @return the info of a random memory within the MemoryContainer
* @return the info of a random memory within the MemoryContainer using eval as a weight
*/
private synchronized Object getIRandomProportionalStable() {

if (memories.size() == 0) return null;
double indexfrom[] = new double[memories.size()];
double indexto[] = new double[memories.size()];
int i = 0;
for (Memory memory : memories) {
if (i == 0)
indexfrom[i] = 0;
else
indexfrom[i] = indexto[i-1];
double interval = memory.getEvaluation();
indexto[i] = indexfrom[i] + interval;
i++;
}
double llast = indexto[i-1];
double wheel = rand.nextDouble();
if (llast*wheel == 0) return(getIRandomFlatStable());
for (int j=0;j<=memories.size();j++)
if (indexfrom[j] < wheel*llast && wheel*llast < indexto[j]) {
if (randchoice < 0) randchoice = j;
last = memories.get(randchoice);
return(last.getI());
}
last = memories.get(0);
return(last.getI());
}

/**
* Gets the info of a memory from within the MemoryContainer in an iterative way.
*
* @return the info of a memory within the MemoryContainer in an iterative way
*/
private synchronized Object getIIterate() {
if (memories.size() > 0 && lasti < memories.size()) {
Expand All @@ -253,7 +300,9 @@ private synchronized Object getIIterate() {
* Policy.MAX
* Policy.MIN
* Policy.RANDOM_FLAT
* Policy.RANDOM_FLAT_STABLE
* Policy.RANDOM_PROPORTIONAL
* Policy.RANDOM_PROPORTIONAL_STABLE
* Policy.ITERATE
*
* @return the info of the memory according to the specified MemoryContainer policy
Expand All @@ -264,7 +313,9 @@ public synchronized Object getI() {
case MAX: return getIMax();
case MIN: return getIMin();
case RANDOM_FLAT: return getIRandomFlat();
case RANDOM_FLAT_STABLE: return getIRandomFlatStable();
case RANDOM_PROPORTIONAL: return getIRandomProportional();
case RANDOM_PROPORTIONAL_STABLE: return getIRandomProportionalStable();
case ITERATE: return getIIterate();
default: return getIMax();
}
Expand Down Expand Up @@ -390,7 +441,8 @@ public synchronized int setI(Object info) {
*/
public synchronized int setI(Object info, Double evaluation) {

MemoryObject mo = new MemoryObject();
randchoice = -1;
MemoryObject mo = new MemoryObject();
mo.setI(info);
if (evaluation != -1.0)
mo.setEvaluation(evaluation);
Expand All @@ -408,6 +460,7 @@ public synchronized int setI(Object info, Double evaluation) {
* @param index the index of the memory inside the container.
*/
public synchronized void setI(Object info, int index) {
randchoice = -1;
if (memories != null && memories.size() > index) {
Memory memory = memories.get(index);
if (memory != null) {
Expand All @@ -430,6 +483,7 @@ public synchronized void setI(Object info, int index) {
* @param evaluation the evaluation to be set.
*/
public synchronized void setI(Object info, Double evaluation, int index) {
randchoice = -1;
if (memories != null && memories.size() > index) {
Memory memory = memories.get(index);
if (memory != null) {
Expand All @@ -454,6 +508,7 @@ public synchronized void setI(Object info, Double evaluation, int index) {
* @return the index of the memory
*/
public synchronized int setI(Object info, double evaluation, String type) {
randchoice = -1;
int index = -1;
if (memories != null) {
boolean set = false;
Expand Down Expand Up @@ -529,6 +584,7 @@ public synchronized String getName() {
*/
@Override
public synchronized void setEvaluation(Double eval) {
randchoice = -1;
if (last != null && last instanceof Memory) {
last.setEvaluation(eval);
}
Expand All @@ -542,6 +598,7 @@ public synchronized void setEvaluation(Double eval) {
* @param index the index of the memory inside this container.
*/
public synchronized void setEvaluation(Double eval, int index) {
randchoice = -1;
if (memories != null && memories.size() > index) {
Memory memory = memories.get(index);
if (memory != null) {
Expand All @@ -561,6 +618,7 @@ public synchronized void setEvaluation(Double eval, int index) {
* @return the index of the added memory
*/
public synchronized int add(Memory memory) {
randchoice = -1;
int index = -1;
if (memory != null) {
memories.add(memory);
Expand Down Expand Up @@ -707,6 +765,7 @@ public synchronized void removeMemoryObserver(MemoryObserver memoryObserver) {
* @param pol the new Policy to be used
*/
public void setPolicy(Policy pol) {
randchoice = -1;
policy = pol;
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/br/unicamp/cst/core/entities/Mind.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This class represents the Mind of the agent, wrapping all the CST's core
Expand Down Expand Up @@ -75,7 +77,7 @@ public synchronized void createCodeletGroup(String groupName) {
*
*/
public synchronized void createMemoryGroup(String groupName) {
ArrayList<Codelet> group = new ArrayList<Codelet>();
ArrayList<Memory> group = new ArrayList<Memory>();
memoryGroups.put(groupName,group);
}

Expand Down Expand Up @@ -276,7 +278,8 @@ public Codelet insertCodelet(Codelet co, String groupName) {
*/
public void registerCodelet(Codelet co, String groupName) {
ArrayList<Codelet> groupList = codeletGroups.get(groupName);
if (groupList != null) groupList.add(co);
if (groupList != null) groupList.add(co);
else Logger.getAnonymousLogger().log(Level.INFO,"The Codelet Group {0} still does not have been created ... create it first with createCodeletGroup",groupName);
}

/**
Expand All @@ -287,7 +290,8 @@ public void registerCodelet(Codelet co, String groupName) {
*/
public void registerMemory(Memory m, String groupName) {
ArrayList<Memory> groupList = memoryGroups.get(groupName);
if (groupList != null) groupList.add(m);
if (groupList != null) groupList.add(m);
else Logger.getAnonymousLogger().log(Level.INFO,"The Memory Group {0} still does not have been created ... create it first with createMemoryGroup",groupName);
}


Expand Down
Loading

0 comments on commit deb39d9

Please sign in to comment.