1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#ifndef _SVMDATA_H_
#define _SVMDATA_H_
#include <stdio.h>
#include <deque>
#include <iostream>
#include "util.h"
using namespace std;
#define VEL_NORM_TOL 1e-4
struct trajectory{
unsigned int dim;
unsigned int nPoints;
double **coords;
double **vel;
int *y;
trajectory(): dim(0), coords(0), vel(0), y(0), nPoints(0){}
trajectory(int dim, int nPoints) : dim(dim), nPoints(nPoints)
{
coords = new double*[nPoints];
vel = new double*[nPoints];
for(int i=0; i<nPoints; i++)
{
coords[i] = new double[dim];
vel[i] = new double[dim];
}
y = new int[nPoints];
}
~trajectory()
{
if(coords)
{
for(int i=0; i<nPoints; i++)
delete [] coords[i];
delete [] coords;
coords = 0;
}
if(vel)
{
for(int i=0; i<nPoints; i++)
delete [] vel[i];
delete [] vel;
vel = 0;
}
if(y)
{
delete [] y;
y=0;
}
}
trajectory(const trajectory& other)
{
nPoints = other.nPoints;
dim = other.dim;
if(other.coords)
{
coords = new double*[nPoints];
for(int i=0; i<nPoints; i++)
{
coords[i] = new double[dim];
memcpy(coords[i], other.coords[i], dim*sizeof(double));
}
}
if(other.vel)
{
vel = new double*[nPoints];
for(int i=0; i<nPoints; i++)
{
vel[i] = new double[dim];
memcpy(vel[i], other.vel[i], dim*sizeof(double));
}
}
if(other.y)
{
y = new int[nPoints];
memcpy(y, other.y, nPoints*sizeof(int));
}
}
trajectory& operator=(const trajectory& other)
{
if(&other == this) return *this;
if(coords)
{
for(int i=0; i<nPoints; i++)
delete [] coords[i];
delete [] coords;
coords = 0;
}
if(vel)
{
for(int i=0; i<nPoints; i++)
delete [] vel[i];
delete [] vel;
vel = 0;
}
if(y)
{
delete [] y;
y=0;
}
nPoints = other.nPoints;
dim = other.dim;
if(other.coords)
{
coords = new double*[nPoints];
for(int i=0; i<nPoints; i++)
{
coords[i] = new double[dim];
memcpy(coords[i], other.coords[i], dim*sizeof(double));
}
}
if(other.vel)
{
vel = new double*[nPoints];
for(int i=0; i<nPoints; i++)
{
vel[i] = new double[dim];
memcpy(vel[i], other.vel[i], dim*sizeof(double));
}
}
if(other.y)
{
y = new int[nPoints];
memcpy(y, other.y, nPoints*sizeof(int));
}
return *this;
}
};
struct target{
unsigned int dim;
deque<trajectory> traj;
double *targ;
target() : targ(0), dim(0){}
target(const target& other)
{
dim = other.dim;
traj = other.traj;
if(other.targ)
{
targ = new double[dim];
memcpy(targ, other.targ, dim*sizeof(double));
}
else targ = 0;
}
~target()
{
if(targ)
{
delete [] targ;
targ = 0;
}
}
target& operator=(const target& other)
{
if(&other == this) return *this;
if(targ)
{
delete [] targ;
targ = 0;
}
dim = other.dim;
if(other.targ)
{
targ = new double[dim];
memcpy(targ, other.targ, dim*sizeof(double));
}
traj = other.traj;
return *this;
}
int classPoints()
{
int sum =0;
for(unsigned int i=0; i<traj.size(); i++)
sum += traj[i].nPoints;
return sum;
}
};
class asvmdata
{
friend class asvm;
friend class ASVM_NLopt_Solver;
friend class ASVM_SMO_Solver;
public:
bool isOkay;
unsigned int dim;
unsigned int num_alpha;
unsigned int num_beta;
double lambda;
int *labels;
int totalpoints()
{
int tp=0;
for(unsigned int i=0;i<tar.size();i++)
tp += tar[i].classPoints();
return tp;
}
asvmdata(): isOkay(false), dim(0), num_alpha(0), num_beta(0), lambda(0), labels(0), matkgh(0), target_class(0), initial(0){}
asvmdata& operator=(const asvmdata& other);
asvmdata(const asvmdata& other);
~asvmdata();
void printToFile(const char* filename);
bool loadFromFile(const char* file);
void setParams(const char *kernel_type="rbf", double kernel_width=0.1, double initial_guess=0);
void preprocess(unsigned int tclass);
void addTarget(target &t) { tar.push_back(t);}
public:
double** matkgh;
deque<target> tar;
double initial;
char type[1024];
unsigned int target_class;
// unsigned int* alpha_indices;
// unsigned int* beta_indices;
private:
void updateModulationKernel();
};
#endif
|