<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 1999-2009 <NAME>
 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include <shogun/classifier/svm/CPLEXSVM.h>
#include <shogun/lib/common.h>

#ifdef USE_CPLEX
#include <shogun/io/SGIO.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Cplex.h>
#include <shogun/features/Labels.h>

using namespace shogun;

CCPLEXSVM::CCPLEXSVM()
: CSVM()
{
}

CCPLEXSVM::~CCPLEXSVM()
{
}

bool CCPLEXSVM::train_machine(CFeatures* data)
{
	bool result = false;
	CCplex cplex;

	if (data)
	{
		if (labels->get_num_labels() != data->get_num_vectors())
			SG_ERROR("Number of training vectors does not match number of labels\n");
		kernel->init(data, data);
	}

	if (cplex.init(E_QP))
	{
		int32_t n,m;
		int32_t num_label=0;
		float64_t* y = labels->get_labels(num_label);
		float64_t* H = kernel->get_kernel_matrix<float64_t>(m, n, NULL);
		ASSERT(n>0 && n==m && n==num_label);
		float64_t* alphas=SG_MALLOC(float64_t, n);
		float64_t* lb=SG_MALLOC(float64_t, n);
		float64_t* ub=SG_MALLOC(float64_t, n);

		//hessian y'y.*K
		for (int32_t i=0; i<n; i++)
		{
			lb[i]=0;
			ub[i]=get_C1();

			for (int32_t j=0; j<n; j++)
				H[i*n+j]*=y[j]*y[i];
		}

		//feed qp to cplex


		int32_t j=0;
		for (int32_t i=0; i<n; i++)
		{
			if (alphas[i]>0)
			{
				//set_alpha(j, alphas[i]*labels->get_label(i)/etas[1]);
				set_alpha(j, alphas[i]*labels->get_label(i));
				set_support_vector(j, i);
				j++;
			}
		}
		//compute_objective();
		SG_INFO( "obj = %.16f, rho = %.16f\n",get_objective(),get_bias());
		SG_INFO( "Number of SV: %ld\n", get_num_support_vectors());

		SG_FREE(alphas);
		SG_FREE(lb);
		SG_FREE(ub);
		SG_FREE(H);

		result = true;
	}

	if (!result)
		SG_ERROR( "cplex svm failed");

	return result;
}
#endif<commit_msg>minor fixes updating data structures<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 1999-2009 <NAME>
 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
 */

#include <shogun/classifier/svm/CPLEXSVM.h>
#include <shogun/lib/common.h>

#ifdef USE_CPLEX
#include <shogun/io/SGIO.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Cplex.h>
#include <shogun/features/Labels.h>

using namespace shogun;

CCPLEXSVM::CCPLEXSVM()
: CSVM()
{
}

CCPLEXSVM::~CCPLEXSVM()
{
}

bool CCPLEXSVM::train_machine(CFeatures* data)
{
	bool result = false;
	CCplex cplex;

	if (data)
	{
		if (labels->get_num_labels() != data->get_num_vectors())
			SG_ERROR("Number of training vectors does not match number of labels\n");
		kernel->init(data, data);
	}

	if (cplex.init(E_QP))
	{
		int32_t n,m;
		int32_t num_label=0;
                SGVector<float64_t> y=labels->get_labels();
		SGMatrix<float64_t> H=kernel->get_kernel_matrix();
                m=H.num_rows;
                n=H.num_cols;
		ASSERT(n>0 && n==m && n==num_label);
		float64_t* alphas=SG_MALLOC(float64_t, n);
		float64_t* lb=SG_MALLOC(float64_t, n);
		float64_t* ub=SG_MALLOC(float64_t, n);

		//hessian y'y.*K
		for (int32_t i=0; i<n; i++)
		{
			lb[i]=0;
			ub[i]=get_C1();

			for (int32_t j=0; j<n; j++)
				H[i*n+j]*=y[j]*y[i];
		}

		//feed qp to cplex


		int32_t j=0;
		for (int32_t i=0; i<n; i++)
		{
			if (alphas[i]>0)
			{
				//set_alpha(j, alphas[i]*labels->get_label(i)/etas[1]);
				set_alpha(j, alphas[i]*labels->get_label(i));
				set_support_vector(j, i);
				j++;
			}
		}
		//compute_objective();
		SG_INFO( "obj = %.16f, rho = %.16f\n",get_objective(),get_bias());
		SG_INFO( "Number of SV: %ld\n", get_num_support_vectors());

		SG_FREE(alphas);
		SG_FREE(lb);
		SG_FREE(ub);
                H.destroy_matrix();

		result = true;
	}

	if (!result)
		SG_ERROR( "cplex svm failed");

	return result;
}
#endif
{'fraction_non_alphanumeric': 0.1636968401045379, 'fraction_numerical': 0.02851033499643621, 'mean_word_length': 3.8502304147465436, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7376755', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1761, 'n_words': 375}