source: cpp/frams/_demos/neuro_layout_test.cpp @ 144

Last change on this file since 144 was 135, checked in by sz, 10 years ago

neuron layout algorithm (for schematic drawing), usage example in frams/_demos/neuro_layout_test.cpp

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include <frams/genetics/geno.h>
6#include <frams/virtfile/stdiofile.h>
7#include <frams/util/sstringutils.h>
8#include <frams/genetics/defgenoconv.h>
9#include <frams/model/model.h>
10#include <frams/errmgr/stdouterr.h>
11#include <frams/canvas/nn_layout_model.h>
12
13#include <algorithm>
14
15/**
16 @file
17 Sample code: Neuron layout tester
18
19 Hint: Use loader_test to extract genotypes from framsticks *.gen files:
20 loader_test "data/walking.gen" "Walking Lizard" | neuro_layout_test -
21*/
22
23StdoutErrorHandler err; //redirect model-related errors to stdout
24DefaultGenoConvManager gcm; //without this object the application would only handle "format 0" genotypes
25
26// stl is fun? ;-) ForwardIterator implementation for element coordinates (required by min_element/max_element)
27template <int MEMBER> struct NNIter //MEMBER: 0..3=x/y/w/h
28{
29NNLayoutState *nn; int index;
30NNIter() {}
31NNIter(NNLayoutState *_nn, int _index):nn(_nn),index(_index) {}
32int operator*() {return nn->GetXYWH(index)[MEMBER];}
33NNIter& operator++() {index++; return *this;}
34bool operator!=(const NNIter& it) {return index!=it.index;}
35bool operator==(const NNIter& it) {return index==it.index;}
36
37static NNIter begin(NNLayoutState *_nn) {return NNIter(_nn,0);}
38static NNIter end(NNLayoutState *_nn) {return NNIter(_nn,_nn->GetElements());}
39};
40
41class Screen
42{
43int min_x,max_x,min_y,max_y,scale_x,scale_y;
44int rows,columns;
45char* screen;
46
47public:
48
49Screen(int _min_x,int _max_x,int _min_y,int _max_y,int _scale_x,int _scale_y)
50        :min_x(_min_x),max_x(_max_x),min_y(_min_y),max_y(_max_y),scale_x(_scale_x),scale_y(_scale_y)
51                {
52                columns=(max_x-min_x+scale_x-1)/scale_x;
53                rows=(max_y-min_y+scale_y-1)/scale_y;
54                screen=new char[rows*columns];
55                memset(screen,' ',rows*columns);
56                }
57
58~Screen()
59                {
60                delete[] screen;
61                }
62
63void put(int x,int y,const char *str)
64        {
65        x=(x-min_x)/scale_x;
66        y=(y-min_y)/scale_y;
67        if (x<0) return;
68        if (y<0) return;
69        if (y>=rows) return;
70        for(;*str;str++,x++)
71                {
72                if (x>=columns) return;
73                screen[columns*y+x]=*str;
74                }
75        }
76
77void print()
78        {
79        for(int y=0;y<rows;y++)
80                {
81                fwrite(&screen[columns*y],1,columns,stdout);
82                printf("\n");
83                }
84        }
85};
86
87int main(int argc,char*argv[])
88{
89if (argc<=1)
90        {
91                puts("Parameters:\n"
92                     " 1. Genotype (or - character indicating the genotype will be read from stdin)\n"
93                     " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp");
94          return 10;
95        }
96SString gen(argv[1]);
97if (!strcmp(gen,"-"))
98        {
99        gen=0;
100        StdioFILEDontClose in(stdin);
101        loadSString(&in,gen);
102        }
103int layout_type=2;
104if (argc>2) layout_type=atol(argv[2]);
105Geno g(gen);
106if (!g.isValid()) {puts("invalid genotype");return 5;}
107Model m(g);
108if (!m.getNeuroCount()) {puts("no neural network");return 1;}
109printf("%d neurons,",m.getNeuroCount());
110
111NNLayoutState_Model nn_layout(&m);
112struct NNLayoutFunction &nnfun=nn_layout_functions[layout_type];
113printf(" using layout type=%d (%s)\n",layout_type,nnfun.name);
114nnfun.doLayout(&nn_layout);
115
116for(int i=0;i<nn_layout.GetElements();i++)
117        {
118        int *xywh=nn_layout.GetXYWH(i);
119        printf("#%-3d %s\t%d,%d\t%dx%d\n",i,(const char*)m.getNeuro(i)->getClassName(),
120               xywh[0],xywh[1],xywh[2],xywh[3]);
121        }
122
123Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))-30,
124              *std::max_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))+70,
125              *std::min_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout)),
126              *std::max_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout))+30,
127              10,35);
128
129printf("===========================================\n");
130for(int i=0;i<nn_layout.GetElements();i++)
131        {
132        int *xywh=nn_layout.GetXYWH(i);
133        SString label=SString::sprintf("%d:%s",i,(const char*)m.getNeuro(i)->getClassName());
134        screen.put(xywh[0],xywh[1],(const char*)label);
135        }
136screen.print();
137printf("===========================================\n");
138
139}
Note: See TracBrowser for help on using the repository browser.