source: tol/tol_pkg/GraphTools/_GraphTools.tol @ 176aa3e

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

New methods get.vertex.feature.index and get.edge.feature.index

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

  • Property mode set to 100644
File size: 15.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  VMatrix _.edge.rep = Rand(0,0,0,0);
223  Real _.edge.check.rep = ?;
224
225  ////////////////////////////////////////////////////////////////////////////
226  Static Text _.autodoc.member.New = "Creates a new instance of "
227  "Class @Graph";
228  Static @Graph New(Text name, Real directed)
229  ////////////////////////////////////////////////////////////////////////////
230  {
231    @Graph aux =
232    [[
233      Text _.name = name;
234      Real _.directed = directed
235    ]]
236  };
237
238  ////////////////////////////////////////////////////////////////////////////
239  Real initialize(Set vertex.names)
240  ////////////////////////////////////////////////////////////////////////////
241  {
242    Real _.vertex.num := Card(vertex.names);
243    Set For(1,_.vertex.num, Real(Real k)
244    {
245      Text PutName(vertex.names[k], vertex.names[k]);
246      k
247    });
248    Real _.vertex::add.feature.nonnumeric("Identifier", vertex.names);
249    Matrix _.vertex::_.data := DifEq(1/(1-B),Rand(_.vertex.num,1,1,1));
250    Real _.edge::add.feature.numeric("Output");
251    Real _.edge::add.feature.numeric("Input");
252    True
253  };
254
255  ////////////////////////////////////////////////////////////////////////////
256  Text _.autodoc.member.edge.build = "";
257  Real edge.build(Matrix data)
258  ////////////////////////////////////////////////////////////////////////////
259  {
260    Real _.edge.num := Rows(data);
261    Matrix out_ = SubCol(data,[[1]]);
262    Matrix in_  = SubCol(data,[[2]]);
263    Matrix out  = If(_.directed, out_, Min(in_, out_));
264    Matrix in   = If(_.directed, in_,  Max(in_, out_));
265    Matrix _.edge::_.data := out | in | SubCol(data,Range(3,Columns(data),1));
266
267    Matrix idx = DifEq(1/(1-B),Rand(_.edge.num,1,1,1));
268    Matrix idx.rep = Rand(_.edge.num,1,1,1);
269    Matrix outInRep = out|in|idx.rep;
270    VMatrix triplet.rep = Triplet(outInRep,_.vertex.num,_.vertex.num);
271    VMatrix _.edge.rep := Convert(triplet.rep,"Cholmod.R.Sparse");
272    Real _.edge.check.rep := VMatMax(_.edge.rep)>1;
273    VMatrix triplet.map = If(!_.edge.check.rep,
274    {
275      VMatrix _.edge.adjacency := _.edge.rep;
276      Triplet(out|in|idx,_.vertex.num,_.vertex.num)
277    },
278    {
279      WriteLn("[GraphTools::@Graph "+_.name+"::edge.build()] "+
280              "There are repeated edges","W");
281      VMatrix triplet.adj = TripletUnique(outInRep,_.vertex.num,_.vertex.num);
282      VMatrix _.edge.adjacency := Convert(triplet.adj,"Cholmod.R.Sparse");
283      TripletUnique(out|in|idx,_.vertex.num,_.vertex.num)
284    });
285    VMatrix _.edge.map := Convert(triplet.map,"Cholmod.R.Sparse");
286    True
287  };
288
289  ////////////////////////////////////////////////////////////////////////////
290  Text _.autodoc.member.get.vertex.names = "Returns the names of all "
291  "vertices";
292  Set get.vertex.names(Real void)
293  ////////////////////////////////////////////////////////////////////////////
294  {
295    ((_.vertex::_.feature)[1])::_.domain
296  };
297
298  ////////////////////////////////////////////////////////////////////////////
299  Text _.autodoc.member.get.vertex.name = "Returns the name of one "
300  "vertex";
301  Text get.vertex.name(Real idx)
302  ////////////////////////////////////////////////////////////////////////////
303  {
304    (((_.vertex::_.feature)[1])::_.domain)[idx]
305  };
306
307  ////////////////////////////////////////////////////////////////////////////
308  Text _.autodoc.member.get.vertex.index = "Returns the index of a vertex or 0 "
309  "if it not exists";
310  Real get.vertex.index(Text name)
311  ////////////////////////////////////////////////////////////////////////////
312  {
313    FindIndexByName(((_.vertex::_.feature)[1])::_.domain, name )
314  };
315
316  ////////////////////////////////////////////////////////////////////////////
317  Text _.autodoc.member.get.vertex.feature.index = "Returns the index of an "
318  "vertex feature or 0 if it not exists";
319  Real get.vertex.feature.index(Text name)
320  ////////////////////////////////////////////////////////////////////////////
321  {
322    FindIndexByName(_.vertex::_.feature, name )
323  };
324
325  ////////////////////////////////////////////////////////////////////////////
326  Text _.autodoc.member.get.vertex.features = "Returns all the features for "
327  "an vertex";
328  Set get.vertex.features(Real vertex.idx)
329  ////////////////////////////////////////////////////////////////////////////
330  {
331    _.vertex::get.all(vertex.idx)
332  };
333
334  ////////////////////////////////////////////////////////////////////////////
335  Text _.autodoc.member.get.vertex.feature = "Returns specified feature for "
336  "an vertex. Feature identifier can be the name (Text) or the position "
337  "(Real)";
338  Anything get.vertex.feature(
339    Real vertex.idx,
340    Anything feature.id) //Text name or Real num
341  ////////////////////////////////////////////////////////////////////////////
342  {
343    _.edge::get(vertex.idx, feature.id)
344  };
345
346  ////////////////////////////////////////////////////////////////////////////
347  Text _.autodoc.member.get.edge.index = "Returns the index of an edge or 0 "
348  "if it not exists";
349  Real get.edge.index(Real out_, Real in_)
350  ////////////////////////////////////////////////////////////////////////////
351  {
352    Real out  = If(_.directed, out_, Min(in_, out_));
353    Real in   = If(_.directed, in_,  Max(in_, out_));
354    Real edge.idx = VMatDat(_.edge.map, out, in);
355    edge.idx
356  };
357
358  ////////////////////////////////////////////////////////////////////////////
359  Text _.autodoc.member.edge.exists = "Returns true if exists the edge from "
360  "out_ to in_";
361  Real edge.exists(Real out_, Real in_)
362  ////////////////////////////////////////////////////////////////////////////
363  {
364    get.edge.index(out_, in_)!=0
365  };
366
367  ////////////////////////////////////////////////////////////////////////////
368  Text _.autodoc.member.get.edge.feature.index = "Returns the index of an "
369  "edge feature or 0 if it not exists";
370  Real get.edge.feature.index(Text name)
371  ////////////////////////////////////////////////////////////////////////////
372  {
373    FindIndexByName(_.edge::_.feature, name )
374  };
375
376  ////////////////////////////////////////////////////////////////////////////
377  Text _.autodoc.member.get.edge.features = "Returns all the features for "
378  "an edge";
379  Set get.edge.features(Real edge.idx)
380  ////////////////////////////////////////////////////////////////////////////
381  {
382    _.edge::get.all(edge.idx)
383  };
384
385  ////////////////////////////////////////////////////////////////////////////
386  Text _.autodoc.member.get.edge.feature = "Returns specified feature for "
387  "an edge. Feature identifier can be the name (Text) or the position (Real";
388  Anything get.edge.feature(
389    Real edge.idx,
390    Anything feature.id) //Text name or Real num
391  ////////////////////////////////////////////////////////////////////////////
392  {
393    _.edge::get(edge.idx, feature.id)
394  }
395
396/*
397PENDIENTE! : Funciones de adyacencia o vecindad de vértices
398
399http://www.absoluteastronomy.com/topics/Adjacency_matrix
400
401////////////////////////////////////////////////////////////////////////////
402Crea las matrices de adyancencia hasta un orden pedido por el usuario y las
403guarda en un set. Sólo crea las que no existan aún. Sólo devuelve la del
404orden solicitado.
405Matrix get.adyacency(Real order)
406////////////////////////////////////////////////////////////////////////////
407
408////////////////////////////////////////////////////////////////////////////
409Devuelve los vértices adyacentes de salida de orden dado para el vértice
410especificado.  V -> ... (n-1) ... -> U
411Set get.adyacency.output(Real V, Real order)
412////////////////////////////////////////////////////////////////////////////
413
414////////////////////////////////////////////////////////////////////////////
415Devuelve los vértices adyacentes U de entrada de orden dado para el vértice
416especificado.  U -> ... (n-1) ... -> V
417Set get.adyacency.input(Real V, Real order)
418////////////////////////////////////////////////////////////////////////////
419
420*/
421
422}
423
424]];
Note: See TracBrowser for help on using the repository browser.