tasks_generator 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +10 -0
- data/ext/tasks_generator/bindings.cc +200 -0
- data/ext/tasks_generator/config.h +43 -0
- data/ext/tasks_generator/extconf.rb +5 -0
- data/ext/tasks_generator/function.cc +141 -0
- data/ext/tasks_generator/function.h +22 -0
- data/ext/tasks_generator/generator.cc +23 -0
- data/ext/tasks_generator/generator.h +42 -0
- data/ext/tasks_generator/question.h +52 -0
- data/ext/tasks_generator/theme.h +42 -0
- data/ext/tasks_generator/types.h +18 -0
- data/lib/tasks_generator.rb +7 -0
- data/lib/tasks_generator/version.rb +3 -0
- data/tasks_generator.gemspec +29 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2e91eff44e7a87a65e00fd652886107421d5fc84
|
4
|
+
data.tar.gz: 4b720339ea6cadbc50fd3b729821f86de4f90838
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37d7779c7e40e8dda51dd55703f449e3b590b3c0f632611f6e4136a31d359cac8944fbcf9ecf878936e5235af4616b4bdd4d629105280b313c18e38db2073248
|
7
|
+
data.tar.gz: 8755e7db35aac4d84e648fb77c10ea705b1cec784f4ef271cfd1de68f8274c0665029ae51c10363690fc8a8cce41be58201cfc547a7b7125672edde8c32536f2
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Arslan Urtashev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# TasksGenerator
|
2
|
+
|
3
|
+
Test tasks generator for AI lab MEPhI
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'tasks_generator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install tasks_generator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### TasksGenerator::Config
|
22
|
+
Config for configure Generator
|
23
|
+
* life_time - iterations count for genetic algorithm (default: 1000)
|
24
|
+
* mutation_chance - mutation chance (default: 0.05)
|
25
|
+
* population_size - starting population (default: 1000)
|
26
|
+
* tasks - tasks count
|
27
|
+
* themes - array of Theme
|
28
|
+
|
29
|
+
### TasksGenerator::Theme
|
30
|
+
Theme class for config.themes
|
31
|
+
* theme_id
|
32
|
+
* difficulty_min
|
33
|
+
* difficulty_max
|
34
|
+
|
35
|
+
### TasksGenerator::Question
|
36
|
+
Question class for questions in Generator constructor
|
37
|
+
* question_id
|
38
|
+
* theme_id
|
39
|
+
* difficulty
|
40
|
+
|
41
|
+
### Tasks::Generator::Generator
|
42
|
+
Test tasks generator
|
43
|
+
* generate() - this method returns array of questions
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it ( https://github.com/Abitella/tasks_generator/fork )
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
#include "rice/Array.hpp"
|
2
|
+
#include "rice/Constructor.hpp"
|
3
|
+
#include "rice/Object.hpp"
|
4
|
+
|
5
|
+
#include "config.h"
|
6
|
+
#include "generator.h"
|
7
|
+
#include "question.h"
|
8
|
+
#include "theme.h"
|
9
|
+
|
10
|
+
using namespace tasks_generator;
|
11
|
+
|
12
|
+
template<>
|
13
|
+
question_t from_ruby<question_t>(Rice::Object obj) {
|
14
|
+
size_t qid = from_ruby<size_t>(obj.call("question_id"));
|
15
|
+
size_t tid = from_ruby<size_t>(obj.call("theme_id"));
|
16
|
+
size_t d = from_ruby<size_t>(obj.call("difficulty"));
|
17
|
+
|
18
|
+
return question_t(qid, tid, d);
|
19
|
+
}
|
20
|
+
|
21
|
+
template<>
|
22
|
+
Rice::Object to_ruby(question_t const &q) {
|
23
|
+
return Rice::Data_Object<question_t>(new question_t(q));
|
24
|
+
}
|
25
|
+
|
26
|
+
template<>
|
27
|
+
std::vector<question_t> from_ruby<std::vector<question_t>>(Rice::Object obj) {
|
28
|
+
Rice::Array arr(obj);
|
29
|
+
|
30
|
+
std::vector<question_t> res;
|
31
|
+
res.reserve(arr.size());
|
32
|
+
|
33
|
+
for (Rice::Object o : arr)
|
34
|
+
res.push_back(from_ruby<question_t>(o));
|
35
|
+
|
36
|
+
return res;
|
37
|
+
}
|
38
|
+
|
39
|
+
template<>
|
40
|
+
Rice::Object to_ruby(std::vector<question_t> const &questions) {
|
41
|
+
Rice::Array arr;
|
42
|
+
for (question_t const &q : questions)
|
43
|
+
arr.push(to_ruby(q));
|
44
|
+
return arr;
|
45
|
+
}
|
46
|
+
|
47
|
+
template<>
|
48
|
+
theme_t from_ruby<theme_t>(Rice::Object obj) {
|
49
|
+
size_t id = from_ruby<size_t>(obj.call("theme_id"));
|
50
|
+
size_t dmin = from_ruby<size_t>(obj.call("difficulty_min"));
|
51
|
+
size_t dmax = from_ruby<size_t>(obj.call("difficulty_max"));
|
52
|
+
|
53
|
+
return theme_t(id, dmin, dmax);
|
54
|
+
}
|
55
|
+
|
56
|
+
template<>
|
57
|
+
Rice::Object to_ruby<theme_t>(theme_t const &th) {
|
58
|
+
return Rice::Data_Object<theme_t>(new theme_t(th));
|
59
|
+
}
|
60
|
+
|
61
|
+
template<>
|
62
|
+
std::vector<theme_t> from_ruby<std::vector<theme_t>>(Rice::Object obj) {
|
63
|
+
Rice::Array arr(obj);
|
64
|
+
|
65
|
+
std::vector<theme_t> themes;
|
66
|
+
themes.reserve(arr.size());
|
67
|
+
|
68
|
+
for (Rice::Object obj : arr)
|
69
|
+
themes.push_back(from_ruby<theme_t>(obj));
|
70
|
+
|
71
|
+
return themes;
|
72
|
+
}
|
73
|
+
|
74
|
+
void set_life_time(Rice::Object obj, size_t life_time) {
|
75
|
+
Rice::Data_Object<config_t>(obj)->life_time = life_time;
|
76
|
+
}
|
77
|
+
|
78
|
+
size_t get_life_time(Rice::Object obj) {
|
79
|
+
return Rice::Data_Object<config_t>(obj)->life_time;
|
80
|
+
}
|
81
|
+
|
82
|
+
void set_mutation_chance(Rice::Object obj, double mutation_chance) {
|
83
|
+
Rice::Data_Object<config_t>(obj)->mutation_chance = mutation_chance;
|
84
|
+
}
|
85
|
+
|
86
|
+
double get_mutation_chance(Rice::Object obj) {
|
87
|
+
return Rice::Data_Object<config_t>(obj)->mutation_chance;
|
88
|
+
}
|
89
|
+
|
90
|
+
void set_population_size(Rice::Object obj, size_t population_size) {
|
91
|
+
Rice::Data_Object<config_t>(obj)->population_size = population_size;
|
92
|
+
}
|
93
|
+
|
94
|
+
size_t get_population_size(Rice::Object obj) {
|
95
|
+
return Rice::Data_Object<config_t>(obj)->population_size;
|
96
|
+
}
|
97
|
+
|
98
|
+
void set_tasks(Rice::Object obj, size_t tasks) {
|
99
|
+
Rice::Data_Object<config_t>(obj)->tasks = tasks;
|
100
|
+
}
|
101
|
+
|
102
|
+
size_t get_tasks(Rice::Object obj) {
|
103
|
+
return Rice::Data_Object<config_t>(obj)->tasks;
|
104
|
+
}
|
105
|
+
|
106
|
+
void set_themes(Rice::Object obj, Rice::Array themes) {
|
107
|
+
std::vector<theme_t> th = from_ruby<std::vector<theme_t>>(themes);
|
108
|
+
Rice::Data_Object<config_t>(obj)->themes = std::move(th);
|
109
|
+
}
|
110
|
+
|
111
|
+
Rice::Array get_themes(Rice::Object obj) {
|
112
|
+
std::vector<theme_t> const &themes = Rice::Data_Object<config_t>(obj)->themes;
|
113
|
+
|
114
|
+
Rice::Array arr;
|
115
|
+
for (theme_t const &t : themes)
|
116
|
+
arr.push(to_ruby<theme_t>(t));
|
117
|
+
|
118
|
+
return arr;
|
119
|
+
}
|
120
|
+
|
121
|
+
template<>
|
122
|
+
config_t from_ruby<config_t>(Rice::Object obj) {
|
123
|
+
config_t result;
|
124
|
+
|
125
|
+
result.life_time = from_ruby<size_t>(obj.call("life_time"));
|
126
|
+
result.mutation_chance = from_ruby<double>(obj.call("mutation_chance"));
|
127
|
+
result.population_size = from_ruby<size_t>(obj.call("population_size"));
|
128
|
+
result.tasks = from_ruby<size_t>(obj.call("tasks"));
|
129
|
+
result.themes = from_ruby<std::vector<theme_t>>(obj.call("themes"));
|
130
|
+
|
131
|
+
return result;
|
132
|
+
}
|
133
|
+
|
134
|
+
template<>
|
135
|
+
Rice::Object to_ruby<config_t>(config_t const &cnf) {
|
136
|
+
return Rice::Data_Object<config_t>(new config_t(cnf));
|
137
|
+
}
|
138
|
+
|
139
|
+
template<>
|
140
|
+
generator_t from_ruby<generator_t>(Rice::Object obj) {
|
141
|
+
config_t config = from_ruby<config_t>(obj.call("config"));
|
142
|
+
std::vector<question_t> questions = from_ruby<std::vector<question_t>>(obj.call("questions"));
|
143
|
+
|
144
|
+
return generator_t(std::move(config), std::move(questions));
|
145
|
+
}
|
146
|
+
|
147
|
+
template<>
|
148
|
+
Rice::Object to_ruby(generator_t const &t) {
|
149
|
+
return Rice::Data_Object<generator_t>(new generator_t(t));
|
150
|
+
}
|
151
|
+
|
152
|
+
template<>
|
153
|
+
Rice::Object to_ruby<generator_t::answer_t>(generator_t::answer_t const &ans) {
|
154
|
+
Rice::Array result;
|
155
|
+
for (std::vector<question_t> const &arr : ans) {
|
156
|
+
Rice::Array buffer;
|
157
|
+
for (question_t const &q : arr)
|
158
|
+
buffer.push(to_ruby<question_t>(q));
|
159
|
+
result.push(buffer);
|
160
|
+
}
|
161
|
+
return result;
|
162
|
+
}
|
163
|
+
|
164
|
+
extern "C" void Init_tasks_generator() {
|
165
|
+
Rice::Module rb_mTasksGenerator = Rice::define_module("TasksGenerator");
|
166
|
+
|
167
|
+
Rice::Data_Type<config_t> rb_cConfig = Rice::define_class_under<config_t>(rb_mTasksGenerator, "Config")
|
168
|
+
.define_constructor(Rice::Constructor<config_t, size_t>(), Rice::Arg("tasks") = 0)
|
169
|
+
.define_method("life_time=", &set_life_time)
|
170
|
+
.define_method("mutation_chance=", &set_mutation_chance)
|
171
|
+
.define_method("population_size=", &set_population_size)
|
172
|
+
.define_method("tasks=", &set_tasks)
|
173
|
+
.define_method("life_time", &get_life_time)
|
174
|
+
.define_method("mutation_chance", &get_mutation_chance)
|
175
|
+
.define_method("population_size", &get_population_size)
|
176
|
+
.define_method("tasks", &get_tasks)
|
177
|
+
.define_method("themes", &get_themes)
|
178
|
+
.define_method("themes=", &set_themes);
|
179
|
+
|
180
|
+
Rice::Data_Type<theme_t> rb_cTheme = Rice::define_class_under<theme_t>(rb_mTasksGenerator, "Theme")
|
181
|
+
.define_constructor(Rice::Constructor<theme_t, size_t, size_t, size_t>(),
|
182
|
+
(Rice::Arg("id"), Rice::Arg("dmin"), Rice::Arg("dmax")))
|
183
|
+
.define_method("theme_id", &theme_t::get_theme_id)
|
184
|
+
.define_method("difficulty_min", &theme_t::get_difficulty_min)
|
185
|
+
.define_method("difficulty_max", &theme_t::get_difficulty_max);
|
186
|
+
|
187
|
+
Rice::Data_Type<question_t> rb_cQuestion = Rice::define_class_under<question_t>(rb_mTasksGenerator, "Question")
|
188
|
+
.define_constructor(Rice::Constructor<question_t, size_t, size_t, size_t>(),
|
189
|
+
(Rice::Arg("id"), Rice::Arg("tid"), Rice::Arg("difficulty")))
|
190
|
+
.define_method("question_id", &question_t::get_question_id)
|
191
|
+
.define_method("theme_id", &question_t::get_theme_id)
|
192
|
+
.define_method("difficulty", &question_t::get_difficulty);
|
193
|
+
|
194
|
+
Rice::Data_Type<generator_t> rb_cGenerator = Rice::define_class_under<generator_t>(rb_mTasksGenerator, "Generator")
|
195
|
+
.define_constructor(Rice::Constructor<generator_t, config_t, std::vector<question_t>>(),
|
196
|
+
(Rice::Arg("cnf"), Rice::Arg("questions")))
|
197
|
+
.define_method("config", &generator_t::get_config)
|
198
|
+
.define_method("questions", &generator_t::get_questions)
|
199
|
+
.define_method("generate", &generator_t::generate);
|
200
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "theme.h"
|
4
|
+
#include "types.h"
|
5
|
+
|
6
|
+
namespace tasks_generator {
|
7
|
+
|
8
|
+
struct config_t {
|
9
|
+
size_t life_time;
|
10
|
+
double mutation_chance;
|
11
|
+
size_t population_size;
|
12
|
+
|
13
|
+
size_t tasks;
|
14
|
+
|
15
|
+
std::vector<theme_t> themes;
|
16
|
+
|
17
|
+
config_t(config_t const &cnf) :
|
18
|
+
life_time(cnf.life_time),
|
19
|
+
mutation_chance(cnf.mutation_chance),
|
20
|
+
population_size(cnf.population_size),
|
21
|
+
tasks(cnf.tasks),
|
22
|
+
themes(cnf.themes) {
|
23
|
+
}
|
24
|
+
|
25
|
+
explicit config_t(size_t tasks = 0) :
|
26
|
+
life_time(1000),
|
27
|
+
mutation_chance(0.05),
|
28
|
+
population_size(1000),
|
29
|
+
tasks(tasks),
|
30
|
+
themes() {
|
31
|
+
}
|
32
|
+
|
33
|
+
config_t &operator = (config_t const &cnf) {
|
34
|
+
life_time = cnf.life_time;
|
35
|
+
mutation_chance = cnf.mutation_chance;
|
36
|
+
population_size = cnf.population_size;
|
37
|
+
tasks = cnf.tasks;
|
38
|
+
themes = cnf.themes;
|
39
|
+
return *this;
|
40
|
+
}
|
41
|
+
};
|
42
|
+
|
43
|
+
}
|
@@ -0,0 +1,141 @@
|
|
1
|
+
#include <algorithm>
|
2
|
+
#include <unordered_map>
|
3
|
+
#include <unordered_set>
|
4
|
+
#include <vector>
|
5
|
+
|
6
|
+
#include <cassert>
|
7
|
+
#include <cstdlib>
|
8
|
+
|
9
|
+
#include "function.h"
|
10
|
+
#include "question.h"
|
11
|
+
|
12
|
+
namespace tasks_generator {
|
13
|
+
|
14
|
+
population_t generate_population(config_t const &config, std::vector<question_t> const &questions) {
|
15
|
+
|
16
|
+
std::unordered_map<theme_id_t, std::vector<question_t>> all_questions;
|
17
|
+
for (question_t const &question : questions)
|
18
|
+
all_questions[question.get_theme_id()].push_back(question);
|
19
|
+
for (std::pair<theme_id_t const, std::vector<question_t>> &q : all_questions)
|
20
|
+
std::sort(q.second.begin(), q.second.end(), question_t::difficulty_cmp);
|
21
|
+
|
22
|
+
population_t population(config.population_size);
|
23
|
+
|
24
|
+
srand((unsigned int)time(0));
|
25
|
+
|
26
|
+
for (individual_t &individual : population) {
|
27
|
+
individual = individual_t(config.tasks, task_t(config.themes.size()));
|
28
|
+
|
29
|
+
for (task_t &task : individual) {
|
30
|
+
task = task_t(config.themes.size());
|
31
|
+
|
32
|
+
std::unordered_set<question_id_t> used;
|
33
|
+
|
34
|
+
for (size_t i = 0; i < task.size(); ++i) {
|
35
|
+
std::vector<question_t> const &candidates = all_questions[config.themes[i].get_theme_id()];
|
36
|
+
|
37
|
+
std::vector<question_t>::const_iterator lower = std::lower_bound(
|
38
|
+
candidates.begin(),
|
39
|
+
candidates.end(),
|
40
|
+
config.themes[i].get_difficulty_min(),
|
41
|
+
question_t::difficulty_size_t_cmp
|
42
|
+
);
|
43
|
+
|
44
|
+
size_t unused_count = 0;
|
45
|
+
std::vector<question_t>::const_iterator it = lower;
|
46
|
+
while (it != candidates.end() && it->get_difficulty() <= config.themes[i].get_difficulty_max()) {
|
47
|
+
if (used.find(it->get_question_id()) == used.end())
|
48
|
+
++unused_count;
|
49
|
+
++it;
|
50
|
+
}
|
51
|
+
|
52
|
+
if (unused_count == 0)
|
53
|
+
throw std::logic_error("hasn't questions for generate test");
|
54
|
+
|
55
|
+
size_t pos = rand() % unused_count;
|
56
|
+
|
57
|
+
it = lower;
|
58
|
+
while (pos > 0 || used.find(it->get_question_id()) != used.end()) {
|
59
|
+
if (used.find(it->get_question_id()) == used.end())
|
60
|
+
--pos;
|
61
|
+
++it;
|
62
|
+
}
|
63
|
+
|
64
|
+
task[i] = *it;
|
65
|
+
used.insert(it->get_question_id());
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
return population;
|
71
|
+
}
|
72
|
+
|
73
|
+
static double fitness(individual_t const &individual) {
|
74
|
+
double s = 0, sqrs = 0;
|
75
|
+
for (task_t const &task : individual) {
|
76
|
+
double avg = 0;
|
77
|
+
for (question_t const &question : task)
|
78
|
+
avg += question.get_difficulty();
|
79
|
+
avg /= task.size();
|
80
|
+
s += avg;
|
81
|
+
sqrs += avg * avg;
|
82
|
+
}
|
83
|
+
double d = (sqrs - s * s / individual.size()) / individual.size();
|
84
|
+
double metric_one = (d > 0 ? 1.0 / d + 1.0 : 2);
|
85
|
+
return metric_one; // FIXME: add diff metric
|
86
|
+
}
|
87
|
+
|
88
|
+
static individual_t crossover(individual_t const &a, individual_t const &b) {
|
89
|
+
// FIXME: change this crossover
|
90
|
+
assert(a.size() == b.size());
|
91
|
+
individual_t res;
|
92
|
+
size_t to = rand() % a.size();
|
93
|
+
for (size_t i = 0; i < to; ++i)
|
94
|
+
res.push_back(a[i]);
|
95
|
+
for (size_t i = to; i < b.size(); ++i)
|
96
|
+
res.push_back(b[i]);
|
97
|
+
return res;
|
98
|
+
}
|
99
|
+
|
100
|
+
static void sort_by_fitness(population_t &population) {
|
101
|
+
std::vector<std::pair<double, size_t>> buf(population.size());
|
102
|
+
for (size_t i = 0; i < population.size(); ++i) {
|
103
|
+
buf[i].first = fitness(population[i]);
|
104
|
+
buf[i].second = i;
|
105
|
+
}
|
106
|
+
std::sort(buf.begin(), buf.end());
|
107
|
+
for (size_t i = 0; i < population.size(); ++i)
|
108
|
+
std::swap(population[i], population[buf[i].second]);
|
109
|
+
}
|
110
|
+
|
111
|
+
individual_t best(population_t &population) {
|
112
|
+
assert(!population.empty());
|
113
|
+
sort_by_fitness(population);
|
114
|
+
return population.front();
|
115
|
+
}
|
116
|
+
|
117
|
+
void selection(config_t const &config, population_t &population) {
|
118
|
+
// FIXME: make selection better
|
119
|
+
sort_by_fitness(population);
|
120
|
+
population.resize(config.population_size);
|
121
|
+
}
|
122
|
+
|
123
|
+
void recombination(config_t const &config, population_t &population) {
|
124
|
+
if (population.empty())
|
125
|
+
throw std::logic_error("population is empty");
|
126
|
+
std::vector<individual_t> newbies;
|
127
|
+
for (size_t i = 0; i < population.size(); ++i) {
|
128
|
+
size_t pos = i + rand() % (population.size() - i);
|
129
|
+
std::swap(population[i], population[pos]);
|
130
|
+
}
|
131
|
+
for (size_t i = 0; i < population.size(); i += 2) {
|
132
|
+
newbies.push_back(crossover(population[i], population[i + 1]));
|
133
|
+
}
|
134
|
+
for (size_t i = 0; i < newbies.size(); ++i)
|
135
|
+
population.emplace_back(std::move(newbies[i]));
|
136
|
+
}
|
137
|
+
|
138
|
+
void mutation(config_t const &, population_t &) {
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include <algorithm>
|
4
|
+
#include <vector>
|
5
|
+
|
6
|
+
#include "config.h"
|
7
|
+
#include "question.h"
|
8
|
+
#include "types.h"
|
9
|
+
|
10
|
+
namespace tasks_generator {
|
11
|
+
|
12
|
+
population_t generate_population(config_t const &config, std::vector<question_t> const &questions);
|
13
|
+
|
14
|
+
individual_t best(population_t &population);
|
15
|
+
|
16
|
+
void selection(config_t const &config, population_t &population);
|
17
|
+
|
18
|
+
void recombination(config_t const &config, population_t &population);
|
19
|
+
|
20
|
+
void mutation(config_t const &config, population_t &population);
|
21
|
+
|
22
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#include <vector>
|
2
|
+
|
3
|
+
#include "generator.h"
|
4
|
+
|
5
|
+
#include "function.h"
|
6
|
+
#include "question.h"
|
7
|
+
#include "types.h"
|
8
|
+
|
9
|
+
namespace tasks_generator {
|
10
|
+
|
11
|
+
generator_t::answer_t generator_t::generate() const {
|
12
|
+
population_t population = generate_population(config, questions);
|
13
|
+
|
14
|
+
for (size_t current_time = 0; current_time < config.life_time; ++current_time) {
|
15
|
+
selection(config, population);
|
16
|
+
recombination(config, population);
|
17
|
+
mutation(config, population);
|
18
|
+
}
|
19
|
+
|
20
|
+
return best(population);
|
21
|
+
}
|
22
|
+
|
23
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "function.h"
|
4
|
+
#include "question.h"
|
5
|
+
#include "types.h"
|
6
|
+
|
7
|
+
namespace tasks_generator {
|
8
|
+
|
9
|
+
class generator_t {
|
10
|
+
config_t config;
|
11
|
+
std::vector<question_t> questions;
|
12
|
+
|
13
|
+
public:
|
14
|
+
typedef std::vector<std::vector<question_t>> answer_t;
|
15
|
+
|
16
|
+
generator_t(generator_t const &t) :
|
17
|
+
config(t.config),
|
18
|
+
questions(t.questions) {
|
19
|
+
}
|
20
|
+
|
21
|
+
generator_t(config_t const &config, std::vector<question_t> const &questions) :
|
22
|
+
config(config),
|
23
|
+
questions(questions) {
|
24
|
+
}
|
25
|
+
|
26
|
+
generator_t(config_t &&config, std::vector<question_t> &&questions) :
|
27
|
+
config(config),
|
28
|
+
questions(std::move(questions)) {
|
29
|
+
}
|
30
|
+
|
31
|
+
config_t get_config() const {
|
32
|
+
return config;
|
33
|
+
}
|
34
|
+
|
35
|
+
std::vector<question_t> const &get_questions() const {
|
36
|
+
return questions;
|
37
|
+
}
|
38
|
+
|
39
|
+
answer_t generate() const;
|
40
|
+
};
|
41
|
+
|
42
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include <string>
|
4
|
+
|
5
|
+
namespace tasks_generator {
|
6
|
+
|
7
|
+
class question_t {
|
8
|
+
size_t question_id, theme_id, difficulty;
|
9
|
+
|
10
|
+
public:
|
11
|
+
question_t() :
|
12
|
+
question_id(0),
|
13
|
+
theme_id(0),
|
14
|
+
difficulty(0) {
|
15
|
+
}
|
16
|
+
|
17
|
+
question_t(size_t qid, size_t tid, size_t difficulty) :
|
18
|
+
question_id(qid),
|
19
|
+
theme_id(tid),
|
20
|
+
difficulty(difficulty) {
|
21
|
+
}
|
22
|
+
|
23
|
+
size_t get_difficulty() const {
|
24
|
+
return difficulty;
|
25
|
+
}
|
26
|
+
|
27
|
+
size_t get_question_id() const {
|
28
|
+
return question_id;
|
29
|
+
}
|
30
|
+
|
31
|
+
size_t get_theme_id() const {
|
32
|
+
return theme_id;
|
33
|
+
}
|
34
|
+
|
35
|
+
bool operator == (question_t const &q) const {
|
36
|
+
return question_id == q.question_id;
|
37
|
+
}
|
38
|
+
|
39
|
+
static bool theme_id_cmp(question_t const &a, question_t const &b) {
|
40
|
+
return a.theme_id < b.theme_id;
|
41
|
+
}
|
42
|
+
|
43
|
+
static bool difficulty_cmp(question_t const &a, question_t const &b) {
|
44
|
+
return a.difficulty < b.difficulty;
|
45
|
+
}
|
46
|
+
|
47
|
+
static bool difficulty_size_t_cmp(question_t const &a, size_t d) {
|
48
|
+
return a.difficulty < d;
|
49
|
+
}
|
50
|
+
};
|
51
|
+
|
52
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "question.H"
|
4
|
+
|
5
|
+
namespace tasks_generator {
|
6
|
+
|
7
|
+
class theme_t {
|
8
|
+
size_t theme_id, difficulty_min, difficulty_max;
|
9
|
+
|
10
|
+
public:
|
11
|
+
theme_t(size_t tid, size_t dmin, size_t dmax) :
|
12
|
+
theme_id(tid),
|
13
|
+
difficulty_min(dmin),
|
14
|
+
difficulty_max(dmax) {
|
15
|
+
}
|
16
|
+
|
17
|
+
theme_t(theme_t const &th) :
|
18
|
+
theme_id(th.theme_id),
|
19
|
+
difficulty_min(th.difficulty_min),
|
20
|
+
difficulty_max(th.difficulty_max) {
|
21
|
+
}
|
22
|
+
|
23
|
+
size_t get_theme_id() const {
|
24
|
+
return theme_id;
|
25
|
+
}
|
26
|
+
|
27
|
+
size_t get_difficulty_min() const {
|
28
|
+
return difficulty_min;
|
29
|
+
}
|
30
|
+
|
31
|
+
size_t get_difficulty_max() const {
|
32
|
+
return difficulty_max;
|
33
|
+
}
|
34
|
+
|
35
|
+
bool operator == (theme_t const &t) const {
|
36
|
+
return theme_id == t.theme_id &&
|
37
|
+
difficulty_min == t.difficulty_min &&
|
38
|
+
difficulty_max == t.difficulty_max;
|
39
|
+
}
|
40
|
+
};
|
41
|
+
|
42
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include <vector>
|
4
|
+
|
5
|
+
#include "question.h"
|
6
|
+
|
7
|
+
#define __unsed __attribute__((unused))
|
8
|
+
|
9
|
+
namespace tasks_generator {
|
10
|
+
|
11
|
+
typedef std::vector<question_t> task_t;
|
12
|
+
typedef std::vector<task_t> individual_t;
|
13
|
+
typedef std::vector<individual_t> population_t;
|
14
|
+
|
15
|
+
typedef size_t theme_id_t;
|
16
|
+
typedef size_t question_id_t;
|
17
|
+
|
18
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'tasks_generator/version'
|
7
|
+
|
8
|
+
spec = Gem::Specification.new do |spec|
|
9
|
+
spec.name = "tasks_generator"
|
10
|
+
spec.version = TasksGenerator::VERSION
|
11
|
+
spec.authors = ["Arslan Urtashev"]
|
12
|
+
spec.email = ["urtashev@gmail.com"]
|
13
|
+
spec.summary = %q{Test Tasks Generator}
|
14
|
+
spec.description = %q{Test Tasks Generator for AI lab MEPhI}
|
15
|
+
spec.homepage = 'https://github.com/Avitella/tasks_generator'
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rice"
|
26
|
+
|
27
|
+
spec.platform = Gem::Platform::RUBY
|
28
|
+
spec.extensions = %w[ext/tasks_generator/extconf.rb]
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tasks_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arslan Urtashev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rice
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Test Tasks Generator for AI lab MEPhI
|
56
|
+
email:
|
57
|
+
- urtashev@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions:
|
60
|
+
- ext/tasks_generator/extconf.rb
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- ext/tasks_generator/bindings.cc
|
68
|
+
- ext/tasks_generator/config.h
|
69
|
+
- ext/tasks_generator/extconf.rb
|
70
|
+
- ext/tasks_generator/function.cc
|
71
|
+
- ext/tasks_generator/function.h
|
72
|
+
- ext/tasks_generator/generator.cc
|
73
|
+
- ext/tasks_generator/generator.h
|
74
|
+
- ext/tasks_generator/question.h
|
75
|
+
- ext/tasks_generator/theme.h
|
76
|
+
- ext/tasks_generator/types.h
|
77
|
+
- lib/tasks_generator.rb
|
78
|
+
- lib/tasks_generator/version.rb
|
79
|
+
- tasks_generator.gemspec
|
80
|
+
homepage: https://github.com/Avitella/tasks_generator
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Test Tasks Generator
|
104
|
+
test_files: []
|