예제 #1
0
void Monster::wander()
{
  if (wander_duration <= 0) {
    wander_target = Point( posx + rng(-3, 3), posy + rng(-3, 3) );
    wander_duration = 3;
  }
  move_towards(wander_target.x, wander_target.y);
}
예제 #2
0
void Monster::move_towards(Entity* entity)
{
  if (!entity) {
    debugmsg("Monster attempted move_towards() on a null target.");
    return;
  }
  move_towards(entity->posx, entity->posy);
}
예제 #3
0
파일: auto.c 프로젝트: ajinkya93/netbsd-src
/* move_away():
 * move away form the robot given
 */
static char
move_away(COORD *rob)
{
	int dx, dy;

	dx = sign(My_pos.x - rob->x);
	dy = sign(My_pos.y - rob->y);
	return  move_towards(dx, dy);
}
예제 #4
0
static void tract_reshape(tract *tr)
{
    SPFLOAT amount;
    SPFLOAT slow_return;
    SPFLOAT diameter;
    SPFLOAT target_diameter;
    int i;
    int current_obstruction;

    current_obstruction = -1;
    amount = tr->block_time * tr->movement_speed;

    for(i = 0; i < tr->n; i++) {
        slow_return = 0;
        diameter = tr->diameter[i];
        target_diameter = tr->target_diameter[i];

        if(diameter < 0.001) current_obstruction = i;

        if(i < tr->nose_start) slow_return = 0.6;
        else if(i >= tr->tip_start) slow_return = 1.0;
        else {
            slow_return =
                0.6+0.4*(i - tr->nose_start)/(tr->tip_start - tr->nose_start);
        }

        tr->diameter[i] = move_towards(diameter, target_diameter,
                slow_return * amount, 2 * amount);

    }

    if(tr->last_obstruction > -1 && current_obstruction == -1 &&
            tr->noseA[0] < 0.05) {
        append_transient(&tr->tpool, tr->last_obstruction);
    }
    tr->last_obstruction = current_obstruction;

    tr->nose_diameter[0] = move_towards(tr->nose_diameter[0], tr->velum_target,
            amount * 0.25, amount * 0.1);
    tr->noseA[0] = tr->nose_diameter[0] * tr->nose_diameter[0];
}
예제 #5
0
파일: auto.c 프로젝트: ajinkya93/netbsd-src
/* move_between():
 * move the closest heap between us and the closest robot
 */
static char
move_between(COORD *rob, COORD *hp)
{
	int dx, dy;
	float slope, cons;

	/* equation of the line between us and the closest robot */
	if (My_pos.x == rob->x) {
		/*
		 * me and the robot are aligned in x
		 * change my x so I get closer to the heap
		 * and my y far from the robot
		 */
		dx = - sign(My_pos.x - hp->x);
		dy = sign(My_pos.y - rob->y);
		CONSDEBUG(("aligned in x"));
	}
	else if (My_pos.y == rob->y) {
		/*
		 * me and the robot are aligned in y
		 * change my y so I get closer to the heap
		 * and my x far from the robot
		 */
		dx = sign(My_pos.x - rob->x);
		dy = -sign(My_pos.y - hp->y);
		CONSDEBUG(("aligned in y"));
	}
	else {
		CONSDEBUG(("no aligned"));
		slope = (My_pos.y - rob->y) / (My_pos.x - rob->x);
		cons = slope * rob->y;
		if (ABS(My_pos.x - rob->x) > ABS(My_pos.y - rob->y)) {
			/*
			 * we are closest to the robot in x
			 * move away from the robot in x and
			 * close to the scrap in y
			 */
			dx = sign(My_pos.x - rob->x);
			dy = sign(((slope * ((float) hp->x)) + cons) -
				  ((float) hp->y));
		}
		else {
			dx = sign(((slope * ((float) hp->x)) + cons) -
				  ((float) hp->y));
			dy = sign(My_pos.y - rob->y);
		}
	}
	CONSDEBUG(("me (%d,%d) robot(%d,%d) heap(%d,%d) delta(%d,%d)",
		My_pos.x, My_pos.y, rob->x, rob->y, hp->x, hp->y, dx, dy));
	return move_towards(dx, dy);
}
예제 #6
0
파일: engine.c 프로젝트: Crimson-Demon/IPP
void knights_act() {
    unit_list_elem_ptr our_list = us.king == player_1.king ? board.player_1_list : board.player_2_list;
    while(our_list != null) {
        if(our_list->unit->type == KNIGHT) {
            unit_ptr closest_unit = find_closest_unit(our_list->unit);
            //Move in the direction of the closest unit and fight
            if(closest_unit != null) {
                int x = our_list->unit->x;
                int y = our_list->unit->y;
                int x_closest = closest_unit->x;
                int y_closest = closest_unit->y;
                move_towards(x, y, x_closest, y_closest);
            } else {
                fprintf(stderr, "there is no closest unit! WTF?!");
            }
        }
        our_list = our_list->next;
    }
}
예제 #7
0
void Monster::take_turn()
{
// TODO: Move make_plans() outside of this function?
  make_plans();
  while (action_points > 0 && !dead) {
    if (wander_duration > 0) {
      wander_duration--;
    }
    if (target) {
      if (can_attack(target)) {
        attack(target);
      } else {
        move_towards(target);
      }
    } else {
      wander();
    }
  }
}
예제 #8
0
파일: main.c 프로젝트: cielo523/tsp-pso
int main()
{
    FILE *f_trace;
    FILE *f_graph; // File containing the graph
    FILE *f_run; // To save swarm positions, for each iteration

    int               auto_move_type;
    struct particle	best_hood; // Best particle in the neighbourhood
    double         best_best_f;
    struct coeff	coeff;
    double         eps_max,eps_mean,eps_min;
    int               hamilt;
    int				i;
    int                  improv,improv_best;
//int				iter,iter_min;
//struct graph   G2;
    char			graph[80]; // Graph file name
    int                  j;
    int                  k;
// int                 m;
//float             level;
//int               loop_auto_move;
    float			max_eval;
//int				max_iter;
    double         mean_eval;
    float			min_tour;
    int               move4;
    struct   particle new_p;
    int               norm_v;
    int               n_success;
//int				parallel;
    int               run,run_max;
    int scanR;
    int				swarm_size;
    double            zzz,zzz2;

    /*----------------- SOME PARAMETERS -------------- */

#include "param.c"
    f_trace=fopen("trace.txt","w");

    if (save==0) goto graph;
    /*-------------------------- */
    printf("\nChoose the file to save the run, please");
    scanR=scanf("%s",graph);

    f_run=fopen(graph,"w");

    /*-------------------------- */

graph:

    /* Read the valuated graph  (Note: value 0 <=> no arc) */

    printf("\nFile name for the graph, please: ");
    scanR=scanf("%s",graph);

    //printf("\nMultiply all arc values by: ");
    //scanf("%f",&integer_coeff)  ;
    integer_coeff=1;

    f_graph=fopen(graph,"r");

    G=read_graph(f_graph,trace);

    //display_graph(G);
    /*  // Save the graph
     for (i=0;i<G.N;i++)
    {
     fprintf(f_trace,"\n");
     for (j=0;j<G.N;j++) fprintf(f_trace,"%.0f ",G.v[i][j]);
    }
     return EXIT_SUCCESS;
     */

    printf("\n Just looking for Hamilton cycles  ? (y/n): ");
    scanR=scanf("%s",answer);

    if (answer[0]=='y')
    {
        G=TSP_to_Hamilton(G);
        return EXIT_SUCCESS;
    }

    G=graph_min_max(G); /* Compute the max and the min arc values, and the number of edges */
    min_tour=G.N*G.l_min;

    printf("\n Target  ? (suggestion %.0f) ",min_tour);
    scanR=scanf("%f",&target);

    /*------------------------------ */
//graph_study:

    hamilt=check_Hamilton_cycle(G);
    if (hamilt==0) printf("\n\n WARNING. There is NO Hamiltonian cycle. I look for a Hamiltonian path\n");
    if(hamilt==1 || hamilt==2)printf("\n\n LUCKY YOU. There IS at least one Hamiltonian cycle. I look for the best one\n");
    if (hamilt==3) printf("\n I don't know whether there is a Hamiltonian cycle or not. Let's try");

//parameters:

    printf("\n\n Default parameters");
    //swarm_size=G.N;  // Just a rule of thumb
    //zzz=0; for (i=2;i<G.N;i++) zzz=zzz+log(i);
    //swarm_size=2.46*zzz-55;   // Another rule of thumb (valid only if the result is >0
    swarm_size=2*G.N+1;    // Another rule of thumb

    //swarm_size=400;

    printf("\nSwarm size ......... %i",swarm_size);
    swarm_size=MAX(1,MIN(swarm_size,Max_size));
    sw1.size=swarm_size;

    // see param.c
    printf("\n Init option................ %i",init_option);
    printf("\n Neighbourhood size.. %i ",hood);
    printf("\nHood_type .............%i",hood_type);
    printf("\nMove_type ............. %i",move[0]);
    if(move[0]<=5)
    {
        printf("\nKappa value ...... %.2f",kappa) ;
        printf("\nPhi value ............%.2f",phi);
    }
    printf("\nAuto_move_type ....%i",move[1]);
    printf("\nSplice_around the best %i",move[2]);
    printf("\nSplice_around more.... %i",move[3]);
    printf("\nRebuild method .......... %i",move[4]);

    //same_best_thresh=G.N/(2*hood); //Just a rule of thumb

    size_max=G.N;   // Max size for splice_around. Just a rule of thumb

    printf("\n Do you want to modify them? (y/n): ") ;
    scanR=scanf("%s",answer);
    if (answer[0]=='n')   goto end_param;


    // -----------------------------------Ask for parameters

    printf("\n Swarm size? (max = %i) ",Max_size);
    scanR=scanf("%i",&swarm_size);
    swarm_size=MAX(1,MIN(swarm_size,Max_size));
    sw1.size=swarm_size;

    printf("\n Init option? (suggested 1 or 2): ");
    scanR=scanf("%i",&init_option);

    printf("\n Neighbourhood size? (max = %i) ",sw1.size);
    scanR=scanf("%i",&hood);
    hood=MAX(1,MIN(hood,sw1.size));

    printf("\n Hood type? (0 = social (quick) 1 = physical (long)\n (suggestion: %i):  ",hood_type);
    scanR=scanf(" %i",&hood_type);

    printf("\n Move type? (1 to 10) (suggestion: %i):  ",move[0]);
    scanR=scanf("%i",&move[0]);

    if(move[0]<=5)
    {
        printf("\n  kappa value? (suggestion: %.2f): ",kappa); // Constriction coefficients. See move_towards
        scanR=scanf("%f",&kappa);
        printf("\n  phi value? (suggestion: %.2f):  ",phi);
        scanR=scanf("%f",&phi);
    }


    printf("\n Auto-move type? (0 to 6) (suggestion: %i):  ",move[1]);
    scanR=scanf("%i",&move[1]);

    printf("\nSplice_around the best?(suggestion  %i)",move[2]);
    scanR=scanf("%i",&move[2]);
    printf("\nSplice_around more? (suggestion %i)",move[3]);
    scanR=scanf("%i",&move[3]);
    printf("\nRebuild method? (suggestion %i)",move[4]);
    scanR=scanf("%i",&move[4]);

end_param:
    //-------------------------------- Iterations
    printf("\n Max tour evaluations? (0 => end)  ");
    scanR=scanf("%f",&max_eval);
    if (max_eval==0)   goto the_end;

    printf("\n Trace level  ? (0,1,2,3,4)  ");
    scanR=scanf("%i",&trace);

    /* ======================================================== HERE IS THE ALGORITHM */

    printf("\n How many times? ");
    scanR=scanf("%i",&run_max);
    if (run_max==0 ) goto the_end;
    run=1;
    n_success=0;
    eps_min=10000*target;
    eps_max=0;
    eps_mean=0;
    mean_eval=0;

    if (move[4]>=3) // Initialize the blackboard
    {
        for (i=0; i<G.N; i++) for (j=0; j<G.N; j++) for (k=0; k<2; k++) BB[k].v[i][j]=0;
    }

loop_run:
    printf("\n ___________________________ RUN %i",run);
    eval_f=0;
    time=0; // Current time step
    splice_time=0; // Last time splice_around has been used
    // Initialize the swarm

    sw1=init_swarm(swarm_size,G,trace);

    printf("\nAfter init: eval %.0f  value %f for particle %i",eval_f, best_best.best_f,sw1.rank_best);
    if (best_best.best_f<=target) goto success;// Success

    if (save!=0) save_swarm(f_run,sw1); /* Save the run as text file */


    if(move[0]<=5 && move[0]!=0)
        coeff=convergence_case(kappa,phi);  // Coeffs for non spherical methods

moves:
    time=time+1;
    printf("\n total velocity %.0f",tot_vel(sw1));

    if (move[0]>0)
    {
        //----------------------------------------------------------------------------- Normal move
        printf("\nEval %.0f. Normal move %i",eval_f,move[0]);
        if (eval_f>=max_eval)  goto end_max_eval;
        best_best_f=best_best.best_f;     // Before the move

        for (i=0; i<sw1.size; i++)
        {
            // find the best in the neighbourhood (or the local queen)
            best_hood=best_neighbour(sw1,sw1.p[i],hood,hood_type,monotony,equiv_v,trace);

            // M O V E
            sw1.p[i]=move_towards(sw1.p[i],best_hood,coeff,move[0],explicit_vel,conv_case,equiv_v,trace);

            if (sw1.p[i].best_f<best_best.best_f)// Check best of the best after the move
            {
                best_best=sw1.p[i];
                sw1.rank_best=i;
                printf("\neval %.0f  value %f",eval_f, best_best.best_f);
                display_position(best_best,1);
            }

            if (best_best.best_f<=target) goto success;// Success

            if (best_best.best_f<best_best_f) goto moves; // Loop as soon as there is a global improvement

        } // next i for normal move
    }

    if (move[1]>0)
    {
        //------------------------------------------------------------- If no improvement, auto_move
        printf("  /auto_move %i",move[1]);
        auto_move_type=move[1];
        //   auto_move:
        for (i=0; i<sw1.size; i++)
        {
            sw1.p[i]= auto_move(sw1.p[i],auto_move_type,0,0,trace);
            if (sw1.p[i].best_f<best_best.best_f)
            {
                best_best=sw1.p[i];
                sw1.rank_best=i;
                if (best_best.best_f<=target)  goto success;// Success
                printf("\neval %.0f  value %f",eval_f, best_best.best_f);
                display_position(best_best,1);
                goto moves;
            }
            if (eval_f>=max_eval)  goto end_max_eval;
        } // next i for auto_move

    }

    if(move[2]>0)
    {
        // ---------------------------------------------------  If  still no improvement, splice_around best particle
        printf("  /splice_around, best particle");
        i= sw1.rank_best;
        sw1.p[i]= splice_around( sw1, sw1.p[i], hood_type,hood);

        if (sw1.p[i].best_f<best_best.best_f)// Best of the best after the move
        {
            best_best=sw1.p[i];
            if (best_best.best_f<=target)  goto success;// Success
            printf("\neval %.0f  value %f",eval_f, best_best.best_f);
            goto moves;
        }
    }

    if(move[3]>0)
    {
        // ---------------------------------------------------  If  still no improvement, splice_around more particles
        printf(" /splice_around, more, option %i",splice_around_option);
        improv=0;
        improv_best=0;
        splice_time=time;

        for (i=0; i<sw1.size; i++) // For each particle
        {
            zzz= sw1.p[i].f;
            zzz2= sw1.p[i].best_f;
            sw1.p[i]= splice_around( sw1, sw1.p[i], hood_type,hood);

            if (sw1.p[i].f<zzz)    improv=improv+1;
            if (sw1.p[i].best_f<zzz2)    improv_best=improv_best+1;

            if (sw1.p[i].best_f<best_best.best_f)// Best of the best after the move
            {
                best_best=sw1.p[i];
                sw1.rank_best=i;
                if (best_best.best_f<=target)  goto success;// Success
                printf("\neval %.0f  value %f",eval_f, best_best.best_f);
                goto moves;
            }

            if (eval_f>=max_eval)  goto end_max_eval;
        } // next i for splice_around

        // if (improv_best>sw1.size/2) goto moves;
    }

    if (move[4]==9) move4=alea(1,3);
    else move4=move[4];
    if(move4>0)
    {
        //--------------------------------- If still no improvement, rebuild  tours
        if (move4==1) printf("  /rebuild, random");
        if (move4==2) printf("  /rebuild, arc_around");
        if (move4==3) printf("  /rebuild, blackboard");

        for (i=0; i<sw1.size; i++)
        {
            switch( move4)
            {
            case 1:
                norm_v=alea(1,G.N);
                sw1.p[i].v=alea_velocity(norm_v);    // random velocity
                sw1.p[i]=pos_plus_vel(sw1.p[i],sw1.p[i].v,0,0,0); //  => random new position
                break;

            case 2:
                new_p=arc_around(sw1,sw1.p[i]);
                sw1.p[i].v=coeff_pos_minus_pos(new_p,sw1.p[i],monotony,0,equiv_v,trace);
                sw1.p[i]=new_p;
                break;

            case 3:
                new_p=BB_use(sw1.p[i]);
                sw1.p[i].v=coeff_pos_minus_pos(new_p,sw1.p[i],monotony,0,equiv_v,trace);
                sw1.p[i]=new_p;
                break;
            }
            if (sw1.p[i].best_f<best_best.best_f)/* Best of the best after the move */
            {
                best_best=sw1.p[i];
                if (best_best.best_f<=target)  goto success;// Success
                printf("\neval %.0f  value %f",eval_f, best_best.best_f);
                goto moves;
            }
            if (eval_f>=max_eval)  goto end_max_eval;
        }
    }

    // Primitive rehope
    printf("\n Primitive rehope");
    for (i=0; i<sw1.size; i++)
    {
        sw1.p[i].v=alea_velocity(G.N);
        sw1.p[i]= pos_plus_vel(sw1.p[i],sw1.p[i].v,0,0,0);
    }

    goto moves;


    //-----------------------------------
    // end_move:

    if (save!=0) save_swarm(f_run,sw1);
    if (trace>1) display_swarm(sw1);
    printf("\n Eval %.0f",eval_f);
    if (trace>0) display_position(best_best,2);

    goto moves;

end_max_eval:
    printf("\n\n Stop at max eval %.0f",eval_f);
    goto end;

success:
    n_success=n_success+1;
    printf("\n SUCCESS");

end:
    // Give the best position (sequence) found

    printf("\n Best solution found after %.0f evaluations",eval_f);
    if (best_best.best_x.s[0]!=0) best_best.best_x=rotate(best_best.best_x,0);
    display_position(best_best,2);

    // Check if no error
    zzz=f(best_best,-1,-1);
    if (zzz!=best_best.best_f)
    {
        printf("\n ERROR. The true f value is in fact %f ",zzz);
    }
    zzz=best_best.best_f;
    if (zzz<eps_min) eps_min= zzz;
    if (zzz>eps_max) eps_max= zzz;
    eps_mean=eps_mean+zzz;
    mean_eval=mean_eval+eval_f;

    if (save!=0) save_swarm(f_run,sw1);
    save_swarm(f_trace,sw1);

    if (trace>1) display_swarm(sw1);

    run=run+1;
    if (run<=run_max) goto loop_run;
    else
    {

        printf("\n With  swarm size=%i, max eval=%.0f => %i successes/%i. Rate=%.3f ",swarm_size,max_eval,n_success,run_max,(float)n_success/run_max);

        printf("\n Mean  %f [%f, %f]",eps_mean/run_max,eps_min,eps_max);
        mean_eval=mean_eval/run_max;
        printf("\n Mean eval %f",mean_eval);

        goto the_end;
    }

    //error_end:
    printf("\n Sorry, I give up");

the_end:
    return EXIT_SUCCESS;
}