Exemple #1
0
void move_to_floor(int current_floorr, Floors dest){
	while(SensorValue[shaft_1] != convert_Floor_to_shaft(dest)){
		if(check_direction(current_floorr, convert_Floor_to_int(dest)) > 0)
			move_motors(15);

		else if(check_direction(current_floorr, convert_Floor_to_int(dest)) < 0)
			move_motors(-1 * 20);
	}

	if(SensorValue[shaft_1] == dest)
		move_motors(0);

	//SensorValue[shaft_1] = destination;
}
Exemple #2
0
int output_direction(int channel, direction_t direction) {
	check_channel(channel);
	check_direction(direction);
	if (direction == DIR_HALT) {
		if (channel == 0) {
			gpio_clear(CTRL_PORT, M0INa);
			gpio_clear(CTRL_PORT, M0INb);
		} else {
			gpio_clear(CTRL_PORT, M1INa);
			gpio_clear(CTRL_PORT, M1INb);
		}
	} else if (direction == DIR_FWD) {
		if (channel == 0) {
			gpio_set(CTRL_PORT, M0INa);
			gpio_clear(CTRL_PORT, M0INb);
		} else {
			gpio_set(CTRL_PORT, M1INa);
			gpio_clear(CTRL_PORT, M1INb);
		}
	} else if (direction == DIR_REV) {
		if (channel == 0) {
			gpio_clear(CTRL_PORT, M0INa);
			gpio_set(CTRL_PORT, M0INb);
		} else {
			gpio_clear(CTRL_PORT, M1INa);
			gpio_set(CTRL_PORT, M1INb);
		}
	}
	return 0;
}
/*****************************************************************
 Checks to see if passed token has a valid move, and if it is it
 fills in the tokens for the move.
 
 Parameters:
    char: either 'B' or 'W', token of player who made move;
    int: the row num of move;
    int: the column num of move;
    int: size of the board;
    char**: 2d array of the board;

 Returns:
    0: if move is invalid;
    < 1: if move is valid, number indicates num of valid directions;
 *****************************************************************/
int make_move (char token, int row, int col,
               int size, char board[][size]) {
    
    int move_num = 0;
    //travel clockwise around the location
    
    //Check north
    if (check_direction(token, row, col, 0, -1, size, board) == 1) {
        fill_direction(token, row, col, 0, -1, size, board);
        move_num++;
    }
    
    //Check northeast
    if (check_direction(token, row, col, 1, -1, size, board) == 1) {
        fill_direction(token, row, col, 1, -1, size, board);
        move_num++;
    }
    
    //Check east
    if (check_direction(token, row, col, 1, 0, size, board) == 1) {
        fill_direction(token, row, col, 1, 0, size, board);
        move_num++;
    }
    
    //Check southeast
    if (check_direction(token, row, col, 1, 1, size, board) == 1) {
        fill_direction(token, row, col, 1, 1, size, board);
        move_num++;
    }
    
    //Check south
    if (check_direction(token, row, col, 0, 1, size, board) == 1) {
        fill_direction(token, row, col, 0, 1, size, board);
        move_num++;
    }
    
    //Check southwest
    if (check_direction(token, row, col, -1, 1, size, board) == 1) {
        fill_direction(token, row, col, -1, 1, size, board);
        move_num++;
    }
    
    //Check west
    if (check_direction(token, row, col, -1, 0, size, board) == 1) {
        fill_direction(token, row, col, -1, 0, size, board);
        move_num++;
    }
    
    //Check northwest
    if (check_direction(token, row, col, -1, -1, size, board) == 1) {
        fill_direction(token, row, col, -1, -1, size, board);
        move_num++;
    }
    
    return move_num;
}
 // NAME set_language()
 // DESCRIPTION sets up language direction which should be used for further
 // translations
 bool set_language(const std::string& from, const std::string& to) {
     if (!check_direction(from, to)) {
         return false;
     }
     else {
         from_to = std::make_pair(from, to);
         return true;
     }
 }
Exemple #5
0
void
selecting (void)
{
	struct unit *up;

	if (mousebutton[1]) {
		if (selectbox.drawing == 0) {
			selectbox.drawing = 1;
			selectbox.x1 = mouse_x;
			selectbox.y1 = mouse_y;
			selectbox.x2 = mouse_x;
			selectbox.y2 = mouse_y;
			check_direction (&selectbox);
		} else {
			selectbox.x2 = mouse_x;
			selectbox.y2 = mouse_y;
			check_direction (&selectbox);
		}
		for (up = first_unit; up; up = up->next) {
			switch (selectbox.direction) {
			case 1:
				select_check (selectbox.x2, selectbox.x1,
					      selectbox.y2, selectbox.y1);
				break;
			case 2:
				select_check (selectbox.x1, selectbox.x2,
					      selectbox.y2, selectbox.y1);
				break;
			case 3:
				select_check (selectbox.x2, selectbox.x1,
					      selectbox.y1, selectbox.y2);
				break;
			case 4:
				select_check (selectbox.x1, selectbox.x2,
					      selectbox.y1, selectbox.y2);
				break;
			}
		}
	}
	if (mousebutton[1] == 0) {
		selectbox.drawing = 0;
	}
}
/*****************************************************************
 Checks to see if passed token has a valid move at that location.
 
 Parameters:
    char: either 'B' or 'W', token of player to check for;
    int: the row num of move;
    int: the column num of move;
    int: size of the board;
    char**: 2d array of the board;
 
 Returns:
    0: if move is invalid;
    < 1: if move is valid, number indicates num of valid directions;
 *****************************************************************/
int check_location(char token, int row, int col,
                   int size, char board[][size]) {
    
    int move_num = 0;
    
    //Check north
    if (check_direction(token, row, col, 0, -1, size, board) == 1) {
        move_num++;
    }
    
    //Check northeast
    if (check_direction(token, row, col, 1, -1, size, board) == 1) {
        move_num++;
    }
    
    //Check east
    if (check_direction(token, row, col, 1, 0, size, board) == 1) {
        move_num++;
    }
    
    //Check southeast
    if (check_direction(token, row, col, 1, 1, size, board) == 1) {
        move_num++;
    }
    
    //Check south
    if (check_direction(token, row, col, 0, 1, size, board) == 1) {
        move_num++;
    }
    
    //Check southwest
    if (check_direction(token, row, col, -1, 1, size, board) == 1) {
        move_num++;
    }
    
    //Check west
    if (check_direction(token, row, col, -1, 0, size, board) == 1) {
        move_num++;
    }
    
    //Check northwest
    if (check_direction(token, row, col, -1, -1, size, board) == 1) {
        move_num++;
    }
    
    return move_num;
}
int L1rlrOwlqnAlgorithm::train()
{
    // the main difference between this implementation and OWLQN paper, is we calc gradient after value
    // this is because the calc procedure between value & gradient is similar, we canc calc gradient after
    // value with a little calc cost
    double alpha = 0.0, backoff = 0.0;  
    _iter = 0;

    // init _g_xk
    lr_func_val(_x_k1, _f_xk1, _g_xk1);
    _f_xk = _f_xk1;
    for (uint32_t i = 0; i < _data->feature_num(); ++i)
    {
        _g_xk[i] = _g_xk1[i];
    }

    for (_iter = 0; _iter < _iters; ++_iter )
    {
        // 1.0 check convergence
        if (check_avg_convergence())
        {
            std::cout << "OWLQN convergenced!" << std::endl;
            break;
        }
        // 2.0 get LBFGS Newton Direction
        if (l1_direction() != 0)
        {
            std::cout << "Calc L1 Direction failed!" << std::endl;
            break;
        }
        // 2.1 tow-loop calc p_k = - H_k g_xk
        if (newton_direction() != 0)
        {
            std::cout << "Calc LBFGS Newton Direction failed!" << std::endl;
            break;
        }

        // 2.2 Orthant Projection
        if (orthant_projection() != 0)
        {
            std::cout << "Orthant projection failed!" << std::endl;
            break;
        }
        // 2.3 check search direction
        double dir_grad_product = check_direction();
        if (dir_grad_product >= 0)
        {
            std::cout << "Check Search Direction failed! Please check gradient & search direction!" << std::endl;
            break;
        }

        // 3.0 line search for step length alpha_k
        // 3.1 set alpha & backoff init parameter
        alpha = 1.0;
        backoff = 0.5;
        if ( 0 == _iter )
        {
            alpha = 1 / _dir.l2_norm();
            backoff = 0.1;
        }

        for (uint32_t line_search_idx = 0; line_search_idx < _line_search_steps; ++line_search_idx)
        {
           // 3.2 get Next Point: x_k1
            for (uint32_t i = 0; i < _data->feature_num(); ++i)
            {
                _x_k1[i] = _x_k[i] + _dir[i] * alpha;
                // 3.2.5 Point Orthant Constraint
                if (_x_k1[i] * _x_k[i] < -1e-15)
                {
                    _x_k1[i] = 0.0;
                }
            }

            // 3.3 calc f_xk1, g_xk1
            if (lr_func_val(_x_k1, _f_xk1, _g_xk1) != 0)
            {
                std::cout << "calc function value failed!" << std::endl;
                break;
            }
            // 3.4 check line search quit
            printf("check line search quit\n _f_xk1:[%lf] _f_xk[%lf] orig [%lf] alpha [%lf]\n", _f_xk1, _f_xk, dir_grad_product, alpha); 
            if (_f_xk1 <= _f_xk + 1e-4 * dir_grad_product * alpha)
            {
                break;
            }
            alpha *= backoff;
        }
        // 4.0 shift k & k1
        if (shift_xk_xk1() != 0)
        {
            std::cout << "Shift xk xk1 state failed!" << std::endl;
            break;
        }
    } 
    std::cout << "OWLQN train complete!" << std::endl;
    return 0;
}
/**
* can_move(void)
*
* @brief Determine if player is able to move
* @param void
* @return void
*/
sbool can_move(void)
{
	uint32 d;
	sbool ismap = FALSE;

	if(player(myindex())->moving)
		return FALSE;

	if(tempdata()->castedspell){
		if(gettickcount() > player(myindex())->attacktimer + 1)
			tempdata()->castedspell = FALSE;
		else
			return FALSE;
	}

	d = player(myindex())->dir;

	switch(tempdata()->dir){
	case DIR_NONE: return FALSE;
	case DIR_UP:
		player(myindex())->dir = DIR_UP;

		if(player(myindex())->y <= 0){
			if(map()->up) ismap = TRUE;
			return FALSE;
		}
		break;
	case DIR_DOWN:
		player(myindex())->dir = DIR_DOWN;

		if(player(myindex())->y >= MAX_MAPY - 1){
			if(map()->down) ismap = TRUE;
			return FALSE;
		}
		break;
	case DIR_LEFT:
		player(myindex())->dir = DIR_LEFT;

		if(player(myindex())->x <= 0){
			if(map()->left) ismap = TRUE;
			return FALSE;
		}
		break;

	case DIR_RIGHT:
		player(myindex())->dir = DIR_RIGHT;

		if(player(myindex())->x >= MAX_MAPX - 1){
			if(map()->right) ismap = TRUE;
			return FALSE;
		}
		break;
	}

	if(ismap){
		//map_editor_leave_map();
		request_new_map();
		tempdata()->gettingmap = TRUE;
		tempdata()->canmovenow = FALSE;
		return FALSE;
	}

	if(check_direction(player(myindex())->dir)){
		if(d != player(myindex())->dir)
			send_player_dir();

		return FALSE;
	}

	return TRUE;
}