#include "Defines.h"
#include "Params.h"
#include "Utils.h"
#include "File.h"
#include "GenAlg.h"

CParams	g_Params;

bool SortCriterion(const SGenome& p1, const SGenome& p2)
{
	return (p1.dFitness > p2.dFitness);
}
/*
int main()
{
	CFile::Write();
	return 0;
}
*/
int main()
{
	vector<SGenome> m_vecRes;
	SGenome	m_sGen;
	ofstream fout("Res.txt");
	ofstream fres[32];
	srand((unsigned)time(NULL));//埋下随机化种子
	CFile::ReadALL();

	CGenAlg* m_pGA = new CGenAlg();

	/*-------------进化过程-------------*/
	m_sGen.dDistance = MAXDOUBLE;
    for(unsigned int i=0; i<=CParams::iNumEpoch; ++i)
    {
    	fout << setprecision(9) << setiosflags(ios::fixed);
    	fout << "-----第" << i << "代-----" << endl;

    	m_vecRes.clear();
		m_vecRes = m_pGA->GetChromos();

		fout << "**最短距离:"	<< m_pGA->MinDistance () << "**" << endl;
		fout << "**最长距离:"	<< m_pGA->MaxDistance () << "**" << endl;

		for(unsigned int ii=0; ii<m_vecRes.size(); ++ii)
		{
			fout << "第" << ii+1 << "组点:" << endl;
			fout << "适应度:" << m_vecRes[ii].dFitness << endl;
			for(unsigned int jj=0; jj<CParams::iChromoLength; ++jj)
			{
				fout << "#第" << jj+1 << "点#:";
				for(int kk=0; kk<5; ++kk)
				{
					fout << m_vecRes[ii].vecDatas[jj].w[kk] << "\t";
				}
				fout << endl;
			}
		}

		if(i<CParams::iNumEpoch)	m_pGA->Epoch();
    }
    m_sGen = m_pGA->GetFittestGenome();
    fout.close();
    delete m_pGA;
    cout << "进化完成!" << endl;

	/*--------------输出操作-----------------*/
	fout.open("Best.txt");
	for(unsigned int i=0; i<CParams::iChromoLength; ++i)
	{
        string str = string("Points") + itoa(i+1, new char[3], 10) + ".txt";
        fres[i].open(str.c_str());
        fres[i] << setprecision(9) << setiosflags(ios::fixed);
	}

    double  m_dMinD = MAXDOUBLE;
    int     m_dMinJ = 0;
	double  m_Dis = 0;
	double  w[5] = {0};
	bool    m_bSort[5] = {false};
	vector<long> m_lCount(m_sGen.vecDatas.size(), 0);

	//求最优解的距离总和
	for(unsigned int i=0; i<CFile::datas.size(); ++i)
	{
		m_dMinJ = 0;
		m_dMinD = MAXDOUBLE;
		for(unsigned int j=0; j<m_sGen.vecDatas.size(); ++j)
		{
			m_Dis = Distance(CFile::datas[i], m_sGen.vecDatas[j]);
			if(m_dMinD > m_Dis)
			{
				m_dMinD = m_Dis;
				m_dMinJ = j;
			}
		}
		//将一个点归到一个类之中
		++m_lCount[m_dMinJ];
		//求所有维坐标总和和输出
		for(int j=0; j<5; ++j)
		{
			w[j] += CFile::datas[i].w[j];
		}
		fres[m_dMinJ] << CFile::datas[i] << endl;
	}
	//求维坐标平均值
	for(int i=0; i<5; ++i)
	{
		w[i] /= CFile::datas.size();
	}
	//输出32个聚类点信息
	for(unsigned int i=0; i<m_sGen.vecDatas.size(); ++i)
    {
		//输出计数
    	fout << i+1 << "\t";
    	//输出点的每个维及计算客户价值排序
    	for(int j=0; j<5; ++j)
    	{
			fout << m_sGen.vecDatas[i].w[j] << "\t";
			if(m_sGen.vecDatas[i].w[j] >= w[j])
				m_bSort[j] = true;
			else
				m_bSort[j] = false;
    	}
    	//输出点上的聚类数
    	fout << m_lCount[i] << "\t";
    	//计算客户价值排序
    	if(m_sGen.vecDatas[i].w[1] >= w[1])
			m_bSort[1] = false;
		else
			m_bSort[1] = true;
		//输出价值排序结果
		for(int j=0; j<5; ++j)
		{
			fout << m_bSort[j];
		}
		fout << endl;
		fres[i].close();
    }
    fout << "AVE:";
    for(int i=0; i<5; ++i)
    {
    	fout << "\t" << w[i];
    }
    fout << endl;
    fout.close();
    return 0;
}

