source: tol/tol_pkg/GraphTools/_GraphTools.tol @ 8d2a4f1

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

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

  • Property mode set to 100644
File size: 13.7 KB
RevLine 
[571deda]1/* -*- mode: C++ -*- */
2//////////////////////////////////////////////////////////////////////////////
[f359e02]3// FILE   : graphtools.tol
[571deda]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  {
[dc274ac]21    WriteLn("[GraphTools::Append.Indexed] Cannot add an existant element "+
22            Name(set)+"["<<idx+"] with name "+ name,"E");
[571deda]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.";
[dc274ac]48  Anything get(Matrix store, Real entity)
[571deda]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  {
[dc274ac]66    @Feature.Numeric aux =
[571deda]67    [[
68      Text _.name = name;
69      Real _.position  = position
[dc274ac]70    ]]
[571deda]71  };
72 
73  ////////////////////////////////////////////////////////////////////////////
[dc274ac]74  Anything get(Matrix store, Real entity)
[571deda]75  ////////////////////////////////////////////////////////////////////////////
76  {
[dc274ac]77    MatDat(store, entity, _.position)
[571deda]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  ////////////////////////////////////////////////////////////////////////////
[dc274ac]110  Anything get(Matrix store, Real entity)
[571deda]111  ////////////////////////////////////////////////////////////////////////////
112  {
[dc274ac]113    Real idx = MatDat(store, entity, _.position);
[571deda]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
[dc274ac]127  Matrix _.data = Rand(0,0,0,0);
[571deda]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  ////////////////////////////////////////////////////////////////////////////
[dc274ac]139  Static Text _.autodoc.member.add.feature.numeric =
140  "Adds a new numerical feature";
[571deda]141  Real add.feature.numeric(Text name)
142  ////////////////////////////////////////////////////////////////////////////
143  {
144    GraphTools::Append.Indexed(
145      _.feature,
[dc274ac]146      @Feature.Numeric::New(name, Card(_.feature)+1),
[571deda]147      name)
148  };
149
150  ////////////////////////////////////////////////////////////////////////////
[dc274ac]151  Static Text _.autodoc.member.add.feature.nonnumeric=
152  "Adds a new non numerical feature";
[571deda]153  Real add.feature.nonnumeric(Text name, Set domain)
154  ////////////////////////////////////////////////////////////////////////////
155  {
156    GraphTools::Append.Indexed(
157      _.feature,
[dc274ac]158      @Feature.NonNumeric::New(name, Card(_.feature)+1, domain),
[571deda]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  {
[f7b99ac]177    @Feature.Abstract f = _.feature[feature];
178    f::get(_.data, entity)
[571deda]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  {
[f7b99ac]187    EvalSet(_.feature, Anything get.all.cycle(@FeatureSet f)
188    {
189      f::get(_.data, entity)
190    })
[571deda]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"
[dc274ac]203  "  E : a finite set of edges relating an input vertex with an output \n"
[571deda]204  "Each vertex has a set of features related with the system\n"
[dc274ac]205  "Each edge also has its own set of features.\n";
[571deda]206
207  //The identifier of the graph
208  Text _.name;
[f7b99ac]209  //Indicates if the graph is directed or symmetric
210  Real _.directed;
[dc274ac]211  //Features of vertices. First feature must be the identifier of vertices
212  @FeatureSet _.vertex = @FeatureSet::New(?);
[571deda]213  //Number of vertices
[dc274ac]214  Real _.vertex.num = 0; 
215  //Features of edges. First two features must be input and output vertex
216  @FeatureSet _.edge = @FeatureSet::New(?);
[571deda]217  //Number of edges
[dc274ac]218  Real _.edge.num = 0; 
[571deda]219  //Sparse matrix storing index of edge for each cell (out,in)
220  VMatrix _.edge.map = Rand(0,0,0,0);
[f7b99ac]221  VMatrix _.edge.map.rep = Rand(0,0,0,0);
222  Real _.edge.check.rep = ?;
223
[571deda]224  ////////////////////////////////////////////////////////////////////////////
[dc274ac]225  Static Text _.autodoc.member.New = "Creates a new instance of "
226  "Class @Graph";
[f7b99ac]227  Static @Graph New(Text name, Real directed)
[571deda]228  ////////////////////////////////////////////////////////////////////////////
229  {
[dc274ac]230    @Graph aux =
231    [[
[f7b99ac]232      Text _.name = name;
233      Real _.directed = directed
[dc274ac]234    ]]
[571deda]235  };
236
237  ////////////////////////////////////////////////////////////////////////////
[dc274ac]238  Real initialize(Set vertex.names)
[571deda]239  ////////////////////////////////////////////////////////////////////////////
240  {
[dc274ac]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");
[f7b99ac]250    Real _.edge::add.feature.numeric("Input");
[dc274ac]251    True
[571deda]252  };
253
254  ////////////////////////////////////////////////////////////////////////////
255  Text _.autodoc.member.edge.build = "";
[dc274ac]256  Real edge.build(Matrix data)
[571deda]257  ////////////////////////////////////////////////////////////////////////////
258  {
[dc274ac]259    Real _.edge.num := Rows(data);
[f7b99ac]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));
[dc274ac]265    Matrix idx = DifEq(1/(1-B),Rand(_.edge.num,1,1,1));
[f7b99ac]266    VMatrix triplet = Triplet(out|in|idx,_.vertex.num,_.vertex.num);
[dc274ac]267    VMatrix _.edge.map := Convert(triplet,"Cholmod.R.Sparse");
[f7b99ac]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.map.rep := Convert(triplet.rep,"Cholmod.R.Sparse");
271    Real _.edge.check.rep := VMatMax(_.edge.map.rep)>1;
272    If(_.edge.check.rep,
273    {
274      WriteLn("[GraphTools::@Graph "+_.name+"::edge.build()] "+
[1317b8b]275              "There are repeated edges","W")
[f7b99ac]276    });
[dc274ac]277    True
278  };
279
280  ////////////////////////////////////////////////////////////////////////////
[1317b8b]281  Text _.autodoc.member.get.vertex.names = "Returns the names of all "
282  "vertices";
283  Set get.vertex.names(Real void)
[dc274ac]284  ////////////////////////////////////////////////////////////////////////////
285  {
[1317b8b]286    ((_.vertex::_.feature)[1])::_.domain
[738ed66]287  };
288
289  ////////////////////////////////////////////////////////////////////////////
[1317b8b]290  Text _.autodoc.member.get.vertex.name = "Returns the name of one "
291  "vertex";
292  Text get.vertex.name(Real idx)
[738ed66]293  ////////////////////////////////////////////////////////////////////////////
294  {
[1317b8b]295    (((_.vertex::_.feature)[1])::_.domain)[idx]
[f7b99ac]296  };
297
298  ////////////////////////////////////////////////////////////////////////////
[1317b8b]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)
[f7b99ac]302  ////////////////////////////////////////////////////////////////////////////
303  {
[1317b8b]304    FindIndexByName(_.vertex, name )
[2b60aab]305  };
[dc274ac]306
[2b60aab]307  ////////////////////////////////////////////////////////////////////////////
[1317b8b]308  Text _.autodoc.member.get.vertex.features = "Returns all the features for "
309  "an vertex";
310  Set get.vertex.features(Real vertex.idx)
[2b60aab]311  ////////////////////////////////////////////////////////////////////////////
312  {
[1317b8b]313    _.vertex::get.all(vertex.idx)
[4d44f38]314  };
315
316  ////////////////////////////////////////////////////////////////////////////
[1317b8b]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
[4d44f38]323  ////////////////////////////////////////////////////////////////////////////
324  {
[1317b8b]325    _.edge::get(vertex.idx, feature.id)
[4d44f38]326  };
327
328  ////////////////////////////////////////////////////////////////////////////
[1317b8b]329  Text _.autodoc.member.get.edge.index = "Returns the index of an edge or 0 "
[4d44f38]330  "if it not exists";
[1317b8b]331  Real get.edge.index(Real out_, Real in_)
[4d44f38]332  ////////////////////////////////////////////////////////////////////////////
333  {
[1317b8b]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.map.rep, 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)
[4d44f38]356  };
357
358  ////////////////////////////////////////////////////////////////////////////
[1317b8b]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";
[4d44f38]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)
[2b60aab]367  }
[4d44f38]368
[571deda]369}
370
371]];
Note: See TracBrowser for help on using the repository browser.