-
Notifications
You must be signed in to change notification settings - Fork 1
/
Optimization.h
78 lines (61 loc) · 2.74 KB
/
Optimization.h
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
// Copyright 2022 Gzh0821/Gaozih
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
//最优化头文件
#include <vector>
#include <tuple>
#include <random>
#include <set>
#include <memory>
#include <ctime>
using para_format = std::tuple<double, double, double, int>;//SA参数格式
class random_num_engine {//随机数生成引擎
std::random_device rand_source;//随机熵源
std::mt19937_64 rand_eng;//64位梅森环绕生成器
public:
random_num_engine() : rand_source(), rand_eng(time(nullptr) + rand_source()) {};
//下为函数对象定义
int operator()(int max) {//生成0-max的随机整数
rand_eng.seed(rand_eng() ^ time(nullptr) + rand_source());
return std::uniform_int_distribution<>(0, max - 1)(rand_eng);
}
double operator()(double start = 0.0, double end = 1.0) {//生成start-end的随机浮点数
rand_eng.seed(rand_eng() ^ time(nullptr) + rand_source());
return std::uniform_real_distribution<>(start, end)(rand_eng);
}
};
class Solution {//解决方案类
public:
static constexpr double e_v = 2.71828;//编译常量e
Solution() : order_record(std::make_unique<std::set<std::vector<int>>>()), machines(0), workpieces(0) {}
Solution(const Solution &other) : machines(other.machines), workpieces(other.workpieces),
cost_table(other.cost_table),
order_record(std::make_unique<std::set<std::vector<int>>>()) {}
~Solution() {
order_record.reset();
}
void setup(int id);
[[maybe_unused, nodiscard]] int HCS();
[[maybe_unused, nodiscard]] int SA(para_format input_para);
friend int cal_func(Solution &&,int);
private:
random_num_engine get_random_num;//随机数模块
int machines, workpieces; //机器数量和工件数量
std::vector<int> last_order, order, next_order;//当前序列
std::vector<std::vector<int>> cost_table;//工件耗时表
std::unique_ptr<std::set<std::vector<int>>> order_record;//独占指针指向存储序列的红黑树
inline void change_random_order();//随机换序
inline void change_adjacent_order(int wa);//相邻两点调换
int calc_order_time();//计算函数模块
};