// The score is calculate dbetween a ground truth with dilation 6 and 
	// the very first frame of cam 1 contoured.
	float calculate_score()
	{
		const Mat m = imread("data\\cam1\\ground_truth.png");
		const Mat img = imread("data\\cam1\\first_frame_threshed.png");
		float score = get_difference(img, m);
		return score;
	}
Example #2
0
File: dpa.c Project: Radlbeck/cc
// return the key byte at location bytenum
int	dpa_aes(int bytenum)
{
	int	i_pt, i, nbits;
	int	kv;

	// Initialization
	for (i = 0; i < NUM_KEYS; i ++) {
	    for (nbits = MIN_KEYBITS; nbits < MAX_KEYBITS; nbits ++) {
		PT_zero(pts0[i][nbits], PT_LENGTH);
		PT_zero(pts1[i][nbits], PT_LENGTH);
		num_pts0[i][nbits] = num_pts1[i][nbits] = 0;
	    }
	    PT_zero(pt_delta[i], PT_LENGTH);
	}

	// Put your code here
	int count_S0, count_S1, j;
	
	// rotate through key guess NUM_KEYS
	for(i = 0; i < NUM_KEYS; i++){
		for (nbits = MIN_KEYBITS; nbits < MAX_KEYBITS; nbits++) {
			// reset counters
			count_S0 = count_S1 = 0;

			// look at bit bytenum refering to 10th round key byte (0 -> 15)
			// rotate through power traces
			for(j = 0; j < NUM_PTS; j++){
				// cyper_i reg -> XOR key guess -> shift rows -> inv SBox -> get state_i reg
				// compair state_i reg and cyper_i reg bits if a change exists P_i -> S1 otherwise S0
				int diff = get_difference(cipher[j], bytenum, i);			
				int mask = (1 << nbits);
	
				if(diff & mask){
					count_S1++;
	 				PT_add(NULL, &pts1[i][nbits][0], &pts[j][0], PT_LENGTH);
				}else{
					count_S0++;
	 				PT_add(NULL, &pts0[i][nbits][0], &pts[j][0], PT_LENGTH); 				
				}			
			}
			// average
			PT_scale(NULL, &pts0[i][nbits][0], (1.0 / count_S0), PT_LENGTH);
			PT_scale(NULL, &pts1[i][nbits][0], (1.0 / count_S1), PT_LENGTH);

			// find difference abs(S0 - S1)
			// store diff accosiated to i
			double pt_temp[PT_LENGTH];
			PT_diff(&pt_temp[0], &pts0[i][nbits][0], &pts1[i][nbits][0], PT_LENGTH);

			// sum all of the deltas for each key bit
			PT_add(NULL, &pt_delta[i][0], &pt_temp[0], PT_LENGTH);
		}
		pt_delta_max[i] = max_dp(&pt_delta[i][0], PT_LENGTH, &pt_delta_max_idx[i]);

	}
	// get the max of all stored differrences and return accosiated i(key value)
	max_dp(&pt_delta_max[0], NUM_KEYS, &kv);

	return kv;
}
void controller_initialize(void)
{
    auto_set_target_position_by_value(0.5);
    auto_print_target_position_list();

    last_diff = get_difference();
    last_target = auto_get_target_position();
}
Example #4
0
int main(int argc, char *argv[]) {

    // Configuration
    string path;
    if(!check_flags(argc,argv,path)){
        return 1;
    }
    read_input_file(path);

    // Algorithm
    //int iteration = 0;
    //int loop;
    int watch_best;
    int best_fitness;

    solutions_generation(sols);
    fitness_calculation(sols);

    solutions_improvement(sols);
    refset_build();

    save_best_solution(refset);
    refset_tmp = refset;

    do {
        // number of new solutions in the RefSet
        loop = 0;
        while (get_difference(refset_tmp,refset) != 0)
        {
            refset_tmp = refset;
            solutions_combination();
            fitness_calculation(new_set);
            new_set = solutions_improvement2(new_set);
            //solutions_improvement(new_set);
            refset_modification(new_set);
            loop++;
        }
        save_best_solution(refset);

        /* A: If we find the optimal, stop */
        if(best.p == 100){
            iteration = max_iter;
        }
        /* A: end */

        refset_rebuild();
        iteration++;
        print_one_solution(best);
        //print_strip(best);
        //print_tikz(best);
    } while (iteration < max_iter);
    //print_one_solution(best);
    //print_strip(best);
    print_tikz(best);

    return 0;
}
Example #5
0
// -------------------------------------------------------------------------
// footprint of combat object, marked by cell as to whether a cell is used.
// -------------------------------------------------------------------------
t_threat_footprint::t_threat_footprint( int size, t_direction direction, int arc )
{
	int		     row;
	int		     column;
	int		     delta_row;
	int		     delta_column;
	int		     delta_row_squared;
	int		     distance;
	int*	     marked;
	t_direction* angles;
	int          size_squared = size * size;
	bool         in_range;
	int          angle;
	int          base_angle;
	t_direction  bearing;

	m_size = size;
	m_arc = arc;
	m_direction = direction;
	base_angle = ::get_angle( direction );
	m_angles.resize( size_squared );
	m_marked.resize( size_squared );
	marked = ((m_marked.size() > 0) ? &(*m_marked.begin()) : NULL);
	angles = ((m_angles.size() > 0) ? &(*m_angles.begin()) : NULL);
	delta_row = 1 - size;
	for (row = 0; row < size; row++)
	{
		delta_row_squared = delta_row * delta_row;
		delta_column = 1 - size;
		for (column = 0; column < size; column++)
		{
			distance = delta_column * delta_column + delta_row_squared;
			in_range = (distance <= size_squared);
			if (in_range && arc < 360)
			{
				angle = ::get_angle( t_map_point_2d( delta_row, delta_column ));
				bearing = ::get_direction( angle );
				angle = get_difference( angle, base_angle );
				if (angle * 2 > arc)
					in_range = false;
			}
			*marked++ = in_range;
			if (in_range)
				*angles++ = bearing;
			else
				*angles++ = k_direction_count;
			delta_column += 2;
		}
		delta_row += 2;
	}
}
void controller_simulation_step(void)
{
    if (last_target != auto_get_target_position()) {
        last_diff = get_difference();
        last_target = auto_get_target_position();
    }

    double current_diff = get_difference();
    indicator_set(difference_indicator_id, current_diff);
    double diff_diff = current_diff - last_diff;

    roller_set_motor_torque(current_diff*3 + diff_diff*1000);

    last_diff = current_diff;

    static int count = 0;
    if (count > 100) {
        printf("%lf %lf %lf\n", auto_get_target_position(), current_diff, roller_get_encoder_value());
        count = 0;
    } else {
        count ++;
    }
}
Example #7
0
//--------------------------------------------------------------------
// Determine if a given unit would interfere with another unit, given
// certain changes to the battlefield.
// interferer: Creature that might interfere
// interferee: Creature that might be interfered with
// interferer_facing: Assumed angle interferer will face
// post_attack: If true, ignore blind spells which would be 
//   canceled by any attack
// Static function
//--------------------------------------------------------------------
double t_combat_ai::would_interfere(const t_combat_creature &interferer, const t_combat_creature &interferee, int interferer_facing, bool post_attack)
{
	t_map_point_2d						   point = interferee.get_cell_position();
	t_map_point_2d						   center;
	int									   half_footprint_size;
	int									   distance;
	int									   attack_distance;
	int									   angle;
	
	// creatures in arrow towers can always shoot.
	if (interferee.is_in_tower())
		return false; 

	half_footprint_size = interferee.get_half_footprint_size();
	center = point;
	center <<= k_battlefield_subcell_shift;
	center.row += half_footprint_size;
	center.column += interferee.get_half_footprint_size();

	if (interferer.is_friendly( interferee ))
		return false;

	if (   !interferer.is_alive() 
		|| interferer.is_active( k_spell_frozen )
	    || interferer.is_active( k_spell_stun ))
		return false;

	if (!post_attack && interferer.is_active( k_spell_blind ))
		return false;



	distance = interferer.get_edge_subcell_distance( center );
	attack_distance = (interferer.get_attack_range() + 1) << k_battlefield_subcell_shift;
	if (distance > attack_distance + half_footprint_size)
		return false;

	// check angle
	angle = get_angle( interferer.get_footprint_center(), center );
	return (get_difference( angle, interferer_facing ) << 1) <= interferer.get_arc_threat();

}
Example #8
0
void Loader::reload()
{
    boost::unique_lock<boost::mutex> lock( m_mutex );

    if ( ! boost::filesystem::exists( m_file_name ) )
    {
        LOG << "can not find " << m_file_name;
        m_string_hash_set.clear();
        m_hash_2_string_map.clear();
        m_last_write_time = 0;
        return;
    }

    std::time_t t = boost::filesystem::last_write_time( m_file_name );

    if ( t == m_last_write_time )
    {
        return;
    }

    LOG_DEBUG << "last-writ-time: " << Utility::string_from_time_t( m_last_write_time ) << ", new last-write-time: " << Utility::string_from_time_t( t );

    std::wstring fise_string = Utility::wstring_from_file( m_file_name.c_str() );
    std::vector<std::wstring> lines = Utility::split_string_to_lines( fise_string );

    if ( lines.empty() )
    {
        LOG << "cannot open file: " << m_file_name;
        return;
    }

    std::set<size_t> string_hash_set;
    std::map<size_t, std::wstring> hash_2_string_map;

    for ( size_t i = 0; i < lines.size(); ++i )
    {
        std::wstring& s = lines[i];

        boost::trim(s);
        boost::replace_all( s, L"\\n", L"\n" );
        boost::replace_all( s, L"\\t", L"\t" );

        if ( s.empty() || L'#' == s[0] )
        {
            continue;
        }

        size_t hash = m_hash_function( s );

        if ( hash != 0 )
        {
            string_hash_set.insert( hash );
            hash_2_string_map[hash] = s;
        }
    }

    if ( m_string_hash_set != string_hash_set )
    {
        LOG_DEBUG << "old-size = " << m_string_hash_set.size() << ", new-size = " << string_hash_set.size();

        if ( m_last_write_time != 0 )
        {
            LOG_DEBUG << get_difference( m_string_hash_set, m_hash_2_string_map, string_hash_set, hash_2_string_map );
        }

        m_string_hash_set = string_hash_set;
        m_hash_2_string_map = hash_2_string_map;
    }

    m_last_write_time = t;
}
Example #9
0
// -----------------------------------------------------------------
// sparks combat spell
// -----------------------------------------------------------------
t_combat_creature_list get_sparks_targets( t_combat_creature* caster, t_direction direction )
{
	int const                 k_arc = 180;
	int						  attack_distance = 3;
	t_map_point_2d			  position		  = caster->get_cell_position();
	t_map_point_2d			  center		  = caster->get_footprint_center();
	int						  footprint_size  = caster->get_footprint_size() 
		                                        + attack_distance * 2;
	t_combat_footprint const& footprint		  = get_combat_footprint( footprint_size );
	int                       angle;
	t_map_point_2d            offset;
	t_map_point_2d			  point;
	t_combat_creature*        candidate;
	int		                  base_angle;
	t_battlefield&            battlefield = caster->get_battlefield();
	t_combat_creature_list    targets;

	base_angle = get_angle( direction );
	position.row	-= attack_distance;
	position.column -= attack_distance;
	for (offset.row = 0; offset.row < footprint_size; offset.row++)
	{
		for (offset.column = 0; offset.column < footprint_size; offset.column++)
		{
			// check if point is in attack area
			if (!footprint[offset.row][offset.column])
				continue;
			// check if point is a valid square
			point = position + offset;
			if (!battlefield.is_valid( point ))
				continue;
			// get creature in square
			candidate = battlefield.get_creature( point );
			if (candidate == 0)
				continue;
			// check if creature is friendly
			if (candidate == caster)
				continue;
			if (!can_affect( *caster, k_spell_burning_hands, *candidate ))
				continue;

			// check if creature has already been attacked
			t_combat_creature_iterator index = targets.begin();
			t_combat_creature_iterator end   = targets.end();

			for (; index != end; index++)
			{
				if (index->get() == candidate)
					break;
			}
			if (index != end)
				continue;

			// check if square is in attack arc
			point = (point << k_battlefield_subcell_shift) + k_battlefield_half_cell;
			angle = get_angle( center, point );
			angle = get_difference( angle, base_angle );
			if (angle * 2 > k_arc)
				continue;
			targets.push_back( candidate );
		}
	}
	return targets;
}
static int gator_events_mali_read(int **buffer) {
    int cnt, len = 0;

    if (smp_processor_id()) return 0;

    // Read the L2 C0 and C1 here.
    if (counter_enabled[COUNTER_L2_C0] || counter_enabled[COUNTER_L2_C1] ) {
        u32 src0 = 0;
        u32 val0 = 0;
        u32 src1 = 0;
        u32 val1 = 0;

        // Poke the driver to get the counter values
        if (_mali_profiling_get_counters_function_pointer){
            _mali_profiling_get_counters_function_pointer(&src0, &val0, &src1, &val1);
        }

        if (counter_enabled[COUNTER_L2_C0])
        {
            // Calculate and save src0's counter val0
            counter_dump[len++] = counter_key[COUNTER_L2_C0];
            counter_dump[len++] = get_difference(val0, counter_prev[COUNTER_L2_C0]);
        }

        if (counter_enabled[COUNTER_L2_C1])
        {
            // Calculate and save src1's counter val1
            counter_dump[len++] = counter_key[COUNTER_L2_C1];
            counter_dump[len++] = get_difference(val1, counter_prev[COUNTER_L2_C1]);
        }

        // Save the previous values for the counters.
        counter_prev[COUNTER_L2_C0] = val0;
        counter_prev[COUNTER_L2_C1] = val1;
    }

    // Process other (non-timeline) counters.
    for (cnt = COUNTER_VP_C0; cnt <= LAST_SW_COUNTER; cnt++) {
        if (counter_enabled[cnt]) {
            u32 value = 0;

            // Determine the current value of the counter.
            if( counter_address[cnt] != NULL && 0 ) {   // Never true!
                value = *counter_address[cnt];
            } else if (counter_data[cnt]!=0) {
                value = counter_data[cnt];
                counter_data[cnt] = 0;
            }

            // Send the counter value only if it differs from last time.
            if (value != counter_prev[cnt]) {
                counter_prev[cnt] = value;
                counter_dump[len++] = counter_key[cnt];
                counter_dump[len++] = value;
            }
        }
    }

    if (buffer) {
        *buffer = (int*) counter_dump;
    }

    return len;
}
void get_new_states(void){
//find all the new states and put them in the buffer
    
    //copy the current stuff into storage
    
    int8_t x=X_AXIS_LEN;
    while(x--){
        int8_t y=Y_AXIS_LEN;
        while(y--){
            if(get_new_pixel_state(fb, x, y)==1){
                state_storage[x] |= (1<<y);
            } else {
                state_storage[x] &= ~(1<<y);
            }
        }
    }
    //store the difference between the two generations in diff_val
    //to be used in finding when to reset.
    uint8_t diff_val= get_difference(state_storage,fb);
    
    if((diff_val <= 4)){
        //if diff_val is a low difference then increment it's counter
        low_diff_count++;
    }
    else if((diff_val<=8)){
        //if diff_val is a medium difference then increment that counter
        med_diff_count++;
    }
    else{
        //if neither, then decrement their counters to stay longer before reset
        if(low_diff_count > 0){
            low_diff_count--;
        }
        if(med_diff_count >0){
            med_diff_count--;
        }
    }
    #if DO_YOU_WANT_A_GENERATION_RESET_BUTTON==1
    #if DO_YOU_WANT_BUTTON_INT0==0
    //if you don't want to use INT0 for button
    //then this "if" statement will compile
    //which just checks the button pin's state whenever
    //this function runs
    if(bit_is_clear(BUTTON_PIN, BUTTON_BIT)){
        reset_grid();
    } 
    else 
    #endif
    #endif
    if(low_diff_count > LOW_DIFF_THRESHOLD){
    //if low_diff_count is above threshold, reset
        low_diff_count=0;
        reset_grid();
    }
    else if(med_diff_count > MED_DIFF_THRESHOLD){
    //if med_diff_count is above threshold, reset
        med_diff_count=0;
        reset_grid();
    }
    else{
    //if it is interesting enough so far then just add the new generation
    //to the framebuffer.
        for(x=0;x<X_AXIS_LEN;x++){
            //put the new values into the framebuffer
            fb[x] = state_storage[x];
        }
    }
}