Ejemplo n.º 1
0
	basic_spaceship::basic_spaceship(phys_agent_specification const& spec, phys_system* system):
		simple_rigid_body(spec, system),
		m_t_cfg(new thruster_config< WorldDimensionality::dim2D >(thruster_presets::square_complete())),
		m_t_system(m_t_cfg)
	{
		m_half_width = spec.spec_acc["size"].as< double >() * 0.5;
		m_mass = spec.spec_acc["mass"].as< double >();
		m_rotational_inertia = spec.spec_acc["rotational_inertia"].as< double >();
		m_thruster_strength = spec.spec_acc["thruster_strength"].as< double >();
		m_sensor_range = spec.spec_acc["sensor_range"].as< double >();

		auto world = system->get_world();

		b2BodyDef bd;
		bd.type = b2_dynamicBody;
		bd.position.Set(0.0f, 0.0f);
		bd.angle = 0.0f;
		auto body = world->CreateBody(&bd);

		b2PolygonShape shape;
		shape.SetAsBox(m_half_width, m_half_width);

		b2FixtureDef fd;
		fd.shape = &shape;
		fd.density = 1.0f;
		body->CreateFixture(&fd);

		// Override mass and rotational inertia
		// TODO: should be optional parameters, and if unspecified, left as auto b2d defaults
		b2MassData md;
		md.center = b2Vec2(0, 0);
		md.mass = m_mass;
		md.I = m_rotational_inertia;
		body->SetMassData(&md);

		set_body(body);

/*		phys_sensor_defn sdef(SensorType::Fan);
		sdef.body = body;
		sdef.pos = b2Vec2(0, 0);
		sdef.orientation = 0;
		sdef.range = m_sensor_range;
		sdef.type_specific = fan_sensor_data{ b2_pi / 3 };
		m_front_sensor = create_sensor(sdef);
		sdef.orientation = b2_pi;
		m_rear_sensor = create_sensor(sdef);
		sdef.orientation = b2_pi / 2;
		m_left_sensor = create_sensor(sdef);
		sdef.orientation = -b2_pi / 2;
		m_right_sensor = create_sensor(sdef);
*/

		phys_sensor_defn sdef(SensorType::Directional);
		sdef.body = body;
		sdef.pos = b2Vec2(0, 0);
		sdef.orientation = 0;
		sdef.range = m_sensor_range;
		sdef.type_specific = directional_sensor_data{};
		m_front_sensor = create_sensor(sdef);
		sdef.orientation = b2_pi;
		m_rear_sensor = create_sensor(sdef);
		sdef.orientation = b2_pi / 2;
		m_left_sensor = create_sensor(sdef);
		sdef.orientation = -b2_pi / 2;
		m_right_sensor = create_sensor(sdef);

		m_collisions = 0;
		m_damage = 0.0;
	}
int main(int argc, char*argv[])
{
	char *conf_file_name = NULL;
	FILE *conf_file_pointer = NULL;
	char line[LINE_MAX] = {'\0'};
	char *tokens[TOKEN_MAX] = {NULL};
	int count = 0;
	sensor_create_params sensor_device = {NULL, NULL, NULL};
	sensor_handle sensor = NULL;

	LOG_DEBUG(("DEBUG: Number of arguments are: %d\n", argc));

	if(argc<3)
	{
		LOG_ERROR(("ERROR: Please provide configuration file name(s)\n"));
		return (0);
	}

	conf_file_name = argv[1];

	LOG_DEBUG(("DEBUG: Configuration File Name is %s\n", conf_file_name));

	conf_file_pointer = fopen(conf_file_name, "r");
	if(!conf_file_pointer)
	{
		LOG_ERROR(("ERROR: Error in opening configuration file\n"));
		return (0);
	}

	/* Read line */
	if(fgets(line, LINE_MAX, conf_file_pointer) == NULL)
	{
		LOG_DEBUG(("DEBUG: Cleanup and return\n"));
		fclose(conf_file_pointer);
		LOG_ERROR(("ERROR: Wrong configuration file\n"));
		return (0);
	}
	str_tokenize(line, ":\n\r", tokens, &count);
	if(count<2)
	{
		LOG_ERROR(("ERROR: Wrong configuration file\n"));
		fclose(conf_file_pointer);
		return (0);
	}

	str_copy(&sensor_device.gateway_ip_address, tokens[0]);
	str_copy(&sensor_device.gateway_port_no, tokens[1]);
	LOG_DEBUG(("DEBUG: Gateway IP Address: %s\n", sensor_device.gateway_ip_address));
	LOG_DEBUG(("DEBUG: Gateway Port No: %s\n", sensor_device.gateway_port_no));

	/* Read line */
	if (fgets(line, LINE_MAX, conf_file_pointer) == NULL)
	{
		LOG_DEBUG(("DEBUG: Cleanup and return\n"));
		fclose(conf_file_pointer);
		LOG_ERROR(("ERROR: Wrong configuration file\n"));
		return (0);
	}
	str_tokenize(line, ":\r\n", tokens, &count);
	if (count < 4)
	{
		LOG_ERROR(("ERROR: Wrong configuration file\n"));
		fclose(conf_file_pointer);
		return (0);
	}
	if(strcmp("sensor", tokens[0])!=0)
	{
		LOG_ERROR(("ERROR: Wrong configuration file\n"));
		fclose(conf_file_pointer);
		return (0);
	}
	str_copy(&sensor_device.sensor_ip_address, tokens[1]);
	str_copy(&sensor_device.sensor_port_no, tokens[2]);
	str_copy(&sensor_device.sensor_area_id, tokens[3]);

	sensor_device.sensor_value_file_name = argv[2];

	LOG_DEBUG(("DEBUG: sensor ip_address: %s\n", sensor_device.sensor_ip_address));
	LOG_DEBUG(("DEBUG: sensor port_no: %s\n", sensor_device.sensor_port_no));
	LOG_DEBUG(("DEBUG: sensor area_id: %s\n", sensor_device.sensor_area_id));

	int return_value = create_sensor(&sensor, &sensor_device);
	if(return_value != E_SUCCESS)
	{
		LOG_ERROR(("ERROR: Unable to create sensor\n"));
		free(sensor_device.gateway_ip_address);
		free(sensor_device.gateway_port_no);
		free(sensor_device.sensor_area_id);
		free(sensor_device.sensor_ip_address);
		free(sensor_device.sensor_port_no);
		fclose(conf_file_pointer);
		return (0);
	}

	LOG_ERROR(("Sensor started successfully\n"));

	char choice;

	printf("Press enter to exit\n");
	scanf("%c", &choice);
	delete_sensor(sensor);

	free(sensor_device.gateway_ip_address);
	free(sensor_device.gateway_port_no);
	free(sensor_device.sensor_area_id);
	free(sensor_device.sensor_ip_address);
	free(sensor_device.sensor_port_no);
	fclose(conf_file_pointer);
	logger_close();
	return (0);
}
Ejemplo n.º 3
0
int main() {
    int k = 1, err = 10, iter = 0;
    float ratio = 0;
    int A[4][5] = {
                { 1, 1, 1, 1, 1},
                { 1, 0, 0, 0, 1},
                { 1, 1, 1, 1, 1},
                { 1, 0, 0, 0, 1} };
    int C[4][5] = {
                { 1, 1, 1, 1, 1},
                { 1, 0, 0, 0, 0},
                { 1, 0, 0, 0, 0},
                { 1, 1, 1, 1, 1} };
    int L[4][5];
    int last_used_matrix[4][5] = {
                { -1, -1, -1, -1, -1},
                { -1, -1, -1, -1, -1},
                { -1, -1, -1, -1, -1},
                { -1, -1, -1, -1, -1} };
    t_neuron d_neuron = { .potential = 0, .threshold = 0.5, .output = 0 };
    t_synapse synapses[20];

    // Création du réseau
    for (int i = 0; i < 20; i++) {
        synapses[i].source = create_sensor();
        synapses[i].target = &d_neuron;
        synapses[i].weight = 0;
    }


    // Phase d'apprentissage
    do {
        feed_neural_network(&synapses, A);
        learning_phase(&synapses, EPSILON, 1, 0);

        feed_neural_network(&synapses, C);
        learning_phase(&synapses, EPSILON, 0, 1);
        
        feed_neural_network(&synapses, A);
        calculate_potential(&synapses, &d_neuron);
        if (d_neuron.output != 1)
            err++;

        feed_neural_network(&synapses, C);
        calculate_potential(&synapses, &d_neuron);
        if (d_neuron.output != 0)
            err++;

        iter++;
        ratio = (float) iter / (float) err;
        
        //printf("iter: %d && err: %d\n", iter, err);
        //printf("ratio:%f\n", ratio);
    } while ( ratio < 0.8);


    // Phase de test pour A
    while (k < 20) {
        int i = 0;
        int success = 0;
        while (i < 100000) {
            while (true) {
                // Generer copie d'une lettre
                lcpy(&L, &A);
                // Generer du bruit sur cette copie
                generate_noise(&L, k);
                //print_letter(L);
                //printf("\n");
                if (!matrices_are_equal(L, last_used_matrix)) {
                    lcpy(&last_used_matrix, &L);
                    break;
                }
            }

            feed_neural_network(&synapses, L);
            calculate_potential(&synapses, &d_neuron);
            if (d_neuron.output == 1)
                success++;
            
            i++;
        }
        success /= 1000;
        printf("%d%% de succes sur %d essais avec %d%% de bruit.\n", success, i, k*5);
        k++;
    }

    // Phase de test pour C
    k = 1;
    while (k < 20) {
        int i = 0;
        int success = 0;
        while (i < 100000) {
            while (true) {
                // Generer copie d'une lettre
                lcpy(&L, &C);
                // Generer du bruit sur cette copie
                generate_noise(&L, k);
                if (!matrices_are_equal(L, last_used_matrix)) {
                    lcpy(&last_used_matrix, &L);
                    break;
                }
            }

            feed_neural_network(&synapses, L);
            calculate_potential(&synapses, &d_neuron);
            if (d_neuron.output == 0)
                success++;
            
            i++;
        }
        success /= 1000;
        printf("%d%% de succes sur %d essais avec %d%% de bruit.\n", success, i, k*5);
        k++;
    }
}