Skip to content

Commit

Permalink
Merge pull request #356 from karlnapf/master
Browse files Browse the repository at this point in the history
adding new parameters
  • Loading branch information
Soeren Sonnenburg committed Jan 11, 2012
2 parents 7561e11 + 84f648c commit 109cd6a
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 151 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -37,6 +37,7 @@ TARGETS = basic_minimal classifier_libsvm classifier_minimal_svm \
base_load_file_parameters \
base_map_parameters \
base_migration_type_conversion \
base_migration_dropping_and_new \
splitting_stratified_crossvalidation \
mathematics_arpack \
library_fibonacci_heap \
Expand Down
36 changes: 18 additions & 18 deletions examples/undocumented/libshogun/base_load_file_parameter.cpp
Expand Up @@ -94,44 +94,44 @@ class CTestClassFloat : public CSGObject

/* add some parameter mappings for number, here: type changes */
m_parameter_map->put(
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1),
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0)
new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 1),
new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0)
);

m_parameter_map->put(
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0),
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1)
new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT8, 0),
new const SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, -1)
);

/* changes for vector: from int32_t vector to float64_t SG_VECTOR */
m_parameter_map->put(
new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1),
new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0)
new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_FLOAT64, 1),
new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0)
);

/* from normal vector to SG_VECTOR of same type */
m_parameter_map->put(
new SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0),
new SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1)
new const SGParamInfo("vector", CT_SGVECTOR, ST_NONE, PT_INT32, 0),
new const SGParamInfo("vector", CT_VECTOR, ST_NONE, PT_INT32, -1)
);

/* changes for vector: from int32_t vector to float64_t SG_VECTOR */
m_parameter_map->put(
new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1),
new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0)
new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_FLOAT64, 1),
new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0)
);

/* from normal vector to SG_VECTOR of same type */
m_parameter_map->put(
new SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0),
new SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1)
new const SGParamInfo("matrix", CT_SGMATRIX, ST_NONE, PT_INT32, 0),
new const SGParamInfo("matrix", CT_MATRIX, ST_NONE, PT_INT32, -1)
);

/* name change for sgobject */
m_parameter_map->put(
new SGParamInfo("float_features", CT_SCALAR, ST_NONE,
new const SGParamInfo("float_features", CT_SCALAR, ST_NONE,
PT_SGOBJECT, 1),
new SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT,
new const SGParamInfo("int_features", CT_SCALAR, ST_NONE, PT_SGOBJECT,
0)
);

Expand Down Expand Up @@ -174,16 +174,16 @@ void test_load_file_parameter()

/* build parameter info for parameter of the OTHER instance, start from
* version 1 */
SGParamInfo param_info_number(
const SGParamInfo param_info_number(
float_instance->m_parameters->get_parameter(0), 1);

SGParamInfo param_info_vector(
const SGParamInfo param_info_vector(
float_instance->m_parameters->get_parameter(1), 1);

SGParamInfo param_info_matrix(
const SGParamInfo param_info_matrix(
float_instance->m_parameters->get_parameter(2), 1);

SGParamInfo param_info_sgobject(
const SGParamInfo param_info_sgobject(
float_instance->m_parameters->get_parameter(3), 1);

int32_t file_version=-1;
Expand Down
5 changes: 3 additions & 2 deletions examples/undocumented/libshogun/base_map_parameters.cpp
Expand Up @@ -158,7 +158,7 @@ class CTestClassFloat : public CSGObject
virtual const char* get_name() const { return "TestClassFloat"; }

virtual TParameter* migrate(DynArray<TParameter*>* param_base,
SGParamInfo* target)
const SGParamInfo* target)
{
TSGDataType type(target->m_ctype, target->m_stype,
target->m_ptype);
Expand Down Expand Up @@ -283,7 +283,8 @@ void test_load_file_parameter()
file, "");

/* create an array of param infos from float instance parameters */
DynArray<SGParamInfo*>* param_infos=new DynArray<SGParamInfo*>();
DynArray<const SGParamInfo*>* param_infos=
new DynArray<const SGParamInfo*>();
for (index_t i=0; i<float_instance->m_parameters->get_num_parameters(); ++i)
{
param_infos->append_element(
Expand Down
157 changes: 157 additions & 0 deletions examples/undocumented/libshogun/base_migration_dropping_and_new.cpp
@@ -0,0 +1,157 @@
/*
* 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) 2012 Heiko Strathmann
* Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society
*/

#include <shogun/base/init.h>
#include <shogun/base/Parameter.h>
#include <shogun/io/SerializableAsciiFile.h>
#include <shogun/base/ParameterMap.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

class CTestClassOld : public CSGObject
{
public:
CTestClassOld()
{
m_number_to_drop=10;
m_parameters->add(&m_number_to_drop, "m_number_to_drop", "");

m_number_to_keep=10;
m_parameters->add(&m_number_to_keep, "m_number_to_keep", "");
}

int32_t m_number_to_drop;
int32_t m_number_to_keep;

virtual const char* get_name() const { return "TestClassOld"; }
};

class CTestClassNew : public CSGObject
{
public:
CTestClassNew()
{
m_number=3;
m_parameters->add(&m_number, "m_number", "");

m_number_new=4;
m_parameters->add(&m_number_new, "m_number_new", "");

/* change name of to be kept number */
m_parameter_map->put(
new SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1),
new SGParamInfo("m_number_to_keep", CT_SCALAR, ST_NONE, PT_INT32, 0)
);

/* this parameter is new in this version, mapping from "nowhere" */
m_parameter_map->put(
new SGParamInfo("m_number_new", CT_SCALAR, ST_NONE, PT_INT32, 1),
new SGParamInfo()
);

/* note that dropped parameters need not be considered, just ignored */

/* needed if more than one element */
m_parameter_map->finalize_map();
}

int32_t m_number;
int32_t m_number_new;

virtual const char* get_name() const { return "TestClassNew"; }

virtual TParameter* migrate(DynArray<TParameter*>* param_base,
const SGParamInfo* target)
{
SG_PRINT("entering CTestClassNew::migrate\n");
TParameter* result=NULL;
TParameter* to_migrate=NULL;

if (*target==SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1))
{
/* specify name change here (again, was also done in mappings) */
char* new_name="m_number_to_keep";
one_to_one_migration_prepare(param_base, target, result,
to_migrate, new_name);

/* here: simply copy data because nothing has changed */
*((int32_t*)result->m_parameter)=
*((int32_t*)to_migrate->m_parameter);
}

/* note there has to be no case distinction for the new parameter */

if (result)
return result;
else
return CSGObject::migrate(param_base, target);
}
};

void check_equalness(CTestClassOld* old_instance,
CTestClassNew* new_instance)
{
/* number */
SG_SPRINT("checking \"m_number\":\n");
SG_SPRINT("\t%d==%d\n", old_instance->m_number_to_keep,
new_instance->m_number);
ASSERT(old_instance->m_number_to_keep==new_instance->m_number);

/* new element */
SG_SPRINT("checking \"m_number_new\":\n");
SG_SPRINT("\t%d\n", new_instance->m_number_new);
}

void test_migration()
{
const char* filename="test.txt";

/* create one instance of each class */
CTestClassOld* old_instance=new CTestClassOld();
CTestClassNew* new_instance=new CTestClassNew();

CSerializableAsciiFile* file;

/* serialize int instance, use custom parameter version */
file=new CSerializableAsciiFile(filename, 'w');
old_instance->save_serializable(file, "", 0);
file->close();
SG_UNREF(file);

/* de-serialize float instance, use custom parameter version */
file=new CSerializableAsciiFile(filename, 'r');
new_instance->load_serializable(file, "", 1);
file->close();
SG_UNREF(file);

/* assert that content is equal */
check_equalness(old_instance, new_instance);

SG_UNREF(old_instance);
SG_UNREF(new_instance);
SG_UNREF(file);
}

int main(int argc, char **argv)
{
init_shogun(&print_message, &print_message, &print_message);

test_migration();

exit_shogun();

return 0;
}

Expand Up @@ -162,11 +162,8 @@ class CTestClassFloat : public CSGObject
virtual const char* get_name() const { return "TestClassFloat"; }

virtual TParameter* migrate(DynArray<TParameter*>* param_base,
SGParamInfo* target)
const SGParamInfo* target)
{
TSGDataType type(target->m_ctype, target->m_stype,
target->m_ptype);

TParameter* result=NULL;
TParameter* to_migrate=NULL;

Expand Down Expand Up @@ -247,8 +244,9 @@ class CTestClassFloat : public CSGObject
TParameter* to_migrate=NULL;

/* specify name change and thats it */
char* new_name="int_features";
one_to_one_migration_prepare(param_base, target, result,
to_migrate, "int_features");
to_migrate, new_name);
}

if (result)
Expand Down Expand Up @@ -324,7 +322,7 @@ void check_equalness(CTestClassInt* int_instance,
}
}

void test_load_file_parameter()
void test_migration()
{
/* create one instance of each class */
CTestClassInt* int_instance=new CTestClassInt();
Expand Down Expand Up @@ -369,7 +367,7 @@ int main(int argc, char **argv)
{
init_shogun(&print_message, &print_message, &print_message);

test_load_file_parameter();
test_migration();

exit_shogun();

Expand Down
32 changes: 17 additions & 15 deletions examples/undocumented/libshogun/base_parameter_map.cpp
Expand Up @@ -35,6 +35,11 @@ void test_mapping_1()
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 0)
);

map->put(
new SGParamInfo("number_2", CT_SCALAR, ST_NONE, PT_INT32, 1),
new SGParamInfo("number_to_keep", CT_SCALAR, ST_NONE, PT_INT32, 0)
);

/* finalizing the map is needed before accessing it */
map->finalize_map();

Expand All @@ -52,6 +57,8 @@ void test_mapping_1()
PT_INT32, 2));
dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE,
PT_FLOAT64, 0));
dummies.append_element(new SGParamInfo("number_2", CT_SCALAR, ST_NONE,
PT_INT32, 1));

for (index_t i=0; i<dummies.get_num_elements(); ++i)
{
Expand All @@ -61,12 +68,7 @@ void test_mapping_1()
SG_SPRINT("searching for: %s\n", s);
SG_FREE(s);

if (i==2)
{

}

SGParamInfo* result=map->get(current);
const SGParamInfo* result=map->get(current);
if (result)
{
s=result->to_string();
Expand All @@ -82,9 +84,9 @@ void test_mapping_1()
delete map;
}

void print_value(SGParamInfo* key, ParameterMap* map)
void print_value(const SGParamInfo* key, ParameterMap* map)
{
SGParamInfo* current=map->get(key);
const SGParamInfo* current=map->get(key);
key->print_param_info();
SG_SPRINT("value: ");

Expand All @@ -109,14 +111,14 @@ void test_mapping_2()
EPrimitiveType pfrom=PT_BOOL;
EPrimitiveType pto=PT_SGOBJECT;

map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 1),
new SGParamInfo("eins", cto, sto, pto, 2));
map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2),
new SGParamInfo("eins", cto, sto, pto, 1));
map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2),
new SGParamInfo("zwei", cto, sto, pto, 1));
map->put(new SGParamInfo("3", cfrom, sfrom, pfrom, 3),
new SGParamInfo("drei", cto, sto, pto, 5));
map->put(new SGParamInfo("3", cfrom, sfrom, pfrom, 4),
new SGParamInfo("drei", cto, sto, pto, 3));
map->put(new SGParamInfo("4", cfrom, sfrom, pfrom, 4),
new SGParamInfo("vier", cto, sto, pto, 2));
new SGParamInfo("vier", cto, sto, pto, 3));

SG_SPRINT("before finalization:\n");
map->print_map();
Expand All @@ -125,7 +127,7 @@ void test_mapping_2()
SG_SPRINT("\n\nafter finalization:\n");
map->print_map();

SGParamInfo* key;
const SGParamInfo* key;

SG_SPRINT("\n\ntesting map\n");
key=new SGParamInfo("1", cfrom, sfrom, pfrom, 1);
Expand All @@ -148,7 +150,7 @@ void test_mapping_2()
print_value(key, map);
delete key;

key=new SGParamInfo("5", cfrom, sfrom, pfrom, 5);
key=new SGParamInfo("5", cfrom, sfrom, pfrom, 4);
print_value(key, map);
delete key;

Expand Down

0 comments on commit 109cd6a

Please sign in to comment.