/**
 * @brief CompositionCoreUNITTest::chunkTest
 *
 * Checks the world chunk drawing stuff (i.e. the
 * things that keeps track of resources/other static
 * shit in the world)
 */
void CompositionCoreUNITTest::chunkTest()
{
    tst_drawRegistry registry;
    QRect screenbounds(registry.screenGeometry());

    /*these aren't working correctly. Someone smarter than I will have to figure them out later...*/
//    QTest::ignoreMessage(QtWarningMsg, "QPaintDevice::metrics: Device has no metric information");
//    QTest::ignoreMessage(QtWarningMsg, "Unrecognised metric 1!");
//    QTest::ignoreMessage(QtWarningMsg, "Unrecognised metric 2!");

    coWorld_p world (new coWorld(screenbounds, screenbounds));
    coWorld::t_simtag t = world->addObject(tstNullRenderable(), scTaskIterator(point(0,0)));


    coTagLocFunctor tagFunc (world, t);
    coChunkRenderer<coTagLocFunctor> cr(world, gcImage("face"), tagFunc);
    world->addObject(cr);

    measure_type r = maxLateralRenderDist(screenbounds);

    scSimulationStep *cgen = new scChunkGenerator<coTagLocFunctor>(tagFunc, r);
    scSimulationStep_p sstep (cgen);

    world->registerSimulationStep(sstep);
    world->simulate(10);\
    world->draw(registry);


    auto resourceDrawn = [&] (scResourceDeposit rd) {
        QVERIFY(registry.hasDrawn(rd.location()));
    };

    auto comparePoints = [&] (scChunkManager::const_chunk_description cd) {
        std::for_each(cd.beginDeposits(), cd.endDeposits(), resourceDrawn);
    };


    world->chunkCallback(tagFunc(), r, comparePoints);
}
Example #2
0
int RadialNeuron::optimizeRadialNeuron()
{
    try
    {
            std::vector<double> par;
            for(double c:C_N)
            {
                par.push_back(c);
            }
            par.push_back(sigm);
            double delta_E=10;
            double delta_par=10;
            double delta_E_min=1e-4;
            double E_prev=tagFunc(theachPoints,par);

            std::vector<double> par_plus_lambda{par};

            std::vector<double> par_minus_lambda{par};

            //std::vector<double> par_prev{par};

            std::vector<double> par_new{par};

            std::vector<double> lambda;
            lambda.resize(par.size(),1e-2);
            double increase=1.2;
            double reduce=0.8;

            std::vector<double> b_factor;
            b_factor.resize(par.size(),2.5);
            double b_factor_min=0.5;
            std::vector<bool> prev_dir={true,true,true};
            std::vector<bool> new_dir={true,true,true};

            double delta_par_min=1e-6;

            int success_step_count=0;
            int need_success_step=3;

            int max_iter=200;

        while(--max_iter)
        {
            int count_mis=0;

               for(int i=0;i<par.size();++i)
               {
                    par_plus_lambda=par_new;
                    par_minus_lambda=par_new;
                    par_plus_lambda[i]+=lambda[i];
                    par_minus_lambda[i]-=lambda[i];

                    double E1=tagFunc(theachPoints,par_plus_lambda);
                    double E2=tagFunc(theachPoints,par_minus_lambda);

                    if(E_prev>E1)
                    {
                        E_prev=E1;
                        par_new[i]=par_plus_lambda[i];
                        new_dir[i]=true;
                        //lambda[i]*=increase;
                    }
                    else if(E_prev>E2)
                    {
                        E_prev=E2;
                        par_new[i]=par_minus_lambda[i];
                        new_dir[i]=false;
                       // lambda[i]*=increase;
                    }
                    else
                    {
                        ++count_mis;
                        lambda[i]*=reduce;
                    }

               }

            if(count_mis==par.size())
            {
                par_new=par;
                new_dir=prev_dir;
                continue;
            }

            int max_num_iter=30;
            std::vector<double> next_par;

           while(--max_num_iter)
           {
               next_par.clear();
               next_par=par;
               for(int i=0;i<par_new.size();++i)
               {
                   if(prev_dir[i]!=new_dir[i])

                    b_factor[i]*=reduce;

                   else

                    b_factor[i]*=increase;

                   double n_par=next_par[i]+=b_factor[i]*(par_new[i]-par[i]);
                    ;
               }

               double E_next=tagFunc(theachPoints,next_par);
               //if(E_next<1e-6)
               // goto end_optimize;

               if(E_next<E_prev)
               {
                   break;
               }
               else
               {
                   for(double& b:b_factor)
                        b*=reduce;
                        continue;
               }
                double b_factor_max=0;
                for(auto b:b_factor)
                {
                    b_factor_max=b>b_factor_max?b:b_factor_max;
                }
               if(b_factor_max<b_factor_min)
                    goto end_optimize;
           }

            double next_E=tagFunc(theachPoints,next_par);

            if(next_E> E_prev)
                goto end_optimize;
            if(next_E>1e-10)

            delta_E=std::fabs(next_E-E_prev)/next_E;

            else
                break;
            delta_par=0;
            for(int i=0;i<par.size();++i)
            {
                if(std::fabs(par[i])>1e-10)
                    delta_par+=std::fabs((par[i]-next_par[i])/par[i]);
            }

            if(delta_par<delta_par_min)
                goto end_optimize;

            double s=next_par[dimension];
            if(next_par[dimension]>4)
            {
                next_par[dimension]=3;
                next_E=tagFunc(theachPoints,next_par);
                b_factor[dimension]=0.;
            }



            double Cn[2]={0.,0.};
            for(int i=0;i<2;++i)
                Cn[i]=next_par[i];
            par_new=next_par;
            par=next_par;
            prev_dir=new_dir;
            E_prev=next_E;
        }

    end_optimize:

    sigm=par[dimension];

    for(int i=0;i<dimension;++i)
    {
        C_N[i]=par[i];
    }

    }
    catch(std::string s)
    {
        std::cout<<s<<std::endl;
        return -1;
    }
    return 0;
}