-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexhaust.cpp
133 lines (117 loc) · 3.88 KB
/
exhaust.cpp
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
#include<cmath>
#include<exhaust.hpp>
#include<lua_cmds.hpp>
#include<eap_resources.hpp>
exhaust::exhaust(std::string lua_file) : algorithm(lua_file)
{
}
void exhaust::setup_algo_params()
{
try
{
algorithm::setup_algo_params();
m_nec_input = boost::format(eap::run_directory + "ind%09d");
}
catch (...)
{
throw;
}
}
void exhaust::run(unsigned int run_id)
{
try
{
std::vector<position_ptr> placements;
std::cout<<"***creating individuals\n";
recur_placements(placements, 0);
std::cout<<"pop size"<<m_pop.size()<<"\n";
evaluate();
std::sort(m_pop.begin(), m_pop.end(), eap::gain_fitness_sort);
m_max_gain = m_pop.back()->m_gain_fitness;
std::sort(m_pop.begin(), m_pop.end(), eap::coupling_fitness_sort);
m_min_coup = m_pop.front()->m_coupling_fitness;
m_max_coup = m_pop.back()->m_coupling_fitness + std::abs(m_min_coup);
for (individual_ptr i_ind : m_pop)
{
i_ind->m_coupling_fitness += std::abs(m_min_coup);
if (i_ind->m_coupling_fitness < 0)
throw eap::InvalidStateException("Invalid coupling calculation\n");
i_ind->m_coupling_fitness /= m_max_coup;
i_ind->m_gain_fitness /= m_max_gain;
i_ind->m_fitness = cal_fitness(i_ind);
}
std::sort(m_pop.begin(), m_pop.end(), eap::fitness_sort);
std::cout<<"best "<<m_pop[0]->m_fitness<<"\n";
save_norm(eap::run_directory);
save_population(m_pop, run_id, 0);
save_best_nec(m_pop[0], run_id, 0);
}
catch (...)
{
throw;
}
}
void exhaust::evaluate()
{
try
{
if (m_run_simulator)
run_simulation(0); //argument doesn't signify anything here
boost::format nec_output(eap::run_directory + "ind%09da%02d.out");
for (unsigned int i=0; i<m_pop.size(); ++i)
{
for (unsigned int j=0; j<m_ant_configs.size(); ++j)
{
evaluation_ptr p_eval(new evaluation);
m_pop[i]->m_evals.push_back(p_eval);
unsigned int read = read_radiation(str(nec_output % i % j), p_eval);
if (read != (num_polar() * m_step_freq))
throw eap::InvalidStateException("Problem with output in " + str(nec_output % i % j));
m_pop[i]->m_one_ant_on_fitness.push_back(compare(m_free_inds[j]->m_evals[0], m_pop[i]->m_evals[j]));
m_pop[i]->m_gain_fitness += m_pop[i]->m_one_ant_on_fitness[j];
}
m_pop[i]->m_coupling_fitness = read_coupling(str(nec_output % i % m_ant_configs.size()), m_ant_configs.size());
if (m_pop[i]->m_coupling_fitness == 0.0f)
throw eap::InvalidStateException("coupling bad\n");
m_pop[i]->m_fitness = cal_fitness(m_pop[i]);
}
}
catch (...)
{
throw;
}
}
void exhaust::run_simulation(unsigned int id)
{
try
{
std::cout<<"***running simulation for exhaustive\n";
std::string cmd("find " + eap::run_directory + " -iname \"*.nec\" | parallel -j+0 nec2++ -i {}");
system(cmd.c_str());
std::cout<<"***completed simulation for exhaustive\n";
}
catch (...)
{
throw;
}
}
void exhaust::recur_placements(std::vector<position_ptr> &placements, unsigned int i)
{
if(m_ant_configs.size() == placements.size())
{
m_pop.push_back(create_individual(str(m_nec_input % m_pop.size())+"a%02d.nec", placements));
return;
}
for (unsigned int j=0; j<m_ant_configs[i]->m_positions.size(); j++)
{
if(!overlap(placements, m_ant_configs[i]->m_positions[j]))
{
placements.push_back(m_ant_configs[i]->m_positions[j]);
recur_placements(placements, i+1);
placements.pop_back();
}
}
}
exhaust::~exhaust(void)
{
}