private.cpp 6.91 KB
Newer Older
Gauthier Quesnel's avatar
2019    
Gauthier Quesnel committed
1
/* Copyright (C) 2018-2019 INRA
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
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "private.hpp"
#include "utils.hpp"

namespace baryonyx {

Gauthier Quesnel's avatar
Gauthier Quesnel committed
28
const fmt::text_style context::message_style[] = {
29
30
31
32
33
34
35
36
    (fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), // emerg
    (fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), // alert
    (fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red)), // crit
    (fmt::fg(fmt::terminal_color::red)),                       // err
    (fmt::fg(fmt::terminal_color::yellow)),                    // warning
    (fmt::fg(fmt::terminal_color::white)),                     // notice
    (fmt::fg(fmt::terminal_color::white)),                     // info
    (fmt::fg(fmt::terminal_color::blue))                       // debug
Gauthier Quesnel's avatar
Gauthier Quesnel committed
37
38
};

39
40
void
context_set_parameters(const context_ptr& ctx,
41
                       const std::string& name,
42
43
44
                       std::string value)
{
    if (name == "method")
45
        ctx->method = std::move(value);
46
    else
47
        warning(*ctx, "context: unknown variable {}.\n", name);
48
49
50
51
52
53
}

void
context_set_solver_parameters(const context_ptr& ctx,
                              const solver_parameters& params)
{
54
55
56
    ctx->parameters.time_limit = params.time_limit <= 0
                                   ? std::numeric_limits<double>::infinity()
                                   : params.time_limit;
57

58
    if (params.theta >= 0 && params.theta <= 1)
59
60
        ctx->parameters.theta = params.theta;

61
    if (params.delta == -1 || params.delta >= 0)
62
63
        ctx->parameters.delta = params.delta;

64
65
66
    if (params.kappa_min < params.kappa_max && params.kappa_min >= 0 &&
        params.kappa_min < 1 && params.kappa_max <= 1 &&
        params.kappa_step >= 0 && params.kappa_step < 1) {
67
68
69
70
71
72
73
74
        ctx->parameters.kappa_min = params.kappa_min;
        ctx->parameters.kappa_step = params.kappa_step;
        ctx->parameters.kappa_max = params.kappa_max;
    }

    if (params.alpha >= 0)
        ctx->parameters.alpha = params.alpha;

75
    if (std::isfinite(params.pushing_k_factor))
76
77
        ctx->parameters.pushing_k_factor = params.pushing_k_factor;

78
    if (std::isfinite(params.pushing_objective_amplifier))
79
80
81
        ctx->parameters.pushing_objective_amplifier =
          params.pushing_objective_amplifier;

82
83
    if (std::isfinite(params.init_crossover_bastert_insertion))
        ctx->parameters.init_crossover_bastert_insertion =
84
          std::clamp(params.init_crossover_bastert_insertion, 0.0, 1.0);
85
86
87

    if (std::isfinite(params.init_crossover_solution_selection_mean))
        ctx->parameters.init_crossover_solution_selection_mean =
88
          std::clamp(params.init_crossover_solution_selection_mean, 0.0, 1.0);
89
90

    if (std::isfinite(params.init_crossover_solution_selection_stddev))
91
92
        ctx->parameters.init_crossover_solution_selection_stddev = std::clamp(
          params.init_crossover_solution_selection_stddev, 0.0, 1.0);
93
94
95

    if (std::isfinite(params.init_mutation_variable_mean))
        ctx->parameters.init_mutation_variable_mean =
96
          std::clamp(params.init_mutation_variable_mean, 0.0, 1.0);
97
98
99

    if (std::isfinite(params.init_mutation_variable_stddev))
        ctx->parameters.init_mutation_variable_stddev =
100
          std::clamp(params.init_mutation_variable_stddev, 0.0, 1.0);
101
102
103

    if (std::isfinite(params.init_mutation_value_mean))
        ctx->parameters.init_mutation_value_mean =
104
          std::clamp(params.init_mutation_value_mean, 0.0, 1.0);
105
106
107

    if (std::isfinite(params.init_mutation_value_stddev))
        ctx->parameters.init_mutation_value_stddev =
108
          std::clamp(params.init_mutation_value_stddev, 0.0, 1.0);
109

110
111
112
    if (params.init_policy_random >= 0 && params.init_policy_random <= 1)
        ctx->parameters.init_policy_random = params.init_policy_random;

113
    ctx->parameters.seed = params.seed;
114
    ctx->parameters.thread = params.thread;
115

116
117
118
    ctx->parameters.limit = (params.limit <= 0)
                              ? std::numeric_limits<long int>::max()
                              : params.limit;
119
120
121
122

    if (params.print_level >= 0)
        ctx->parameters.print_level = params.print_level;

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    // Convert the value [0..1] into a percentage of loop or use directly the
    // number if the value.

    if (params.w < 0) {
        ctx->parameters.w = 0;
    } else {
        if (params.limit > 0) {
            auto limit = static_cast<double>(params.limit);

            if (params.w < 1)
                ctx->parameters.w = limit * params.w;
            else
                ctx->parameters.w = std::min(limit, params.w);
        } else {
            ctx->parameters.w = std::floor(params.w);
        }
    }
140

141
142
    ctx->parameters.pushes_limit =
      params.pushes_limit <= 0 ? 0 : params.pushes_limit;
143

144
145
    ctx->parameters.pushing_iteration_limit =
      params.pushing_iteration_limit <= 0 ? 0 : params.pushing_iteration_limit;
146

147
148
149
    if (params.init_population_size >= 5)
        ctx->parameters.init_population_size = params.init_population_size;

150
151
152
153
154
155
156
157
158
159
160
    ctx->parameters.init_kappa_improve_start =
      std::clamp(params.init_kappa_improve_start, 0.0, 1.0);

    ctx->parameters.init_kappa_improve_increase =
      std::clamp(params.init_kappa_improve_increase, 0.0, 1.0);

    ctx->parameters.init_kappa_improve_stop =
      std::clamp(params.init_kappa_improve_stop,
                 ctx->parameters.init_kappa_improve_start,
                 1.0);

161
162
163
164
165
    ctx->parameters.pre_order = params.pre_order;
    ctx->parameters.order = params.order;
    ctx->parameters.float_type = params.float_type;
    ctx->parameters.init_policy = params.init_policy;
    ctx->parameters.cost_norm = params.cost_norm;
166
    ctx->parameters.mode = params.mode;
167
    ctx->parameters.preprocessor = params.preprocessor;
168
    ctx->parameters.observer = params.observer;
169
    ctx->parameters.storage = params.storage;
170
    ctx->parameters.solver = params.solver;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
171
    ctx->parameters.debug = params.debug;
172
173
174
175
176
177
178
179
180
}

solver_parameters
context_get_solver_parameters(const context_ptr& ctx)
{
    return ctx->parameters;
}

} // namespace baryonyx