source: tol/tol_pkg/GraphTools/_GraphTools.tol @ 23d43bc

1941-docker-image-to-testdevel
Last change on this file since 23d43bc was 883e960, checked in by Víctor de Buen <vdebuen@…>, 17 years ago

Commentary about pendent adjacency methods

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@1649 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 14.8 KB
Line 
1/* -*- mode: C++ -*- */
2//////////////////////////////////////////////////////////////////////////////
3// FILE   : graphtools.tol
4// PURPOSE: Defines NameBlock GraphTools
5//////////////////////////////////////////////////////////////////////////////
6
7//////////////////////////////////////////////////////////////////////////////
8NameBlock GraphTools =
9//////////////////////////////////////////////////////////////////////////////
10[[
11//////////////////////////////////////////////////////////////////////////////
12Text _.autodoc.member.Append.Indexed = "Adds and index a new element to a "
13"set and returns 0 if itŽs all right. If an element with given name exists "
14"already then returns its index, else returns 0";
15Real Append.Indexed(Set set, Anything newElement, Text name)
16//////////////////////////////////////////////////////////////////////////////
17{
18  Real idx = FindIndexByName(set, name );
19  If(idx != 0,
20  {
21    WriteLn("[GraphTools::Append.Indexed] Cannot add an existant element "+
22            Name(set)+"["<<idx+"] with name "+ name,"E");
23    0
24  },
25  {
26    Set aux =  [[ Copy(newElement) ]];
27    Anything PutName(name, aux[1]);
28    Set Append(set, aux, True);
29    idx
30  })
31};
32
33//////////////////////////////////////////////////////////////////////////////
34Class @Feature.Abstract
35//////////////////////////////////////////////////////////////////////////////
36{
37  Static Text _.autodoc.description = "Class for handling with abstract "
38  "features of arbitrary entities, numerical or not, that will be stored in "
39  "an external matrix with a row for each entity.";
40  //The name of the feature
41  Text _.name;
42  //The number of the feature in the full set of features
43  Real _.position;
44
45  ////////////////////////////////////////////////////////////////////////////
46  Static Text _.autodoc.member.get = "Returns the stored value corresponding "
47  "to this feature related to an individual entity.";
48  Anything get(Matrix store, Real entity)
49  ////////////////////////////////////////////////////////////////////////////
50};
51
52//////////////////////////////////////////////////////////////////////////////
53Class @Feature.Numeric : @Feature.Abstract
54//////////////////////////////////////////////////////////////////////////////
55{
56  Static Text _.autodoc.description = "Class for handling with numerical "
57  "numerical features of arbitrary entities that will be stored in an "
58  "external matrix with a row for each entity.";
59
60  ////////////////////////////////////////////////////////////////////////////
61  Static Text _.autodoc.member.New = "Creates a new instance of "
62  "Class @Feature.Numeric";
63  Static @Feature.Numeric New(Text name, Real position)
64  ////////////////////////////////////////////////////////////////////////////
65  {
66    @Feature.Numeric aux =
67    [[
68      Text _.name = name;
69      Real _.position  = position
70    ]]
71  };
72 
73  ////////////////////////////////////////////////////////////////////////////
74  Anything get(Matrix store, Real entity)
75  ////////////////////////////////////////////////////////////////////////////
76  {
77    MatDat(store, entity, _.position)
78  }
79};
80
81//////////////////////////////////////////////////////////////////////////////
82Class @Feature.NonNumeric : @Feature.Abstract
83//////////////////////////////////////////////////////////////////////////////
84{
85  Static Text _.autodoc.description = "Class for handling with numerical "
86  "numerical features of arbitrary entities that will be stored in an "
87  "external matrix with a row for each entity. Here the stored value is the "
88  "index of the feature stored in an arbitrary domain.";
89
90  //The set of valid values for this feature indexed by name
91  Set _.domain;
92
93  ////////////////////////////////////////////////////////////////////////////
94  Static Text _.autodoc.member.New = "Creates a new instance of "
95  "Class @Feature.NonNumeric";
96  Static @Feature.NonNumeric New(Text name, Real position, Set domain)
97  ////////////////////////////////////////////////////////////////////////////
98  {
99    @Feature.NonNumeric aux =
100    [[
101      Text _.name = name;
102      Real _.position  = position;
103      Set _.domain = domain   
104    ]];
105    Real SetIndexByName(aux::_.domain);
106    aux
107  };
108
109  ////////////////////////////////////////////////////////////////////////////
110  Anything get(Matrix store, Real entity)
111  ////////////////////////////////////////////////////////////////////////////
112  {
113    Real idx = MatDat(store, entity, _.position);
114    _.domain[idx]
115  }
116
117};
118
119//////////////////////////////////////////////////////////////////////////////
120Class @FeatureSet
121//////////////////////////////////////////////////////////////////////////////
122{
123  //Stores and index by name a set of instances od @Feature.Abstract
124  Set _.feature = Copy(Empty);
125  //In each row of this matrix are stored the values or indexes of features
126  //related to an individual entity
127  Matrix _.data = Rand(0,0,0,0);
128
129  ////////////////////////////////////////////////////////////////////////////
130  Static Text _.autodoc.member.New = "Creates a new instance of "
131  "Class @FeatureSet";
132  Static @FeatureSet New(Real void)
133  ////////////////////////////////////////////////////////////////////////////
134  {
135    @FeatureSet aux
136  };
137
138  ////////////////////////////////////////////////////////////////////////////
139  Static Text _.autodoc.member.add.feature.numeric =
140  "Adds a new numerical feature";
141  Real add.feature.numeric(Text name)
142  ////////////////////////////////////////////////////////////////////////////
143  {
144    GraphTools::Append.Indexed(
145      _.feature,
146      @Feature.Numeric::New(name, Card(_.feature)+1),
147      name)
148  };
149
150  ////////////////////////////////////////////////////////////////////////////
151  Static Text _.autodoc.member.add.feature.nonnumeric=
152  "Adds a new non numerical feature";
153  Real add.feature.nonnumeric(Text name, Set domain)
154  ////////////////////////////////////////////////////////////////////////////
155  {
156    GraphTools::Append.Indexed(
157      _.feature,
158      @Feature.NonNumeric::New(name, Card(_.feature)+1, domain),
159      name)
160  };
161
162  ////////////////////////////////////////////////////////////////////////////
163  Text _.autodoc.member.feature.index = "Returns the index of a feature or 0 "
164  "if it not exists";
165  Real feature.index(Text name)
166  ////////////////////////////////////////////////////////////////////////////
167  {
168    FindIndexByName(_.feature, name )
169  };
170
171  ////////////////////////////////////////////////////////////////////////////
172  Text _.autodoc.member.get = "Returns the value of a feature for an entity";
173  Anything get(Real entity,
174               Anything feature) //Real position or Text name
175  ////////////////////////////////////////////////////////////////////////////
176  {
177    @Feature.Abstract f = _.feature[feature];
178    f::get(_.data, entity)
179  };
180
181  ////////////////////////////////////////////////////////////////////////////
182  Text _.autodoc.member.get.all = "Returns a set with the values of all "
183  "features for an entity";
184  Set get.all(Real entity)
185  ////////////////////////////////////////////////////////////////////////////
186  {
187    EvalSet(_.feature, Anything get.all.cycle(@FeatureSet f)
188    {
189      f::get(_.data, entity)
190    })
191  }
192
193};
194
195
196//////////////////////////////////////////////////////////////////////////////
197Class @Graph
198//////////////////////////////////////////////////////////////////////////////
199{
200  Text _.autodoc.description = "Class for handling with generic indexed by "
201  "name graphs in TOL. In abstract, a graph is a pair of sets \n"
202  "  V : a finite set of vertices \n"
203  "  E : a finite set of edges relating an input vertex with an output \n"
204  "Each vertex has a set of features related with the system\n"
205  "Each edge also has its own set of features.\n";
206
207  //The identifier of the graph
208  Text _.name;
209  //Indicates if the graph is directed or symmetric
210  Real _.directed;
211  //Features of vertices. First feature must be the identifier of vertices
212  @FeatureSet _.vertex = @FeatureSet::New(?);
213  //Number of vertices
214  Real _.vertex.num = 0; 
215  //Features of edges. First two features must be input and output vertex
216  @FeatureSet _.edge = @FeatureSet::New(?);
217  //Number of edges
218  Real _.edge.num = 0; 
219  //Sparse matrix storing index of edge for each cell (out,in)
220  VMatrix _.edge.map = Rand(0,0,0,0);
221  VMatrix _.edge.adjacency = Rand(0,0,0,0);
222  Real _.edge.check.rep = ?;
223
224  ////////////////////////////////////////////////////////////////////////////
225  Static Text _.autodoc.member.New = "Creates a new instance of "
226  "Class @Graph";
227  Static @Graph New(Text name, Real directed)
228  ////////////////////////////////////////////////////////////////////////////
229  {
230    @Graph aux =
231    [[
232      Text _.name = name;
233      Real _.directed = directed
234    ]]
235  };
236
237  ////////////////////////////////////////////////////////////////////////////
238  Real initialize(Set vertex.names)
239  ////////////////////////////////////////////////////////////////////////////
240  {
241    Real _.vertex.num := Card(vertex.names);
242    Set For(1,_.vertex.num, Real(Real k)
243    {
244      Text PutName(vertex.names[k], vertex.names[k]);
245      k
246    });
247    Real _.vertex::add.feature.nonnumeric("Identifier", vertex.names);
248    Matrix _.vertex::_.data := DifEq(1/(1-B),Rand(_.vertex.num,1,1,1));
249    Real _.edge::add.feature.numeric("Output");
250    Real _.edge::add.feature.numeric("Input");
251    True
252  };
253
254  ////////////////////////////////////////////////////////////////////////////
255  Text _.autodoc.member.edge.build = "";
256  Real edge.build(Matrix data)
257  ////////////////////////////////////////////////////////////////////////////
258  {
259    Real _.edge.num := Rows(data);
260    Matrix out_ = SubCol(data,[[1]]);
261    Matrix in_  = SubCol(data,[[2]]);
262    Matrix out  = If(_.directed, out_, Min(in_, out_));
263    Matrix in   = If(_.directed, in_,  Max(in_, out_));
264    Matrix _.edge::_.data := out | in | SubCol(data,Range(3,Columns(data),1));
265    Matrix idx = DifEq(1/(1-B),Rand(_.edge.num,1,1,1));
266    VMatrix triplet = Triplet(out|in|idx,_.vertex.num,_.vertex.num);
267    VMatrix _.edge.map := Convert(triplet,"Cholmod.R.Sparse");
268    Matrix idx.rep = Rand(_.edge.num,1,1,1);
269    VMatrix triplet.rep = Triplet(out|in|idx.rep,_.vertex.num,_.vertex.num);
270    VMatrix _.edge.adjacency := Convert(triplet.rep,"Cholmod.R.Sparse");
271    Real _.edge.check.rep := VMatMax(_.edge.adjacency)>1;
272    If(_.edge.check.rep,
273    {
274      WriteLn("[GraphTools::@Graph "+_.name+"::edge.build()] "+
275              "There are repeated edges","W")
276    });
277    True
278  };
279
280  ////////////////////////////////////////////////////////////////////////////
281  Text _.autodoc.member.get.vertex.names = "Returns the names of all "
282  "vertices";
283  Set get.vertex.names(Real void)
284  ////////////////////////////////////////////////////////////////////////////
285  {
286    ((_.vertex::_.feature)[1])::_.domain
287  };
288
289  ////////////////////////////////////////////////////////////////////////////
290  Text _.autodoc.member.get.vertex.name = "Returns the name of one "
291  "vertex";
292  Text get.vertex.name(Real idx)
293  ////////////////////////////////////////////////////////////////////////////
294  {
295    (((_.vertex::_.feature)[1])::_.domain)[idx]
296  };
297
298  ////////////////////////////////////////////////////////////////////////////
299  Text _.autodoc.member.get.vertex.index = "Returns the index of a vertex or 0 "
300  "if it not exists";
301  Real get.vertex.index(Text name)
302  ////////////////////////////////////////////////////////////////////////////
303  {
304    FindIndexByName(((_.vertex::_.feature)[1])::_.domain, name )
305  };
306
307  ////////////////////////////////////////////////////////////////////////////
308  Text _.autodoc.member.get.vertex.features = "Returns all the features for "
309  "an vertex";
310  Set get.vertex.features(Real vertex.idx)
311  ////////////////////////////////////////////////////////////////////////////
312  {
313    _.vertex::get.all(vertex.idx)
314  };
315
316  ////////////////////////////////////////////////////////////////////////////
317  Text _.autodoc.member.get.vertex.feature = "Returns specified feature for "
318  "an vertex. Feature identifier can be the name (Text) or the position "
319  "(Real)";
320  Anything get.vertex.feature(
321    Real vertex.idx,
322    Anything feature.id) //Text name or Real num
323  ////////////////////////////////////////////////////////////////////////////
324  {
325    _.edge::get(vertex.idx, feature.id)
326  };
327
328  ////////////////////////////////////////////////////////////////////////////
329  Text _.autodoc.member.get.edge.index = "Returns the index of an edge or 0 "
330  "if it not exists";
331  Real get.edge.index(Real out_, Real in_)
332  ////////////////////////////////////////////////////////////////////////////
333  {
334    Real out  = If(_.directed, out_, Min(in_, out_));
335    Real in   = If(_.directed, in_,  Max(in_, out_));
336    Real edge.idx = VMatDat(_.edge.map, out, in);
337    edge.idx / VMatDat(_.edge.adjacency, out, in)
338  };
339
340  ////////////////////////////////////////////////////////////////////////////
341  Text _.autodoc.member.edge.exists = "Returns true if exists the edge from "
342  "out_ to in_";
343  Real edge.exists(Real out_, Real in_)
344  ////////////////////////////////////////////////////////////////////////////
345  {
346    get.edge.index(out_, in_)!=0
347  };
348
349  ////////////////////////////////////////////////////////////////////////////
350  Text _.autodoc.member.get.edge.features = "Returns all the features for "
351  "an edge";
352  Set get.edge.features(Real edge.idx)
353  ////////////////////////////////////////////////////////////////////////////
354  {
355    _.edge::get.all(edge.idx)
356  };
357
358  ////////////////////////////////////////////////////////////////////////////
359  Text _.autodoc.member.get.edge.feature = "Returns specified feature for "
360  "an edge. Feature identifier can be the name (Text) or the position (Real";
361  Anything get.edge.feature(
362    Real edge.idx,
363    Anything feature.id) //Text name or Real num
364  ////////////////////////////////////////////////////////////////////////////
365  {
366    _.edge::get(edge.idx, feature.id)
367  }
368
369/*
370PENDIENTE! : Funciones de adyacencia o vecindad de vértices
371
372http://www.absoluteastronomy.com/topics/Adjacency_matrix
373
374////////////////////////////////////////////////////////////////////////////
375Crea las matrices de adyancencia hasta un orden pedido por el usuario y las
376guarda en un set. Sólo crea las que no existan aún. Sólo devuelve la del
377orden solicitado.
378Matrix get.adyacency(Real order)
379////////////////////////////////////////////////////////////////////////////
380
381////////////////////////////////////////////////////////////////////////////
382Devuelve los vértices adyacentes de salida de orden dado para el vértice
383especificado.  V -> ... (n-1) ... -> U
384Set get.adyacency.output(Real V, Real order)
385////////////////////////////////////////////////////////////////////////////
386
387////////////////////////////////////////////////////////////////////////////
388Devuelve los vértices adyacentes U de entrada de orden dado para el vértice
389especificado.  U -> ... (n-1) ... -> V
390Set get.adyacency.input(Real V, Real order)
391////////////////////////////////////////////////////////////////////////////
392
393*/
394
395}
396
397]];
Note: See TracBrowser for help on using the repository browser.