source: cpp/frams/_demos/simil_test.cpp @ 870

Last change on this file since 870 was 732, checked in by Maciej Komosinski, 7 years ago

Added support for "checkpoints" (intermediate phases of development of the Model when converting between genetic encodings). See Model.checkpoint() and conv_f1.cpp for an example.

File size: 5.7 KB
RevLine 
[349]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[605]2// Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
[349]3// See LICENSE.txt for details.
4
5
6#include <vector>
[391]7#include "common/loggers/loggertostdout.h"
[349]8#include "frams/_demos/genotypeloader.h"
9#include "frams/genetics/preconfigured.h"
[382]10#include "common/virtfile/stdiofile.h"
[349]11#include "frams/model/similarity/simil_model.h"
12
13
14
15using namespace std;
16
17/** Computes a matrix of distances between all genotypes in the specified
[606]18        .gen file, using the matching and measure weights as specified in the
19        command line. */
[349]20int main(int argc, char *argv[])
21{
[606]22        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
23        typedef double *pDouble;
24        int iCurrParam = 0; // index of the currently processed parameter
25        char *szCurrParam = NULL;
26        ModelSimil M; // similarity computing object
27        bool bPrintNames = false; // specifies if names of genotypes are to be printed
28        int nResult = 0; // a temporary result
[349]29
[606]30        if (argc < 7)
31        {
32                printf("Too few parameters!\n");
33                printf("Command line: [-names] <genotypesFile> <w_dP> <w_dDEG> <w_dNEU> <w_dGEO> <fixZaxis?>\n\n");
[349]34
[606]35                printf("Parameters:\n");
36                printf("  <genotypesFile> name of a file with genotypes\n");
37                printf("  <w_dP> weight of the difference in the number of parts\n");
38                printf("  <w_dDEG> weight of the difference in degrees of matched parts\n");
39                printf("  <w_dNEU> weight of the difference in neurons of matched parts\n");
40                printf("  <w_dGEO> weight of the distance of matched parts\n");
41                printf("  <fixZaxis?> should the 'z' (vertical) coordinate be fixed during the alignment? (0 or 1)\n\n");
[349]42
[606]43                printf("Switches:\n");
44                printf("  -names specifies that the number and names of genotypes are to be printed to output\n");
45                printf("  before the distance matrix; by default the number and names are not printed\n\n");
[349]46
[606]47                printf("Outputs a symmetric distance matrix in the format:\n");
48                printf("  <row_1> (columns in a row are separated by TABs)\n");
49                printf("  ...\n");
50                printf("  <row_n>\n");
[349]51
[606]52                return -1;
53        }
[349]54
[606]55        // prepare output parameters from .gen file
56        vector<Geno *> pvGenos;
57        vector<char *> pvNames;
[349]58
[606]59        // check if there is a switch
60        iCurrParam = 1;
61        szCurrParam = argv[iCurrParam];
62        if (strcmp(szCurrParam, "-names") == 0)
63        {
64                // switch "-names" was given; print names also
65                bPrintNames = true;
66                // pass to the next parameter
67                iCurrParam++;
68        }
[349]69
[606]70        // check the parameters
71        // get <genotypesFile> name from command line
72        char *szFileName = argv[iCurrParam];
[349]73
[606]74        // initially set measure components' weights to invalid values (negative)
75        for (int i = 0; i < M.GetNOFactors(); i++)
76        {
77                M.m_adFactors[i] = -1.0;
78        }
[349]79
[606]80        const char *params[] = { "<w_dP>", "<w_dDEG>", "<w_dNEU>", "<w_dGEO>" };
81        for (int i = 0; i < M.GetNOFactors(); i++)
82        {
83                iCurrParam++;
84                szCurrParam = argv[iCurrParam];
85                nResult = sscanf(szCurrParam, " %lf ", &M.m_adFactors[i]);
86                if (nResult != 1)
87                {
88                        // <w_dX> is not a number -- error
89                        printf("%s", params[i]);
90                        printf(" should be a number\n");
91                        return -1;
92                }
93                else
94                {
95                        // <w_dX> is a number; check if nonnegative
96                        if (M.m_adFactors[i] < 0.0)
97                        {
98                                printf("%s", params[i]);
99                                printf(" should be a nonnegative number\n");
100                                return -1;
101                        }
102                }
103        }
[349]104
[606]105        iCurrParam++;
106        szCurrParam = argv[iCurrParam];
[612]107        nResult = sscanf(szCurrParam, " %d", &M.fixedZaxis);
[606]108        if (nResult != 1)
109        {
110                // <isZFixed> is not a number -- error
111                printf("<isZFixed> should be a number\n");
112                return -1;
113        }
[612]114        else if (M.fixedZaxis != 0 && M.fixedZaxis != 1)
[606]115        {
[612]116                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.fixedZaxis);
[606]117                return -1;
118        }
[349]119
[606]120        // read the input file
121        // prepare loading of genotypes from a .gen file
122        // create some basic genotype converters
123        PreconfiguredGenetics genetics;
124        StdioFileSystem_autoselect stdiofilesys;
125
126        long count = 0, totalsize = 0;
[732]127        GenotypeMiniLoader loader(szFileName);
128        GenotypeMini *loaded;
[606]129        while (loaded = loader.loadNextGenotype())
130        {
131                // while a valid genotype was loaded
132                count++;
133                totalsize += loaded->genotype.len();
134                // create a Geno object based on the MiniGenotype
135                Geno *pNextGenotype = new Geno(loaded->genotype);
136                if ((pNextGenotype != NULL) && (pNextGenotype->isValid()))
137                {
138                        pvGenos.push_back(pNextGenotype);
139                        char *szNewName = new char[loaded->name.len() + 1];
140                        strcpy(szNewName, loaded->name.c_str());
141                        pvNames.push_back(szNewName);
142                }
143                else
144                {
145                        printf("Genotype %2li is not valid\n", count);
146                        if (pNextGenotype != NULL) delete pNextGenotype;
147                }
148        }
[732]149        if (loader.getStatus() == GenotypeMiniLoader::OnError)
[606]150        {
151                printf("Error: %s", loader.getError().c_str());
152        }
153
154        double dSimilarity = 0.0;
155        double **aaSimil = NULL; // array of similarities
156
157        // create the empty array of similarities
158        aaSimil = new pDouble[pvGenos.size()];
159        for (unsigned int k = 0; k < pvGenos.size(); k++)
160        {
161                aaSimil[k] = new double[pvGenos.size()];
[455]162                for (unsigned int l = 0; l < pvGenos.size(); l++)
[606]163                        aaSimil[k][l] = 0.0;
164        }
[349]165
[606]166        // compute and remember similarities
[455]167        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]168        {
[455]169                for (unsigned int j = 0; j < pvGenos.size(); j++)
[606]170                {
171                        dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
172                        aaSimil[i][j] = dSimilarity;
173                }
174        }
[349]175
[606]176        if (bPrintNames)
177        {
178                // if "-names" switch was given, print the number of genotypes and their names
179                printf("%li\n", pvGenos.size());
[455]180                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
[606]181                {
182                        printf("%s\n", pvNames.at(iGen));
183                }
184        }
[349]185
[606]186        // print out the matrix of similarities
[455]187        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]188        {
[455]189                for (unsigned int j = 0; j < pvGenos.size(); j++)
[606]190                {
191                        printf("%.2lf\t", aaSimil[i][j]);
192                }
193                printf("\n");
194        }
[349]195
[606]196        // delete vectors and arrays
[455]197        for (unsigned int i = 0; i < pvGenos.size(); i++)
[606]198        {
199                delete pvGenos.operator[](i);
200                delete[] pvNames.operator[](i);
201                delete[] aaSimil[i];
202        }
[349]203
[606]204        delete[] aaSimil;
[349]205
[606]206        return 0;
[372]207}
Note: See TracBrowser for help on using the repository browser.