File: igraph_moran_process.c

package info (click to toggle)
igraph 0.7.1-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,252 kB
  • sloc: ansic: 188,056; sh: 26,731; cpp: 18,275; yacc: 1,164; makefile: 959; lex: 484; xml: 405
file content (221 lines) | stat: -rw-r--r-- 8,079 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* -*- mode: C -*-  */
/*
  Test suite for the Moran process in a network setting.
  Copyright (C) 2011 Minh Van Nguyen <nguyenminh2@gmail.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301 USA
*/

#include <assert.h>
#include <igraph.h>
#include <stdio.h>

/* test parameters structure */
typedef struct {
  igraph_t *graph;
  igraph_vector_t *weights;
  igraph_vector_t *quantities;
  igraph_vector_t *strategies;
  igraph_neimode_t mode;
  int retval;
} strategy_test_t;

/* Error tests, i.e. we expect errors to be raised for each test.
 */
int error_tests() {
  igraph_t g, gzero, h;
  igraph_vector_t quant, quantnvert, quantzero;
  igraph_vector_t strat, stratnvert, stratzero;
  igraph_vector_t wgt, wgtnedge, wgtzero;
  int i, n, nvert, ret;
  strategy_test_t *test;

  igraph_empty(&h, 0, 0);  /* empty graph */
  /* nonempty graph */
  igraph_small(&g, /*nvert=*/ 0, IGRAPH_UNDIRECTED, 0,1, 1,2, 2,0, -1);
  nvert = igraph_vcount(&g);
  /* weights vectors */
  igraph_vector_init(&wgt, 0);
  igraph_vector_init(&wgtnedge, igraph_ecount(&g));
  /* quantities vectors */
  igraph_vector_init(&quant, 1);
  igraph_vector_init_real(&quantnvert, nvert, 0.1, 0.2, 0.3);
  /* strategies vectors */
  igraph_vector_init(&strat, 2);
  igraph_vector_init_real(&stratnvert, nvert, 0.0, 1.0, 2.0);

  igraph_small(&gzero, /*nvert=*/ 0, IGRAPH_UNDIRECTED,
               0,3, 0,4, 1,2, 1,4, 1,5, 2,3, 2,4, 3,4, -1);
  nvert = igraph_vcount(&gzero);
  igraph_vector_init(&quantzero, nvert);                /* vector of zeros */
  igraph_vector_init(&stratzero, nvert);                /* vector of zeros */
  igraph_vector_init(&wgtzero, igraph_ecount(&gzero));  /* vector of zeros */
  /* igraph_vector_init_real(&stratzero, nvert, 1.0, 0.0, 1.0, 2.0, 0.0, 3.0); */

  /* test parameters */
  /*------graph--weights--quantities--strategies--mode--retval------*/
  /* null pointer for graph */
  strategy_test_t null_graph = {NULL, NULL, NULL, NULL, IGRAPH_ALL, IGRAPH_EINVAL};
  /* null pointer for weights vector */
  strategy_test_t null_wgt = {&g, NULL, &quantnvert, &stratnvert, IGRAPH_ALL, IGRAPH_EINVAL};
  /* null pointer for quantities vector */
  strategy_test_t null_quant = {&g, &wgt, NULL, NULL, IGRAPH_ALL, IGRAPH_EINVAL};
  /* null pointer for strategies vector */
  strategy_test_t null_strat = {&g, &wgt, &quant, NULL, IGRAPH_ALL, IGRAPH_EINVAL};
  /* empty graph */
  strategy_test_t empty_graph = {&h, &wgt, &quant, &strat, IGRAPH_ALL, IGRAPH_EINVAL};
  /* length of quantities vector different from number of vertices */
  strategy_test_t qdiff_length = {&g, &wgtnedge, &quant, &strat, IGRAPH_ALL, IGRAPH_EINVAL};
  /* length of strategies vector different from number of vertices */
  strategy_test_t sdiff_length = {&g, &wgtnedge, &quantnvert, &strat, IGRAPH_ALL, IGRAPH_EINVAL};
  /* length of weights vector different from number of edges */
  strategy_test_t wdiff_length = {&g, &wgt, &quantnvert, &stratnvert, IGRAPH_ALL, IGRAPH_EINVAL};
  /* weights vector contains all zeros */
  strategy_test_t zero_wgt = {&g, &wgtnedge, &quantnvert, &stratnvert, IGRAPH_ALL, IGRAPH_EINVAL};
  /* quantities vector contains all zeros */
  strategy_test_t zero_quant = {&gzero, &wgtzero, &quantzero, &stratzero, IGRAPH_ALL, IGRAPH_EINVAL};
  strategy_test_t *all_checks[] = {/* 1 */  &null_graph,
                                   /* 2 */  &null_quant,
                                   /* 3 */  &null_strat,
                                   /* 4 */  &null_wgt,
                                   /* 5 */  &empty_graph,
                                   /* 6 */  &qdiff_length,
                                   /* 7 */  &sdiff_length,
                                   /* 8 */  &wdiff_length,
                                   /* 9 */  &zero_quant,
                                   /* 10 */ &zero_wgt};

  /* Run the error tests. We expect error to be raised for each test. */
  igraph_set_error_handler(igraph_error_handler_ignore);
  n = 10;
  i = 0;
  while (i < n) {
    test = all_checks[i];
    ret = igraph_moran_process(test->graph, test->weights, test->quantities,
                               test->strategies, test->mode);
    if (ret != test->retval) {
      printf("Error test no. %d failed.\n", (int)(i + 1));
      return IGRAPH_FAILURE;
    }
    i++;
  }
  /* clean up */
  igraph_destroy(&g);
  igraph_destroy(&gzero);
  igraph_destroy(&h);
  igraph_vector_destroy(&quant);
  igraph_vector_destroy(&quantnvert);
  igraph_vector_destroy(&quantzero);
  igraph_vector_destroy(&strat);
  igraph_vector_destroy(&stratnvert);
  igraph_vector_destroy(&stratzero);
  igraph_vector_destroy(&wgt);
  igraph_vector_destroy(&wgtnedge);
  igraph_vector_destroy(&wgtzero);

  return IGRAPH_SUCCESS;
}

/* One iteration of the Moran process on a simple digraph.
 */
int moran_one_test() {
  igraph_t g;
  igraph_integer_t u = -1;  /* vertex chosen for reproduction */
  igraph_integer_t v = -1;  /* clone of u */
  igraph_integer_t nedge, nvert;
  igraph_real_t q = 0.0;
  igraph_vector_t quant, quantcp;
  igraph_vector_t strat, stratcp;
  igraph_vector_t wgt;
  long int i;

  /* graph representing the game network; quantities and strategies vectors */
  igraph_small(&g, /*nvert*/ 0, IGRAPH_DIRECTED,
               0,1, 0,4, 1,2, 1,4, 2,1, 3,2, 4,0, 4,3, -1);
  nvert = igraph_vcount(&g);
  nedge = igraph_ecount(&g);
  igraph_vector_init_real(&quant, nvert, 0.77, 0.83, 0.64, 0.81, 0.05);
  igraph_vector_init_real(&strat, nvert, 2.0, 0.0, 0.0, 1.0, 2.0);
  /* Set the edge weights. Here we assume the following correspondence */
  /* between edge IDs and directed edges: */
  /* edge 0: 0 -> 1 */
  /* edge 1: 0 -> 4 */
  /* edge 2: 1 -> 2 */
  /* edge 3: 1 -> 4 */
  /* edge 4: 2 -> 1 */
  /* edge 5: 3 -> 2 */
  /* edge 6: 4 -> 0 */
  /* edge 7: 4 -> 3 */
  igraph_vector_init_real(&wgt, nedge, 1.9, 0.8, 6.2, 2.4, 1.1, 5.2, 7.3, 8.8);

  /* play game */
  igraph_vector_copy(&quantcp, &quant);
  igraph_vector_copy(&stratcp, &strat);
  igraph_moran_process(&g, &wgt, &quantcp, &stratcp, IGRAPH_OUT);

  /* Determine which vertex was chosen for death. The original quantities */
  /* vector contain unique values, i.e. no duplicates. Thus we compare the */
  /* updated quantities with the original one. */
  for (i = 0; i < igraph_vector_size(&quant); i++) {
    if (VECTOR(quant)[i] != VECTOR(quantcp)[i]) {
      /* found the new clone vertex */
      v = (igraph_integer_t)i;
      q = (igraph_real_t)VECTOR(quantcp)[i];
      break;
    }
  }
  assert(v >= 0);
  assert(q != 0.0);

  /* Now we know that v is a clone of some vertex. Determine the vertex that */
  /* v is a clone of. */
  for (i = 0; i < igraph_vector_size(&quant); i++) {
    if (VECTOR(quant)[i] == q) {
      /* found the vertex chosen for reproduction */
      u = (igraph_integer_t)i;
      break;
    }
  }
  assert(u >= 0);

  /* check that v is indeed a clone of u */
  if (VECTOR(quant)[u] != VECTOR(quantcp)[v])
    return IGRAPH_FAILURE;
  if (VECTOR(strat)[u] != VECTOR(stratcp)[v])
    return IGRAPH_FAILURE;

  igraph_destroy(&g);
  igraph_vector_destroy(&quant);
  igraph_vector_destroy(&quantcp);
  igraph_vector_destroy(&strat);
  igraph_vector_destroy(&stratcp);
  igraph_vector_destroy(&wgt);

  return IGRAPH_SUCCESS;
}

int main() {
  int ret;

  ret = error_tests();
  if (ret)
    return IGRAPH_FAILURE;
  ret = moran_one_test();
  if (ret)
    return IGRAPH_FAILURE;

  return IGRAPH_SUCCESS;
}