use of org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap in project narchy by automenta.
the class GraphMeter method strongly.
// --------------------------------------------------------------------
/**
* Tarjan: Returns the strongly connected cluster roots with size as a value.
* Cluster membership can be seen from the content of the array {@link #root};
* each node has the root of the strongly connected cluster it belongs to.
* A word of caution: for large graphs that have a large diameter and that
* are strongly connected (such as large rings) you can get stack overflow
* because of the large depth of recursion.
*/
// XXX implement a non-recursive version ASAP!!!
public IntIntHashMap strongly(Graph g) {
this.g = g;
stack.clear();
int size = g.size();
if (root == null || root.length < size)
root = new int[size];
if (color == null || color.length < size)
color = new int[size];
for (int i = 0; i < size; ++i) color[i] = WHITE;
counter = 1;
// color is negative (c<1): inComponent true
for (int i = 0; i < size; ++i) {
if (color[i] == WHITE)
tarjanVisit(i);
}
for (int i = 0; i < size; ++i) color[i] = 0;
for (int i = 0; i < size; ++i) color[root[i]]++;
IntIntHashMap ht = new IntIntHashMap();
for (int j = 0; j < size; ++j) {
int cj = color[j];
if (cj > 0) {
ht.put(j, cj);
}
}
return ht;
}
use of org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap in project narchy by automenta.
the class DFA method minimize.
private int[][] minimize(Map<Set<NFAState>, CharObjectHashMap<Set<NFAState>>> oriDFATransitionMap, Set<NFAState> initClosure, NFAState finalNFAState) {
Map<Integer, int[]> renamedDFATransitionTable = new HashMap<>();
Map<Integer, Boolean> finalFlags = new HashMap<>();
Map<Set<NFAState>, Integer> stateRenamingMap = new HashMap<>();
int initStateAfterRenaming = -1;
int renamingStateID = 1;
// rename all states
for (Set<NFAState> nfaState : oriDFATransitionMap.keySet()) {
if (initStateAfterRenaming == -1 && nfaState.equals(initClosure)) {
// record init state id
initStateAfterRenaming = renamingStateID;
}
stateRenamingMap.put(nfaState, renamingStateID++);
}
// the rejected state 0
renamedDFATransitionTable.put(0, newRejectState());
finalFlags.put(0, false);
// construct renamed dfa transition table
for (Map.Entry<Set<NFAState>, CharObjectHashMap<Set<NFAState>>> entry : oriDFATransitionMap.entrySet()) {
Set<NFAState> ek = entry.getKey();
renamingStateID = stateRenamingMap.get(ek);
int[] state = newRejectState();
CharObjectHashMap<Set<NFAState>> ev = entry.getValue();
ev.forEachKeyValue((k, v) -> state[k] = stateRenamingMap.get(v));
renamedDFATransitionTable.put(renamingStateID, state);
finalFlags.put(renamingStateID, ek.contains(finalNFAState));
}
// split states to final states and non-final states
IntIntHashMap groupFlags = new IntIntHashMap();
for (int i = 0; i < finalFlags.size(); i++) {
boolean b = finalFlags.get(i);
groupFlags.put(i, b ? 0 : 1);
}
int groupTotal = 2;
int preGroupTotal;
do {
// splitting, group id is the final state id
preGroupTotal = groupTotal;
for (int sensitiveGroup = 0; sensitiveGroup < preGroupTotal; sensitiveGroup++) {
// <target group table, state id set>
Map<Map<Integer, Integer>, Set<Integer>> invertMap = new HashMap<>();
for (int sid = 0; sid < groupFlags.size(); sid++) {
// use state id to iterate
int group = groupFlags.get(sid);
if (sensitiveGroup == group) {
Map<Integer, Integer> targetGroupTable = new HashMap<>(CommonSets.ENCODING_LENGTH);
for (char ch = 0; ch < CommonSets.ENCODING_LENGTH; ch++) {
int targetState = renamedDFATransitionTable.get(sid)[ch];
int targetGroup = groupFlags.get(targetState);
targetGroupTable.put((int) ch, targetGroup);
}
Set<Integer> stateIDSet = invertMap.computeIfAbsent(targetGroupTable, k -> new HashSet<>());
stateIDSet.add(sid);
}
}
boolean first = true;
for (Set<Integer> stateIDSet : invertMap.values()) {
if (first) {
first = false;
} else {
for (int sid : stateIDSet) {
groupFlags.put(sid, groupTotal);
}
groupTotal++;
}
}
}
} while (preGroupTotal != groupTotal);
// determine initial group state
is = groupFlags.get(initStateAfterRenaming);
// determine rejected group state
rs = groupFlags.get(0);
// determine final group states
Set<Integer> finalGroupFlags = new HashSet<>();
for (int i = 0, groupFlagsSize = groupFlags.size(); i < groupFlagsSize; i++) {
Integer groupFlag = groupFlags.get(i);
if (finalFlags.get(i)) {
finalGroupFlags.add(groupFlag);
}
}
boolean[] fs = this.fs = new boolean[groupTotal];
for (int i = 0; i < groupTotal; i++) {
fs[i] = finalGroupFlags.contains(i);
}
// construct the final transition table
int[][] tt = new int[groupTotal][];
for (int groupID = 0; groupID < groupTotal; groupID++) {
for (int sid = 0; sid < groupFlags.size(); sid++) {
if (groupID == groupFlags.get(sid)) {
int[] oriState = renamedDFATransitionTable.get(sid);
int[] state = new int[CommonSets.ENCODING_LENGTH];
for (char ch = 0; ch < CommonSets.ENCODING_LENGTH; ch++) {
int next = oriState[ch];
state[ch] = groupFlags.get(next);
}
tt[groupID] = state;
break;
}
}
}
return tt;
}
use of org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap in project narchy by automenta.
the class AdjGraphTest method testGraph1.
@Test
public void testGraph1() {
AdjGraph<String, String> g = new AdjGraph<>(true);
assertEquals(1, g.eid(0, 1));
assertEquals(4294967296L, g.eid(1, 0));
assertEquals(4294967297L, g.eid(1, 1));
int xIndex = g.addNode("x");
assertEquals(xIndex, g.addNode("x"));
g.addNode("y");
assertTrue(g.setEdge("x", "y", "xy"));
// duplicate
assertFalse(g.setEdge("x", "y", "xy"));
assertEquals("xy", g.edge("x", "y"));
assertFalse(g.setEdge("x", "z", "xz"));
g.setEdge("y", "x", "yx");
// unconnected
g.addNode("z");
assertTrue(g.removeEdge("x", "y"));
assertNull(g.edge("x", "y"));
g.writeGML(System.out);
GraphMeter m = new GraphMeter();
List<IntHashSet> wc = m.weakly(g);
// assertEquals("[[0, 1], [2]]", wc.toString());
// TODO strongly should return a similar kind of result as wc
IntIntHashMap tj = m.strongly(g);
System.out.println(tj);
}
use of org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap in project narchy by automenta.
the class AgentTest method testAgent.
static void testAgent(Agent agent) {
// assert(agent.inputs == 1); //not true via HaiQ perception
assert (agent.inputs >= 1);
assert (agent.actions == 2);
final float minRatio = 2f;
int cycles = 100;
float nextReward = 0;
IntIntHashMap acts = new IntIntHashMap();
for (int i = 0; i < cycles; i++) {
int action = agent.act(nextReward, new float[] { (float) Math.random() });
// System.out.println(a);
acts.addToValue(action, 1);
switch(action) {
case 0:
nextReward = -1.0f;
break;
case 1:
nextReward = +1.0f;
break;
default:
throw new UnsupportedOperationException();
}
}
System.out.println(agent.getClass() + " " + agent.summary() + "\n" + acts);
assertTrue(acts.get(1) > acts.get(0));
// at least 10:1
assertTrue(acts.get(1) / minRatio > acts.get(0));
}
use of org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap in project eclipse-collections by eclipse.
the class IntIntMapLargeStressTest method setUp.
@Setup
public void setUp() {
this.intIntKoloboke = HashIntIntMaps.newMutableMap(MAP_SIZE);
this.intIntEc = new IntIntHashMap(MAP_SIZE);
this.integerIntegerJdk = new HashMap<>(MAP_SIZE);
Random random = new Random(0x123456789ABCDL);
int[] randomNumbersForMap = this.getRandomKeys(random).toArray();
int number = 23;
int lower = Integer.MIN_VALUE;
int upper = Integer.MAX_VALUE;
this.kolobokeIntKeysForMap = this.fullyRandom ? randomNumbersForMap : this.getKolobokeArray(number, lower, upper, random);
this.ecIntKeysForMap = this.fullyRandom ? randomNumbersForMap : this.getECArray(number, lower, upper, random);
this.jdkIntKeysForMap = this.fullyRandom ? IntIntMapLargeStressTest.boxIntArray(randomNumbersForMap) : this.getJDKArray(lower, upper, random);
for (int i = 0; i < KEY_COUNT; i++) {
this.intIntKoloboke.put(this.kolobokeIntKeysForMap[i], 5);
this.intIntEc.put(this.ecIntKeysForMap[i], 5);
this.integerIntegerJdk.put(this.jdkIntKeysForMap[i], 5);
}
this.shuffle(this.ecIntKeysForMap, random);
this.shuffle(this.kolobokeIntKeysForMap, random);
this.shuffle(this.jdkIntKeysForMap, random);
}
Aggregations