~charon-developers/charon-flow/learningFlowBrock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cfloat>
#include "mtrand.h"

// needed CPP definitions
#ifndef GLOBAL_PLUGIN_DIR
/// set this to the global plugin path
#define GLOBAL_PLUGIN_DIR ""
#error GLOBAL_PLUGIN_DIR not defined!
#endif
#ifndef LOCAL_PLUGIN_DIR
/// set this to the plugin build path (local plugin path)
#define LOCAL_PLUGIN_DIR ""
#error LOCAL_PLUGIN_DIR not defined!
#endif
#ifndef TESTDIR
/// global testing directory
#define TESTDIR ""
#error TESTDIR not defined!
#endif

using namespace std;

#include "helpers.h"
#include <charon/MonteCarloNonlinearOptimizer.h>
#include <charon/TemplateEngine.h>
#include <charon/sdparam.h>

template<class RNG>
string randomString(RNG& rng, const size_t length) {
    string result;
    for (size_t i = 0; i < length; i++) {
        result += (char)('a' + (char)(rng.randInt(25)));
    }
    return result;
}

bool file_exists(const char *name) {
    ifstream file(name);
    return file;
}

template<class RNG>
string getTmpFile(RNG& rng, const char* prefix) {
    string name;
    size_t length = 10;
    do {
        name = string(prefix) + randomString(rng, length++);
    }
    while (file_exists(name.c_str()));
    fstream tmp;
    tmp.open(name.c_str(), fstream::out);
    tmp << 'a';
    tmp.close();
    return name;
}

class WorkflowExecute {
    public:

        typedef double T;

        WorkflowExecute(string _workflow, string _image_prefix, string _subdir) : workflow(_workflow), image_prefix(_image_prefix), subdir(_subdir) {
            execution_counter = 0;
            workflow_content = file_get_contents(_workflow);
            mkdir (_subdir.c_str(), 0777);
        }

        template<class Param>
            T operator()(Param& P) {
                execution_counter++;
                TemplateEngine teng;
                map<string, string> placeholders;
                P.get_placeholders(placeholders);
                placeholders["PREFIX"] = "../" + image_prefix;
                placeholders["RESULTIMAGE"] = "Result-" + itos(execution_counter) + ".cimg";
                teng.setTemplate(workflow_content);
                string tmp_workflow = teng.replace(placeholders);
                file_put_contents(subdir + "/Workflow-" + itos(execution_counter) + ".wrp", tmp_workflow);

                const string filename_log = subdir + "/log-" + itos(execution_counter);

                string command = "tuchulcha-run_d --non-interactive run " + subdir + "/Workflow-" + itos(execution_counter) + ".wrp >> " + filename_log;
                
                cout << "Logfile: " << filename_log << endl;
                system(command.c_str());


                const double mean_endpoint_error = get_mean_endpoint_error(filename_log);

                cout << "Mean endpoint error: " << mean_endpoint_error << endl;
                return mean_endpoint_error;
            }

    private:
        string workflow, image_prefix, subdir;
        int execution_counter;
        string workflow_content;

        double get_mean_endpoint_error(const string filename) {
            fstream in;
            in.open(filename.c_str(), fstream::in);
            while (in) {
                string line;
                getline(in, line);
                size_t pos = line.find("mean endpoint error");
                if (pos == string::npos) {
                    continue;
                }
                string result;
                for (size_t i = 0; i < line.length(); i++) {
                    char c = line[i];
                    if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '.') {
                        result += c;
                    }
                }
                stringstream tmp(result);
                double d_result = 0;
                tmp >> d_result;
                cout << "Line: " << line << endl << "Result: " << result << endl << "d_result: " << d_result;
                return d_result;
            }
            return DBL_MAX;
        }
};

template<class Param>
void run(string workflow, string image_prefix, string subdir) {

    typedef double T;
    typedef WorkflowExecute Function;

    Param current;

    current.addOrthogonal(0.8776312774, 0.0142700408);
    current.addOrthogonal(0.1223687226, 0.2901114552);

    current.addAligned(0.8931826418, 0.0043754741);
    current.addAligned(0.1068173582, 0.1565890242);

    Optimizer<Param, T> opt;
    opt.last_param = current;
    opt.enable_log();

    MTRand rng;
    Function F(workflow, image_prefix, subdir);

    while(true) {
        opt.step(rng, F, subdir);
        if (opt.cooldowncounter > 2) {
            return;
        }
    }
}

int main(int argc, char ** argv) {

    if (argc < 3) {
        cout << "Usage: " << argv[0] << " <workflow> <image-prefix>" << endl;
        return 0;
    }
    string workflow = argv[1];
    string prefix = argv[2];

    setpriority(PRIO_PROCESS, 0, 12);

    string pathname = get_current_dir_name();
    
    while(true) {

        stringstream subdir;
        subdir << workflow << "-" << prefix << "-";
        {
            time_t now = time(NULL);
            struct tm * tim = localtime(&now);
            subdir << tim->tm_year+1900 << "-" << (tim->tm_mon < 9 ? "0" : "") << tim->tm_mon+1 << "-" << (tim->tm_mday < 10 ? "0":"") << tim->tm_mday << "-" << (tim->tm_hour < 10 ? "0":"") << tim->tm_hour << "-" << (tim->tm_min < 10 ? "0":"") << tim->tm_min << "-" << (tim->tm_sec < 10 ? "0":"") << tim->tm_sec;
        }
        cout << subdir.str() << endl;

        cout << pathname << endl;

        run<SDParam>(string(argv[1]), string(argv[2]), subdir.str());
    }

    return 0;
}