Skip to content

Commit a4f3aad

Browse files
committed
Corrected Moran library model and function input consistency test
There was a confusion in the library model between the weight matrix and the gaml grid. The error in the consistency test was due to being tested on the library model. Also added a neighbours spatial weight case. Fairly common with Moran index and easy to test.
1 parent 9b2d5ac commit a4f3aad

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

msi.gama.core/src/msi/gaml/operators/Spatial.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7261,7 +7261,7 @@ public static double moranIndex(final IScope scope, final IList<Double> vals, fi
72617261
if (weightMatrix == null || weightMatrix.numCols != weightMatrix.numRows) throw GamaRuntimeException
72627262
.error("A squared weight matrix should be given for the moran index computation", scope);
72637263
final int N = vals.size();
7264-
if (N != weightMatrix.numRows * weightMatrix.numCols) throw GamaRuntimeException
7264+
if (N != weightMatrix.numRows) throw GamaRuntimeException
72657265
.error("The lengths of the value list and of the weight matrix do not match", scope);
72667266
double I = 0.0;
72677267
double sumWeights = 0.0;

msi.gama.models/models/Modeling/Spatial Topology/Spatial Operators/models/Moran Index.gaml

+20-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ model moranIndex
99

1010
global {
1111
string grid_spatial_init <- "random" among: ["random", "checkerboard", "blocks"];
12-
string weight_type <- "overlapping" among: ["overlapping", "distance"];
12+
string weight_type <- "neighbors" among: ["overlapping", "neighbors", "distance"];
1313
int grid_size <- 20;
1414

1515
list<float> vals;
@@ -27,30 +27,34 @@ global {
2727
}
2828
vals <- cell collect (each.color = #white ? 0.0 : 1.0);
2929
weights <- 0.0 as_matrix {grid_size, grid_size};
30-
loop i from: 0 to: grid_size -1 {
31-
loop j from: 0 to:grid_size -1 {
32-
if (i = j) {weights[i,j] <- 0.0;}
33-
else {
34-
switch weight_type {
35-
match "overlapping" {
36-
weights[i,j] <- (cell[i] overlaps cell[j]) ? 1.0 : 0.0;
30+
ask cell {
31+
switch weight_type {
32+
match "neighbors" {
33+
ask self.neighbors {
34+
weights[int(self), int(myself)] <- 1.0;
35+
}
36+
}
37+
match "overlapping" {
38+
ask cell overlapping self {
39+
if self != myself {
40+
weights[int(self), int(myself)] <- 1.0;
3741
}
38-
match "distance" {
39-
using topology(cell) {
40-
weights[i,j] <- 1/(cell[i] distance_to cell[j]);
41-
}
42+
}
43+
}
44+
match "distance" {
45+
ask cell {
46+
if self != myself {
47+
weights[int(self), int(myself)] <- 1/(self distance_to myself);
4248
}
43-
4449
}
45-
46-
}
50+
}
4751
}
4852
}
4953
I <- moran(vals, weights);
5054
write "moran I: " + I;
5155
}
5256
}
53-
grid cell width: grid_size height: grid_size neighbors: 8;
57+
grid cell width: grid_size height: grid_size neighbors: 4;
5458

5559
experiment "Moran" type: gui {
5660
parameter "grid size: " var: grid_size min: 2 max: 100;

0 commit comments

Comments
 (0)