Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #344 from karlnapf/master
some work towards migration
  • Loading branch information
Soeren Sonnenburg committed Dec 28, 2011
2 parents fd27f86 + 708d544 commit ba1f57d
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 22 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -19,6 +19,7 @@ TARGETS = basic_minimal classifier_libsvm classifier_minimal_svm \
library_hash parameter_set_from_parameters \
parameter_iterate_float64 parameter_iterate_sgobject \
parameter_modsel_parameters \
parameter_map \
modelselection_parameter_combination_test \
modelselection_model_selection_parameters_test \
modelselection_parameter_tree \
Expand Down
90 changes: 90 additions & 0 deletions examples/undocumented/libshogun/parameter_map.cpp
@@ -0,0 +1,90 @@
/*
* 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) 2011 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
*/

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

using namespace shogun;

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


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

ParameterMap* map=new ParameterMap();

map->put(
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_FLOAT64, 2),
new SGParamInfo("number", CT_SCALAR, ST_NONE, PT_INT32, 1)
);

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

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

map->print_map();
SG_SPRINT("\n");


/* get some elements from map, one/two ARE in map, three and four are NOT */
DynArray<SGParamInfo*> dummies;
dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE,
PT_INT32, 1));
dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE,
PT_FLOAT64, 2));
dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE,
PT_INT32, 2));
dummies.append_element(new SGParamInfo("number", CT_SCALAR, ST_NONE,
PT_FLOAT64, 0));

for (index_t i=0; i<dummies.get_num_elements(); ++i)
{
SGParamInfo* current=dummies.get_element(i);

char* s=current->to_string();
SG_SPRINT("searching for: %s\n", s);
SG_FREE(s);

if (i==2)
{

}

SGParamInfo* result=map->get(current);
if (result)
{
s=result->to_string();
SG_SPRINT("found: %s\n\n", s);
SG_FREE(s);
}
else
SG_SPRINT("nothing found\n\n");

delete current;
}


delete map;

exit_shogun();

return 0;
}

140 changes: 121 additions & 19 deletions src/shogun/base/ParameterMap.cpp
Expand Up @@ -9,6 +9,7 @@
*/

#include <shogun/base/ParameterMap.h>
#include <shogun/base/Parameter.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;
Expand All @@ -18,52 +19,95 @@ SGParamInfo::SGParamInfo()
init();
}

SGParamInfo::SGParamInfo(const SGParamInfo& orig)
{
init();

/* copy name */
m_name=strdup(orig.m_name);

m_ctype=orig.m_ctype;
m_stype=orig.m_stype;
m_ptype=orig.m_ptype;
m_param_version=orig.m_param_version;
}

SGParamInfo::SGParamInfo(const char* name, EContainerType ctype,
EStructType stype, EPrimitiveType ptype)
EStructType stype, EPrimitiveType ptype, int32_t param_version)
{
init();

/* copy name */
m_name=SG_MALLOC(char, strlen(name)+1);
strcpy(m_name, name);
m_name=strdup(name);

m_ctype=ctype;
m_stype=stype;
m_ptype=ptype;
m_param_version=param_version;
}

SGParamInfo::SGParamInfo(const TParameter* param, int32_t param_version)
{
init();

/* copy name */
m_name=strdup(param->m_name);

TSGDataType type=param->m_datatype;
m_ctype=type.m_ctype;
m_stype=type.m_stype;
m_ptype=type.m_ptype;
m_param_version=param_version;
}

SGParamInfo::~SGParamInfo()
{
SG_FREE(m_name);
}

void SGParamInfo::print_param_info()
char* SGParamInfo::to_string() const
{
SG_SPRINT("SGParamInfo with: ");

SG_SPRINT("name=\"%s\"", m_name);
char* buffer=SG_MALLOC(char, 200);
strcpy(buffer, "SGParamInfo with: ");
strcat(buffer, "name=\"");
strcat(buffer, m_name);
strcat(buffer, "\", type=");

TSGDataType t(m_ctype, m_stype, m_ptype);
index_t buffer_length=100;
char* buffer=SG_MALLOC(char, buffer_length);
t.to_string(buffer, buffer_length);
SG_SPRINT(", type=%s", buffer);
SG_FREE(buffer);
index_t l=100;
char* b=SG_MALLOC(char, l);
t.to_string(b, l);
strcat(buffer, b);
SG_FREE(b);

b=SG_MALLOC(char, 10);
sprintf(b, "%d", m_param_version);
strcat(buffer, ", version=");
strcat(buffer, b);
SG_FREE(b);

return buffer;
}

SG_SPRINT("\n");
void SGParamInfo::print_param_info()
{
char* s=to_string();
SG_SPRINT("%s\n", s);
SG_FREE(s);
}

SGParamInfo* SGParamInfo::duplicate() const
{
return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype);
return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype, m_param_version);
}

void SGParamInfo::init()
{
m_name=NULL;
m_ctype=(EContainerType) 0;
m_stype=(EStructType) 0;
m_ptype=(EPrimitiveType) 0;
m_ctype=(EContainerType) -1;
m_stype=(EStructType) -1;
m_ptype=(EPrimitiveType) -1;
m_param_version=-1;
}

bool SGParamInfo::operator==(const SGParamInfo& other) const
Expand All @@ -73,17 +117,75 @@ bool SGParamInfo::operator==(const SGParamInfo& other) const
result&=m_ctype==other.m_ctype;
result&=m_stype==other.m_stype;
result&=m_ptype==other.m_ptype;
result&=m_param_version==other.m_param_version;
SG_SPRINT("result: %d\n", result);
return result;
}

bool SGParamInfo::operator<(const SGParamInfo& other) const
{
return strcmp(m_name, other.m_name)<0;
int32_t result=strcmp(m_name, other.m_name);

if (result==0)
{
if (m_param_version==other.m_param_version)
{
if (m_ctype==other.m_ctype)
{
if (m_stype==other.m_stype)
{
if (m_ptype==other.m_ptype)
{
return false;
}
else
return m_ptype<other.m_ptype;
}
else
return m_stype<other.m_stype;
}
else
return m_ctype<other.m_ctype;
}
else
return m_param_version<other.m_param_version;

}
else
return result<0;
}

bool SGParamInfo::operator>(const SGParamInfo& other) const
{
return strcmp(m_name, other.m_name)>0;
int32_t result=strcmp(m_name, other.m_name);

if (result==0)
{
if (m_param_version==other.m_param_version)
{
if (m_ctype==other.m_ctype)
{
if (m_stype==other.m_stype)
{
if (m_ptype==other.m_ptype)
{
return false;
}
else
return m_ptype>other.m_ptype;
}
else
return m_stype>other.m_stype;
}
else
return m_ctype>other.m_ctype;
}
else
return m_param_version>other.m_param_version;

}
else
return result>0;
}

ParameterMapElement::ParameterMapElement()
Expand Down
29 changes: 26 additions & 3 deletions src/shogun/base/ParameterMap.h
Expand Up @@ -16,6 +16,8 @@
namespace shogun
{

struct TParameter;

/** @brief Class that holds informations about a certain parameter of an
* CSGObject. Contains name, type, etc.
* This is used for mapping types that have changed in different versions of
Expand All @@ -35,26 +37,42 @@ class SGParamInfo
* @param ctype container type of parameter
* @param stype struct type of parameter
* @param ptype primitive type of parameter
* @param param_version version of parameter
*/
SGParamInfo(const char* name, EContainerType ctype, EStructType stype,
EPrimitiveType ptype);
EPrimitiveType ptype, int32_t param_version);

/** constructor to create from a TParameter instance
*
* @param param TParameter instance to use
* @param param_version version of parameter
*/
SGParamInfo(const TParameter* param, int32_t param_version);

/** copy constructor
* @param element to copy from
*/
SGParamInfo(const SGParamInfo& orig);

/** destructor */
virtual ~SGParamInfo();

/** prints all parameter values */
void print_param_info();

/** @return string representation, caller has to clean up */
char* to_string() const;

/** @return an identical copy */
SGParamInfo* duplicate() const;

/** operator for comparison, true iff all attributes are equal */
bool operator==(const SGParamInfo& other) const;

/** operator for comparison (by string m_name) */
/** operator for comparison (by string m_name, if equal by others) */
bool operator<(const SGParamInfo& other) const;

/** operator for comparison (by string m_name) */
/** operator for comparison (by string m_name, if equal by others) */
bool operator>(const SGParamInfo& other) const;

private:
Expand All @@ -63,12 +81,17 @@ class SGParamInfo
public:
/** name */
char* m_name;

/** container type */
EContainerType m_ctype;

/** struct type */
EStructType m_stype;

/** primitive type */
EPrimitiveType m_ptype;

int32_t m_param_version;
};

/** @brief Class to hold instances of a parameter map. Each element contains a
Expand Down

0 comments on commit ba1f57d

Please sign in to comment.