Пример #1
0
chromosome eStrategy::xover_and_mut(const chromosome* const p1, const chromosome* const p2) const {
    p1->validate(), p2->validate();
    assert(p1->sigmas.size() == p2->sigmas.size() && p1->sigmas.size() == CONF_DIM);
    double prob = frand();
    auto sp = p1;
    if(prob > 0.5) sp = p2;
    chromosome::data_block new_sigmas, new_genes;
    if(prob < CONF_PROB_MUT) {
        double step = exp(PARAM_TAW_PRIME * normrnd() + CONST_STEP_GLOB);
        auto xd = normrnd();
        for(size_t index = 0; index < sp->sigmas.size(); index++) {
            auto s = sp->sigmas.at(index) * step;
            if(s < CONF_RANGE_SIGMA && s > -CONF_RANGE_SIGMA)
                s = s / abs(s) * CONF_RANGE_SIGMA;
            new_sigmas.push_back(s);
            auto g = sp->genes.at(index) + s * xd;
            if(g > CONF_BOUND_UPPER) g = CONF_BOUND_UPPER;
            else if(g < CONF_BOUND_LOWER) g = CONF_BOUND_LOWER;
            new_genes.push_back(g);
        }
        assert(new_genes.size() == new_sigmas.size());
    } else if((1 - prob) < CONF_PROB_XOVER) {
        double rp = frand(), rq = frand();
        for(size_t index = 0; index < sp->genes.size(); index++) {
            auto g = (p1->genes.at(index) * rp + p2->genes.at(index) * rq);
            if(g > CONF_BOUND_UPPER) g = CONF_BOUND_UPPER;
            else if(g < CONF_BOUND_LOWER) g = CONF_BOUND_LOWER;
            new_genes.push_back(g);
            if(frand() < 0.5) new_sigmas.push_back(p1->sigmas.at(index));
            else new_sigmas.push_back(p2->sigmas.at(index));
        }
    }
    auto c = chromosome(new_genes, new_sigmas);
    if(this->eval(c) < sp->get_fitness())
        return c;
    return chromosome(*sp);
}
Пример #2
0
int CGreyModel::gen_rand_solution(int num)
{
    
//    m_curr_population_num += num;

    
    if (m_curr_population_num >= m_max_population_num)
    {
        return FAIL;
    }
    
    
    
    DataType temp_a = 0.00;
    DataType temp_u = 0.00;
    
    DataType fitness = 0.00;
    DataType fitness_min = 100000000.00;
    DataType fitness_max = 0.00;
    DataType epsinon  = 0.000000000001;
    
//    CData fitness_set;
    
//    vector<int> seed_num;
    
//    srand((unsigned)time(NULL)); /*随机数初始化*/
    
    int i;
    for (i=0; i<num; i++)
    {
//        srand((unsigned)time(NULL)); /*随机数初始化*/
        
        temp_a = get_rand_a();
        temp_u = get_rand_u();  
        
//        cout<<"temp_a="<<temp_a<<endl;
//        cout<<"temp_u="<<temp_u<<endl;
        
        
//        a.push_back(temp_a);
//        u.push_back(temp_u);
 //            
        fitness = get_fitness(temp_a, temp_u);
        
        Fitness temp;
        temp.fitness = fitness;
        temp.a_value = temp_a;
        temp.u_value = temp_u;
        
        
        fitness_set.push_back(temp);
        
//        seed_num.push_back(0);
        
    	//记录最小的fitness
		if ( fitness_min - fitness >= epsinon )
		{
			fitness_min = fitness;				         	
		}
		
		//记录最大的fitness
		if ( fitness - fitness_max >= epsinon )
		{
			fitness_max = fitness;				         	
		}
		
		
//		cout<<"fitness="<<fitness<<endl;
		                
    }
    
//    cout<<"fitness_max="<<fitness_max<<endl;
//    cout<<"fitness_min="<<fitness_min<<endl;
    
    
    DataType coefficient = 0.00;   
    coefficient = (DataType)(m_max_seed-m_min_seed)/(DataType)(fitness_max-fitness_min);
    
    int seed_num_temp = 0;
    for (i=0; i<num; i++)
    {
        seed_num_temp = (int)(coefficient*(fitness_set[i].fitness - fitness_min)+m_min_seed);
//        cout<<"seed_num_temp="<<seed_num_temp<<endl;
        
        if (seed_num_temp > 1)
        {
           m_curr_population_num += seed_num_temp;
           gen_rand_solution(seed_num_temp);
           
//           cout<<"m_curr_population_num="<<m_curr_population_num<<endl;
            
        }
        
        
    }
    
    
    

		
	return SUCC;
}