//文件routgene.h
#pragma once
#include "nodeset.h"
class RoutGene
{
public:
RoutGene(int _nodeNum=0);
~RoutGene(void);
public:
void SetNodeNum(int _nodeNum);
void Init(int from, int to);
//gene增加element片段
void Increase(int element);
void operator =(RoutGene &gene);
bool IsMax();
public:
friend NodeSet & NODE(RoutGene &gene);
//随即删除gene中的片段
friend void RANDOMDELETE(RoutGene &gene);
//打印:测试之用
friend void Print(RoutGene &gene);
private:
int nodeNum;
int last;
NodeSet geneData;
};
//文件routgene.cpp
#include "routgene.h"
#using <mscorlib.dll>
#include "resource.h"
#include <iostream>
using namespace GeneResource;
using namespace std;
RoutGene::RoutGene(int _nodeNum)
{
if(_nodeNum<0)
{
cout<<"\nError :the node number < 0 !\n\n";
exit(-1);
}
nodeNum = _nodeNum;
last = 0;
geneData.SetNodeNum(nodeNum);
}
RoutGene::~RoutGene(void)
{
}
void RoutGene::SetNodeNum(int _nodeNum)
{
if(_nodeNum<=1)
{
cout<<"\nError :the node number <= 1 !\n\n";
exit(-1);
}
nodeNum = _nodeNum;
last = 0;
geneData.SetNodeNum(nodeNum);
}
void RoutGene::Init(int from, int to)
{
last = 0;
for(int i=0;i<GENE_LENGTH;i++)
*(geneData.GetpNodeSet()+i) = -1;
*(geneData.GetpNodeSet())=from;
++ last;
*(geneData.GetpNodeSet()+last)=to;
}
void RoutGene::Increase(int element)
{//gene增加element片段
if(last<GENE_LENGTH-1)
{
*(geneData.GetpNodeSet()+last+1) = *(geneData.GetpNodeSet()+last);
*(geneData.GetpNodeSet()+last) = element;
++ last;
}
}
void RoutGene::operator =(RoutGene &gene)
{
nodeNum = gene.nodeNum;
last = gene.last;
geneData = gene.geneData;
}
bool RoutGene::IsMax()
{
return last>=GENE_LENGTH-1;
}
NodeSet & NODE(RoutGene &gene)
{
//TODO: return statement
return gene.geneData;
}
void RANDOMDELETE(RoutGene &gene)
{//随机删除gene中的片段
int randInteger;
randInteger = RandInteger(gene.last);
if(gene.last-randInteger >= 1 && randInteger>0 ){
for(int i=1;i<randInteger;i++)
*(gene.geneData.GetpNodeSet()+gene.last-i) = -1;
*(gene.geneData.GetpNodeSet()+gene.last-randInteger) =
*(gene.geneData.GetpNodeSet()+gene.last);
*(gene.geneData.GetpNodeSet()+gene.last) = -1;
gene.last -= randInteger;
}
}
void Print(RoutGene &gene)
{
cout<<"\n\nGene:\n";
for(int i=0;i<=gene.last;i++)
cout<<*(gene.geneData.GetpNodeSet()+i);
cout<<"\n\n";
}
本文地址:http://com.8s8s.com/it/it950.htm