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

Last change on this file since 145 was 145, checked in by sz, 6 years ago

Genetics reorganization (affects ALL applications!):

  • Converters/Validators? are now configured/initialized in a more verbose but also less confusing way
  • At the same time, the PreconfiguredGenetics? object will help you avoid the increased complexity by creating the ready-to-use environment that is sufficient in 99% of cases (see the demos)
  • Format F genetics updated (work in progress)
  • 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/virtfile/stdiofile.h>
6#include <frams/util/sstringutils.h>
7#include <frams/genetics/preconfigured.h>
8#include <frams/model/model.h>
9#include <frams/errmgr/stdouterr.h>
10#include <frams/canvas/nn_layout_model.h>
11
12#include <algorithm>
13
14/**
15 @file
16 Sample code: Neuron layout tester
17
18 Hint: Use loader_test to extract genotypes from framsticks *.gen files:
19 loader_test "data/walking.gen" "Walking Lizard" | neuro_layout_test -
20*/
21
22// stl is fun? ;-) ForwardIterator implementation for element coordinates (required by min_element/max_element)
23template <int MEMBER> struct NNIter //MEMBER: 0..3=x/y/w/h
24{
25NNLayoutState *nn; int index;
26NNIter() {}
27NNIter(NNLayoutState *_nn, int _index):nn(_nn),index(_index) {}
28int operator*() {return nn->GetXYWH(index)[MEMBER];}
29NNIter& operator++() {index++; return *this;}
30bool operator!=(const NNIter& it) {return index!=it.index;}
31bool operator==(const NNIter& it) {return index==it.index;}
32
33static NNIter begin(NNLayoutState *_nn) {return NNIter(_nn,0);}
34static NNIter end(NNLayoutState *_nn) {return NNIter(_nn,_nn->GetElements());}
35};
36
37class Screen
38{
39int min_x,max_x,min_y,max_y,scale_x,scale_y;
40int rows,columns;
41char* screen;
42
43public:
44
45Screen(int _min_x,int _max_x,int _min_y,int _max_y,int _scale_x,int _scale_y)
46        :min_x(_min_x),max_x(_max_x),min_y(_min_y),max_y(_max_y),scale_x(_scale_x),scale_y(_scale_y)
47                {
48                columns=(max_x-min_x+scale_x-1)/scale_x;
49                rows=(max_y-min_y+scale_y-1)/scale_y;
50                screen=new char[rows*columns];
51                memset(screen,' ',rows*columns);
52                }
53
54~Screen()
55                {
56                delete[] screen;
57                }
58
59void put(int x,int y,const char *str)
60        {
61        x=(x-min_x)/scale_x;
62        y=(y-min_y)/scale_y;
63        if (x<0) return;
64        if (y<0) return;
65        if (y>=rows) return;
66        for(;*str;str++,x++)
67                {
68                if (x>=columns) return;
69                screen[columns*y+x]=*str;
70                }
71        }
72
73void print()
74        {
75        for(int y=0;y<rows;y++)
76                {
77                fwrite(&screen[columns*y],1,columns,stdout);
78                printf("\n");
79                }
80        }
81};
82
83int main(int argc,char*argv[])
84{
85StdoutErrorHandler err;//the default ErrorHandler constructor automatically registers this object to receive framsg messages (and in this case, redirect them to standard output)
86PreconfiguredGenetics genetics;
87
88if (argc<=1)
89        {
90                puts("Parameters:\n"
91                     " 1. Genotype (or - character indicating the genotype will be read from stdin)\n"
92                     " 2. (Optional) layout type (the only useful layout is 2, which is the default, see nn_simple_layout.cpp");
93          return 10;
94        }
95SString gen(argv[1]);
96if (!strcmp(gen,"-"))
97        {
98        gen=0;
99        StdioFILEDontClose in(stdin);
100        loadSString(&in,gen);
101        }
102int layout_type=2;
103if (argc>2) layout_type=atol(argv[2]);
104Geno g(gen);
105if (!g.isValid()) {puts("invalid genotype");return 5;}
106Model m(g);
107if (!m.getNeuroCount()) {puts("no neural network");return 1;}
108printf("%d neurons,",m.getNeuroCount());
109
110NNLayoutState_Model nn_layout(&m);
111struct NNLayoutFunction &nnfun=nn_layout_functions[layout_type];
112printf(" using layout type=%d (%s)\n",layout_type,nnfun.name);
113nnfun.doLayout(&nn_layout);
114
115for(int i=0;i<nn_layout.GetElements();i++)
116        {
117        int *xywh=nn_layout.GetXYWH(i);
118        printf("#%-3d %s\t%d,%d\t%dx%d\n",i,(const char*)m.getNeuro(i)->getClassName(),
119               xywh[0],xywh[1],xywh[2],xywh[3]);
120        }
121
122Screen screen(*std::min_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))-30,
123              *std::max_element(NNIter<0>::begin(&nn_layout),NNIter<0>::end(&nn_layout))+70,
124              *std::min_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout)),
125              *std::max_element(NNIter<1>::begin(&nn_layout),NNIter<1>::end(&nn_layout))+30,
126              10,35);
127
128printf("===========================================\n");
129for(int i=0;i<nn_layout.GetElements();i++)
130        {
131        int *xywh=nn_layout.GetXYWH(i);
132        SString label=SString::sprintf("%d:%s",i,(const char*)m.getNeuro(i)->getClassName());
133        screen.put(xywh[0],xywh[1],(const char*)label);
134        }
135screen.print();
136printf("===========================================\n");
137
138}
Note: See TracBrowser for help on using the repository browser.