#include <stdio.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <direct.h>
#include <vector>
#define INSTANCE_NUM 7
#define RUN_TIMES 10
#define ALG_NUM 2
#define OBJ_NUM 2
#define eps 1e-8
using namespace std;
struct Chromosome {//
ダブルfを解決[OBJ_NUM]; // 5つの目標値、f [0]:車両の数、f [1]:総移動距離; f [2]:長い経路の移動時間; f [3] :合計待機時間; f [4]:合計遅延時間
double unit_f [OBJ_NUM]; //等化の5つのターゲット
double min_dis;
};
int indi_num;
vector <Chromosome> EP;
vector <Chromosome> total_best;
vector <vector <Chromosome >> algSets [ALG_NUM];
bool is_dominate(Chromosome&s1、Chromosome&s2){
if((int)(s1.f [0] / 100000)<(int)(s2.f [0] / 100000)){
return true;
}
if((int)(s1.f [0] / 100000)>(int)(s2.f [0] / 100000)){
return false;
}
if(s1.f [1]> s2.f [1] + eps || s1.f [0]> s2.f [0] + eps){
return false;
}
return(s1.f [1] + eps <s2.f [1] || s1.f [0] + eps <s2.f [0]);
}
bool is_the_same(Chromosome&s1、Chromosome&s2){
return(fabs(s1.f [1]-s2.f [1])<eps && fabs(s1.f [0]-s2.f [0])< eps);
}
void update_EP(Chromosome&indi){
//アーカイブ内の他のソリューションが支配的であるかどうかをチェックする
int size = EP.size()、i = 0;
while(i <size)
{
if(is_dominate(EP [i]、indi) || is_the_same(EP [i]、indi))
return;
if(is_dominate(indi、EP [i])){
EP [i] = EP.back();
EP.pop_back(); --size
;
}
else
i ++;
} // sはアーカイブ内の要素に支配されないため、アーカイブに追加します__archive
//ノードをアーカイブに追加
EP.push_back(indi);
}
void get_pareto_front(string path){
文字列名;
for(int j = 0; j <RUN_TIMES; j ++){
name = path + "_run_0" + char(j + '0')+ "_obj.txt";
fstream r_result(name、ios :: in);
r_result >> indi_num;
for(int i = 0; i <indi_num; i ++){
染色体chrom;
for(int j = 0; j <OBJ_NUM; j ++)
r_result >> chrom.f [j];
update_EP(chrom);
}
r_result.close();
}
}
bool is_better(Chromosome&chrome_1、Chromosome&chrome_2){
bool better = false;
for(int i = 0; i <OBJ_NUM; ++ i){
if(chrome_2.f [i] -chrome_1.f [i]> = 0.000001)
better = true;
if(chrome_1.f [ i] -chrome_2.f [i]> 0.000001)
return false; // chrome_1の連想マトリックスの方向が古いソリューションより大きい場合、chrome_1は不良であり、chrome_2にはメリットがあります。falseを返します
} // chrome_2の連想マトリックスすべてがchrome_1よりも大きい、better = true、chrome_1は良い、chrome_2は悪い
リターンが良い;
}
/ *アーカイブ内のソリューションが新しいソリューションと等しいかどうかを判断する
chrome_1:アーカイブ内のソリューション;
chrome_2:新しいソリューション
2つのソリューションの一方の方向の値が0.00001より大きい限り、2つのソリューションは等しくありません。falseを返すelse
trueを返す
* /
bool is_equal(Chromosome&chrome_1、Chromosome&chrome_2){
for(int i = 0; i <OBJ_NUM; ++ i)(
if(fabs(chrome_1.f [i] -chrome_2.f [i])> 0.000001)return false; // 2つのソリューションが特定の方向にある限りの値は0.00001より大きく、2つのソリューションは等しくありません。Return false
}
return true;
}
bool update_best(Chromosome&new_chromosome){
if(total_best.size()== 0)
total_best.push_back(new_chromosome);
else {
//アーカイブEPのソリューションをnew_chromosome
ベクトルと比較します<Chromosome> :: iterator iter = total_best.begin();
for(; iter!= total_best.end(); ++ iter){
//コンテナ内のすべてのiterをnew_chromosomeと比較し、new_chromosomeよりも優れているか|| ifis is
(better(* iter、new_chromosome)|| is_equal (* iter、new_chromosome))
return false;
} // new_chromosomeはEPのすべてのソリューションよりも優れています
// new_chromosomeの劣ったソリューションをEPから削除
iter = total_best.begin();
while(iter!= Total_best.end()){
if(is_better(new_chromosome、* iter))iter = total_best.erase(iter);
else ++ iter;
}
//支配的なソリューションセットにnew_chromosomeを追加します
total_best.push_back(new_chromosome);
}
return true;
}
bool cmp1(染色体a、染色体b){
return af [0] <bf [0] || (af [0] == bf [0] && af [1] <bf [1]);
}
/ *
M-NSGA-II \ MTEA \
* /
void main(int argc、char ** argv){
string instance [INSTANCE_NUM] = {"E051-05e"、 "E076-10e"、 "E101-08e"、 "E101-10c"、 "E121-07c"、 "E151-12c"、 "E200-17c"};
文字列名;
//
(int i = 0; i <INSTANCE_NUM; i ++)のインスタンスごとに10実行{//案例数目
for(int a = 0; a <ALG_NUM; a ++){//比算法的数目
EP.clear() ;
文字列パス= argv [a + 1] +インスタンス[i];
for(int j = 0; j <RUN_TIMES; j ++){
name = path + "_run_0" + char(j + '0')+ "_obj.txt";
fstream r_result(name、ios :: in);
r_result >> indi_num;
for(int k = 0; k <indi_num; k ++){
染色体クロム;
for(int o = 0; o <OBJ_NUM; o ++)
r_result >> chrom.f [o];
update_EP(chrom);
}
r_result.close();
}
// EPをトラバースし、2つのアルゴリズムのパレートフロンティアを見つける
algSets [a] .push_back(EP);
}
total_best.clear();
for(int a = 0; a <ALG_NUM; a ++){
for(int j = 0; j <algSets [a] [i] .size(); j ++){
update_best(algSets [a] [i] [j]);
}
}
sort(total_best.begin()、total_best.end()、cmp1);
文字列str = to_string(i);
ファイル* file_pf = fopen(( "front \\" + str + ".txt")。c_str()、 "w");
fprintf(file_pf、 "%d \ n"、total_best.size());
for(int i = 0; i <total_best.size(); i ++)
{
fprintf(file_pf、 "%.8lf%.8lf \ n"、total_best [i] .f [0]、total_best [i] .f [ 1]);
}
fclose(file_pf);
}
}