Esempio n. 1
0
// stops the thread
bool Thread::stop () {


	ssi_msg (SSI_LOG_LEVEL_DEFAULT, "terminate%s'%s'", _single_execution ? " single execution " : " ", _name);

	// if not single execution signal run method to terminate

	if (!_single_execution) {
		Lock lock (_mutex);
		_is_active = false;
	}
	// give user the chance to terminate run ()
	terminate ();

	// wait until thread has terminated
	#if hasCXX11threads
	bool success = true;

	//evil thread slaying has to be adapted for windows
	/*
    pthread_t phandle=_handle->native_handle();
    
    
    std::thread
    ( [](unsigned int* _timeout, unsigned int* phandle)
		{
			pthread_t* phandle_mine=(pthread_t*)phandle;
			std::chrono::milliseconds dura(*_timeout);
			std::this_thread::sleep_for( dura  );
			ssi_wrn ("time-out elapsed 'unknown'");
			pthread_cancel(*phandle);
		}, (unsigned int*)&_timeout, (unsigned int*)&phandle
    ).detach();;
    */
    
    
	_handle->join();



	//todo interruptible threads
	delete _handle;

	#else
    DWORD result;
	result = ::WaitForSingleObject((HANDLE) _handle, _timeout);


	bool success = true;
	if (result == WAIT_TIMEOUT) {
		ssi_wrn ("time-out elapsed '%s'", _name);
		success = false;
	} else if (result == WAIT_FAILED) {
		ssi_wrn ("WaitForSingleObject() failed - error=%lu - ", ::GetLastError (), _name);
		success = false;
	}
	// close _handle



	::CloseHandle ((HANDLE) _handle);
	#endif // hasCXX11threads


	ssi_char_t string[SSI_MAX_CHAR];
	_stop_time = ssi_time_ms ();
	ssi_time_sprint (_stop_time - _start_time, string);
	ssi_msg (SSI_LOG_LEVEL_DEFAULT, "stop%safter %s '%s'", _single_execution ? " single execution " : " ", string, _name);

	return success;
}
Esempio n. 2
0
bool SVM::train (ISamples &samples,
	ssi_size_t stream_index) {

	if (_options.seed > 0) {
		srand(_options.seed);
	} else {
		srand(ssi_time_ms());
	}

	ISamples *s_balance = 0;
	switch (_options.balance) {
	case BALANCE::OFF: {
		s_balance = &samples;
		break;
	}
	case BALANCE::OVER: {		
		s_balance = new ISOverSample(&samples);
		ssi_pcast(ISOverSample, s_balance)->setOver(ISOverSample::RANDOM);
		ssi_msg(SSI_LOG_LEVEL_BASIC, "balance training set '%u' -> '%u'", samples.getSize(), s_balance->getSize());
		break;
	}
	case BALANCE::UNDER: {		
		s_balance = new ISUnderSample(&samples);
		ssi_pcast(ISUnderSample, s_balance)->setUnder(ISUnderSample::RANDOM);
		ssi_msg(SSI_LOG_LEVEL_BASIC, "balance training set '%u' -> '%u'", samples.getSize(), s_balance->getSize());
		break;
	}
	}

	_n_samples = s_balance->getSize();

	if (_n_samples == 0) {
		ssi_wrn ("empty sample list");
		return false;
	}

	if (isTrained ()) {
		ssi_wrn ("already trained");
		return false;
	}

	_n_classes = s_balance->getClassSize();
	_n_features = s_balance->getStream(stream_index).dim;
	ssi_size_t elements = _n_samples * (_n_features + 1);

	init_class_names(*s_balance);

	_problem = new svm_problem;
	_problem->l = ssi_cast (int, _n_samples);
	_problem->y = new double[_problem->l];
	_problem->x = new svm_node *[_problem->l];	

	s_balance->reset();
	ssi_sample_t *sample;
	int n_sample = 0;
	float *ptr = 0;
	svm_node *node = 0;
	while (sample = s_balance->next()) {
		ptr = ssi_pcast (float, sample->streams[stream_index]->ptr);		
		_problem->x[n_sample] = new svm_node[_n_features + 1];
		_problem->y[n_sample] = ssi_cast (float, sample->class_id);
		node = _problem->x[n_sample];
		for (ssi_size_t nfeat = 0; nfeat < _n_features; nfeat++) {
			node->index = nfeat+1;
            node->value = *ptr;
            ptr++;
			++node;
		}
		node->index = -1;		
		++n_sample;
	}

	if(_options.params.gamma == 0 && _n_features > 0) {
		_options.params.gamma = 1.0 / _n_features;
	}
	
	if (_options.params.kernel_type == PRECOMPUTED) {
		int max_index = ssi_cast (int, _n_features);
		for (int i = 0; i < _problem->l; i++) {
			if (_problem->x[i][0].index != 0) {
				ssi_err ("wrong input format: first column must be 0:sample_serial_number");				
			}
			if ((int)_problem->x[i][0].value <= 0 || (int)_problem->x[i][0].value > max_index) {
				ssi_err ("wrong input format: sample_serial_number out of range");
			}
		}
	}