コード例 #1
0
bitwise_weight_value BitwiseWeight::guided_mutation( bitwise_weight_value elite_value, bitwise_weight_value new_value, RandomNumberGenerator& rng )
{
	int i;
	bitwise_weight_value weight;
	weight.integer = 0;
	int elite = elite_value.integer, newv = new_value.integer;
	bool is_neg;
	if( rng.getRandom() > config->gm_prob_of_mutation )
		is_neg = elite < 0;
	else 
		is_neg = newv < 0;
	if( elite < 0 ) elite *= -1;
	if( newv < 0 ) newv *= -1;
	int num = 0x1;

	for( i = 0; i < number_of_elements - 1; i++ )
	{
		bool gm = rng.getRandom() < config->gm_prob_of_mutation;
		if(		( gm && elite % 2 )
			||  (!gm && newv % 2 ) )
			weight.integer += num;
		num <<= 1;
		elite >>= 1;
		newv >>= 1;
	}
	if( is_neg )
		weight.integer *= -1;
	weight.decimal = get_decimal_value( weight.integer );
	return weight;
}
コード例 #2
0
bitwise_weight_value BitwiseWeight::get_weight( RandomNumberGenerator& rng )
{
    int i;
    bitwise_weight_value weight;
    weight.decimal = 0.;
    weight.integer = 0;
    int max_size = 2;
    int num = 0x1;
    for( i = 0; i < number_of_elements - 1; i++ )
    {
        if( elements[i].get_value( rng ) == 1 )
            weight.integer += num;
        num <<= 1;
        max_size *= 2;
    }
    max_size--;
    if( elements[i].get_value( rng ) == 1 )
        weight.integer *= -1;
	weight.decimal = get_decimal_value( weight.integer );
    return weight;
}
コード例 #3
0
ファイル: ttycmd.c プロジェクト: awakecoding/ttycmd
void* CmdThreadProc(void* data)
{
    char* p;
    uint8 state;
    uint8 val = 0;
    uint8 cmd = 0;
    char* cmd_str;
    char* val_str;
    char input[128];

    while (1)
    {
        printf("cmd: ");
        scanf("%s", input);

        p = strchr(input, ':');
        val_str = cmd_str = NULL;

        if (p != NULL)
        {
            val_str = p + 1;
            input[(p - input)] = '\0';
        }

        cmd_str = input;

        cmd = get_command_id(cmd_str);

        if (cmd == CMD_UNKNOWN)
        {
            printf("unknown command!\n");
            continue;
        }

        switch (cmd)
        {
        case CMD_TEENSY_MODE:
            state = get_state_id(val_str);

            if (state == STATE_UNKNOWN)
            {
                printf("unknown mode!\n");
                continue;
            }

            send_command(tty_fd, cmd, state);
            break;

        case CMD_CHANGE_STATE:
            state = get_state_id(val_str);

            if (state == STATE_UNKNOWN)
            {
                printf("unknown state!\n");
                continue;
            }

            send_command(tty_fd, cmd, state);
            break;

        case CMD_HARD_TURN:
            val = get_turn_id(val_str);

            if (val == TURN_UNKNOWN)
            {
                printf("unknown turn!\n");
                continue;
            }

            send_command(tty_fd, cmd, val);
            break;

        case CMD_SOFT_TURN:
            val = get_turn_id(val_str);

            if (val == TURN_UNKNOWN)
            {
                printf("unknown turn!\n");
                continue;
            }

            send_command(tty_fd, cmd, val);
            break;

        case CMD_SET_DIRECTION:
            val = get_move_id(val_str);

            if (val == MOVE_UNKNOWN)
            {
                printf("unknown move direction!\n");
                continue;
            }

            send_command(tty_fd, cmd, val);
            break;

        case CMD_DIST_CENTER:
            val = get_decimal_value(val_str);
            send_command(tty_fd, cmd, val);
            break;

        case CMD_DIST_LEFT:
            val = get_decimal_value(val_str);
            send_command(tty_fd, cmd, val);
            break;

        case CMD_DIST_RIGHT:
            val = get_decimal_value(val_str);
            send_command(tty_fd, cmd, val);
            break;

        case CMD_SPEED:
            val = get_decimal_value(val_str);
            send_command(tty_fd, cmd, val);
            break;

        case CMD_HELP:
            val = get_command_id(val_str);

            if (val == CMD_UNKNOWN)
            {
                printf("command syntax: <command>:<value>\n");
                print_command_list();
            }
            else
            {
                switch (val)
                {
                case CMD_CHANGE_STATE:
                    printf("state:<state>, where <state> is one of the following:\n");
                    printf("nothing, basic, orders, dance.\n");
                    break;

                default:
                    printf("command syntax: <command>:<value>\n");
                    print_command_list();
                    break;
                }
            }

            break;

        case CMD_QUIT:
            exit(0);
            break;
        }
    }

    pthread_exit(NULL);
}