I try to obtain the adjacency matrix of weights, and then use it in the calculation of the minimum weight path. There is a problem, when I try to display it, I get a wrong result :
By logic, the diagonal must have only 0, and in the places where the vertices are adjacent, must be the weight of the edge
//set the source and destination of each edge
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[0]->weight = 9;
g->edge[1]->src = 0;
g->edge[1]->dest = 10;
g->edge[1]->weight = 6;
g->edge[2]->src = 1;
g->edge[2]->dest = 2;
g->edge[2]->weight = 3;
g->edge[3]->src = 1;
g->edge[3]->dest = 10;
g->edge[3]->weight = 2;
g->edge[4]->src = 2;
g->edge[4]->dest = 3;
g->edge[4]->weight = 2;
g->edge[5]->src = 2;
g->edge[5]->dest = 6;
g->edge[5]->weight = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 5;
g->edge[6]->weight = 3;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[7]->weight = 5;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
g->edge[8]->weight = 4;
g->edge[9]->src = 6;
g->edge[9]->dest = 10;
g->edge[9]->weight = 2;
g->edge[10]->src = 6;
g->edge[10]->dest = 7;
g->edge[10]->weight = 9;
g->edge[11]->src = 7;
g->edge[11]->dest = 8;
g->edge[11]->weight = 7;
g->edge[12]->src = 7;
g->edge[12]->dest = 9;
g->edge[12]->weight = 2;
g->edge[13]->src = 8;
g->edge[13]->dest = 9;
g->edge[13]->weight = 7;
g->edge[14]->src = 9;
g->edge[14]->dest = 10;
g->edge[14]->weight = 5;
My code :
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
What's wrong?
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
In this code you are setting every edge from vertex i to every other vertex to the same weight. I do not think this is what you want.
( Note: It is hard to know what you do want. When reporting a problem you need to include a description of both what happens AND what you wanted to happen. "It's wrong!" is almost useless as a bug report. )
I'm working on a slot machine project for school and I've come up with this code for spitting out the images randomly. I feel like the entire randomCheck() function is a huge waste of space. Can anyone enlighten me how to do this more efficiently?
It's a lot of code... sorry about that. Please let me know if you need any more information from me.
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<random>
#include<vector>
#include <windows.h>
#include <ctime>
using namespace std;
int nuM;
int getNum(int min, int max)
{
nuM = min + (rand() % (max - min) + 1);
return nuM;
}
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y, string text)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
cout << text;
}
vector<int>v(9);
string Clover[] =
{ " .-.-. "
," ( ) "
," .-.\\ : /.-. "
,"( . : . ) "
," ( /|\\ ) "
," `\"` | `\"` "
," ~ "
};
string Seven[] =
{ " _________ "
," |____ / "
," / / "
," / / "
," / / "
," / / "
," /__/ "
};
string Money[] =
{ " ___ "
," _\\ /_ "
," / \\ "
," | | "
," | $$$ | "
," | | "
," \\_____/ "
};
string Diamond[] =
{ " _ "
," / \\ "
," / \\ "
," / \\ "
," \\ / "
," \\ / "
," \\_/ "
};
string Bolt[] =
{ " __ "
," _/ / "
," /__/ "
," _// "
," /_/ "
," // "
,"/' "
};
string Cherry[] =
{ "-._.-(`-.__`-. "
," \\ `~~`"
," .--./ \\ "
,"/# \\ \\.--. "
,"\\ / /# \\"
," '--' \\ / "
," '--' "
};
string wtf1[7];
string wtf2[7];
string wtf3[7];
string wtf4[7];
string wtf5[7];
string wtf6[7];
string wtf7[7];
string wtf8[7];
string wtf9[7];
void slotMachine(string a[], string b[], string c[], string d[], string e[], string f[], string g[], string h[], string i[])
{
for (int y = 0; y<7; y++)
{
gotoXY(20, 1 + y, a[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 1 + y, b[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 1 + y, c[y]);
}
//second line
for (int y = 0; y<7; y++)
{
gotoXY(20, 10 + y, d[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 10 + y, e[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 10 + y, f[y]);
}
// third line
for (int y = 0; y<7; y++)
{
gotoXY(20, 20 + y, g[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(45, 20 + y, h[y]);
}
for (int y = 0; y<7; y++)
{
gotoXY(70, 20 + y, i[y]);
}
}
void randomCheck()
{
//a1
getNum(0, 6);
if (nuM == 1)
{
v[0] = 1;
wtf1[0] = Seven[0];
wtf1[1] = Seven[1];
wtf1[2] = Seven[2];
wtf1[3] = Seven[3];
wtf1[4] = Seven[4];
wtf1[5] = Seven[5];
wtf1[6] = Seven[6];
}
else if (nuM == 2)
{
v[0] = 2;
wtf1[0] = Money[0];
wtf1[1] = Money[1];
wtf1[2] = Money[2];
wtf1[3] = Money[3];
wtf1[4] = Money[4];
wtf1[5] = Money[5];
wtf1[6] = Money[6];
}
else if (nuM == 3)
{
v[0] = 3;
wtf1[0] = Diamond[0];
wtf1[1] = Diamond[1];
wtf1[2] = Diamond[2];
wtf1[3] = Diamond[3];
wtf1[4] = Diamond[4];
wtf1[5] = Diamond[5];
wtf1[6] = Diamond[6];
}
else if (nuM == 4)
{
v[0] = 4;
wtf1[0] = Bolt[0];
wtf1[1] = Bolt[1];
wtf1[2] = Bolt[2];
wtf1[3] = Bolt[3];
wtf1[4] = Bolt[4];
wtf1[5] = Bolt[5];
wtf1[6] = Bolt[6];
}
else if (nuM == 5)
{
v[0] = 5;
wtf1[0] = Cherry[0];
wtf1[1] = Cherry[1];
wtf1[2] = Cherry[2];
wtf1[3] = Cherry[3];
wtf1[4] = Cherry[4];
wtf1[5] = Cherry[5];
wtf1[6] = Cherry[6];
}
else
{
v[0] = 6;
wtf1[0] = Clover[0];
wtf1[1] = Clover[1];
wtf1[2] = Clover[2];
wtf1[3] = Clover[3];
wtf1[4] = Clover[4];
wtf1[5] = Clover[5];
wtf1[6] = Clover[6];
}
//a2
getNum(0, 6);
if (nuM == 1)
{
v[1] = 1;
wtf2[0] = Seven[0];
wtf2[1] = Seven[1];
wtf2[2] = Seven[2];
wtf2[3] = Seven[3];
wtf2[4] = Seven[4];
wtf2[5] = Seven[5];
wtf2[6] = Seven[6];
}
else if (nuM == 2)
{
v[1] = 2;
wtf2[0] = Money[0];
wtf2[1] = Money[1];
wtf2[2] = Money[2];
wtf2[3] = Money[3];
wtf2[4] = Money[4];
wtf2[5] = Money[5];
wtf2[6] = Money[6];
}
else if (nuM == 3)
{
v[1] = 3;
wtf2[0] = Diamond[0];
wtf2[1] = Diamond[1];
wtf2[2] = Diamond[2];
wtf2[3] = Diamond[3];
wtf2[4] = Diamond[4];
wtf2[5] = Diamond[5];
wtf2[6] = Diamond[6];
}
else if (nuM == 4)
{
v[1] = 4;
wtf2[0] = Bolt[0];
wtf2[1] = Bolt[1];
wtf2[2] = Bolt[2];
wtf2[3] = Bolt[3];
wtf2[4] = Bolt[4];
wtf2[5] = Bolt[5];
wtf2[6] = Bolt[6];
}
else if (nuM == 5)
{
v[1] = 5;
wtf2[0] = Cherry[0];
wtf2[1] = Cherry[1];
wtf2[2] = Cherry[2];
wtf2[3] = Cherry[3];
wtf2[4] = Cherry[4];
wtf2[5] = Cherry[5];
wtf2[6] = Cherry[6];
}
else
{
v[1] = 6;
wtf2[0] = Clover[0];
wtf2[1] = Clover[1];
wtf2[2] = Clover[2];
wtf2[3] = Clover[3];
wtf2[4] = Clover[4];
wtf2[5] = Clover[5];
wtf2[6] = Clover[6];
}
//a3
getNum(0, 6);
if (nuM == 1)
{
v[2] = 1;
wtf3[0] = Seven[0];
wtf3[1] = Seven[1];
wtf3[2] = Seven[2];
wtf3[3] = Seven[3];
wtf3[4] = Seven[4];
wtf3[5] = Seven[5];
wtf3[6] = Seven[6];
}
else if (nuM == 2)
{
v[2] = 2;
wtf3[0] = Money[0];
wtf3[1] = Money[1];
wtf3[2] = Money[2];
wtf3[3] = Money[3];
wtf3[4] = Money[4];
wtf3[5] = Money[5];
wtf3[6] = Money[6];
}
else if (nuM == 3)
{
v[2] = 3;
wtf3[0] = Diamond[0];
wtf3[1] = Diamond[1];
wtf3[2] = Diamond[2];
wtf3[3] = Diamond[3];
wtf3[4] = Diamond[4];
wtf3[5] = Diamond[5];
wtf3[6] = Diamond[6];
}
else if (nuM == 4)
{
v[2] = 4;
wtf3[0] = Bolt[0];
wtf3[1] = Bolt[1];
wtf3[2] = Bolt[2];
wtf3[3] = Bolt[3];
wtf3[4] = Bolt[4];
wtf3[5] = Bolt[5];
wtf3[6] = Bolt[6];
}
else if (nuM == 5)
{
v[2] = 5;
wtf3[0] = Cherry[0];
wtf3[1] = Cherry[1];
wtf3[2] = Cherry[2];
wtf3[3] = Cherry[3];
wtf3[4] = Cherry[4];
wtf3[5] = Cherry[5];
wtf3[6] = Cherry[6];
}
else
{
v[2] = 6;
wtf3[0] = Clover[0];
wtf3[1] = Clover[1];
wtf3[2] = Clover[2];
wtf3[3] = Clover[3];
wtf3[4] = Clover[4];
wtf3[5] = Clover[5];
wtf3[6] = Clover[6];
}
//b1
getNum(0, 6);
if (nuM == 1)
{
v[3] = 1;
wtf4[0] = Seven[0];
wtf4[1] = Seven[1];
wtf4[2] = Seven[2];
wtf4[3] = Seven[3];
wtf4[4] = Seven[4];
wtf4[5] = Seven[5];
wtf4[6] = Seven[6];
}
else if (nuM == 2)
{
v[3] = 2;
wtf4[0] = Money[0];
wtf4[1] = Money[1];
wtf4[2] = Money[2];
wtf4[3] = Money[3];
wtf4[4] = Money[4];
wtf4[5] = Money[5];
wtf4[6] = Money[6];
}
else if (nuM == 3)
{
v[3] = 3;
wtf4[0] = Diamond[0];
wtf4[1] = Diamond[1];
wtf4[2] = Diamond[2];
wtf4[3] = Diamond[3];
wtf4[4] = Diamond[4];
wtf4[5] = Diamond[5];
wtf4[6] = Diamond[6];
}
else if (nuM == 4)
{
v[3] = 4;
wtf4[0] = Bolt[0];
wtf4[1] = Bolt[1];
wtf4[2] = Bolt[2];
wtf4[3] = Bolt[3];
wtf4[4] = Bolt[4];
wtf4[5] = Bolt[5];
wtf4[6] = Bolt[6];
}
else if (nuM == 5)
{
v[3] = 5;
wtf4[0] = Cherry[0];
wtf4[1] = Cherry[1];
wtf4[2] = Cherry[2];
wtf4[3] = Cherry[3];
wtf4[4] = Cherry[4];
wtf4[5] = Cherry[5];
wtf4[6] = Cherry[6];
}
else
{
v[3] = 6;
wtf4[0] = Clover[0];
wtf4[1] = Clover[1];
wtf4[2] = Clover[2];
wtf4[3] = Clover[3];
wtf4[4] = Clover[4];
wtf4[5] = Clover[5];
wtf4[6] = Clover[6];
}
//b2
getNum(0, 6);
if (nuM == 1)
{
v[4] = 1;
wtf5[0] = Seven[0];
wtf5[1] = Seven[1];
wtf5[2] = Seven[2];
wtf5[3] = Seven[3];
wtf5[4] = Seven[4];
wtf5[5] = Seven[5];
wtf5[6] = Seven[6];
}
else if (nuM == 2)
{
v[4] = 2;
wtf5[0] = Money[0];
wtf5[1] = Money[1];
wtf5[2] = Money[2];
wtf5[3] = Money[3];
wtf5[4] = Money[4];
wtf5[5] = Money[5];
wtf5[6] = Money[6];
}
else if (nuM == 3)
{
v[4] = 3;
wtf5[0] = Diamond[0];
wtf5[1] = Diamond[1];
wtf5[2] = Diamond[2];
wtf5[3] = Diamond[3];
wtf5[4] = Diamond[4];
wtf5[5] = Diamond[5];
wtf5[6] = Diamond[6];
}
else if (nuM == 4)
{
v[4] = 4;
wtf5[0] = Bolt[0];
wtf5[1] = Bolt[1];
wtf5[2] = Bolt[2];
wtf5[3] = Bolt[3];
wtf5[4] = Bolt[4];
wtf5[5] = Bolt[5];
wtf5[6] = Bolt[6];
}
else if (nuM == 5)
{
v[4] = 5;
wtf5[0] = Cherry[0];
wtf5[1] = Cherry[1];
wtf5[2] = Cherry[2];
wtf5[3] = Cherry[3];
wtf5[4] = Cherry[4];
wtf5[5] = Cherry[5];
wtf5[6] = Cherry[6];
}
else
{
v[4] = 6;
wtf5[0] = Clover[0];
wtf5[1] = Clover[1];
wtf5[2] = Clover[2];
wtf5[3] = Clover[3];
wtf5[4] = Clover[4];
wtf5[5] = Clover[5];
wtf5[6] = Clover[6];
}
//b3
getNum(0, 6);
if (nuM == 1)
{
v[5] = 1;
wtf6[0] = Seven[0];
wtf6[1] = Seven[1];
wtf6[2] = Seven[2];
wtf6[3] = Seven[3];
wtf6[4] = Seven[4];
wtf6[5] = Seven[5];
wtf6[6] = Seven[6];
}
else if (nuM == 2)
{
v[5] = 2;
wtf6[0] = Money[0];
wtf6[1] = Money[1];
wtf6[2] = Money[2];
wtf6[3] = Money[3];
wtf6[4] = Money[4];
wtf6[5] = Money[5];
wtf6[6] = Money[6];
}
else if (nuM == 3)
{
v[5] = 3;
wtf6[0] = Diamond[0];
wtf6[1] = Diamond[1];
wtf6[2] = Diamond[2];
wtf6[3] = Diamond[3];
wtf6[4] = Diamond[4];
wtf6[5] = Diamond[5];
wtf6[6] = Diamond[6];
}
else if (nuM == 4)
{
v[5] = 4;
wtf6[0] = Bolt[0];
wtf6[1] = Bolt[1];
wtf6[2] = Bolt[2];
wtf6[3] = Bolt[3];
wtf6[4] = Bolt[4];
wtf6[5] = Bolt[5];
wtf6[6] = Bolt[6];
}
else if (nuM == 5)
{
v[5] = 5;
wtf6[0] = Cherry[0];
wtf6[1] = Cherry[1];
wtf6[2] = Cherry[2];
wtf6[3] = Cherry[3];
wtf6[4] = Cherry[4];
wtf6[5] = Cherry[5];
wtf6[6] = Cherry[6];
}
else
{
v[5] = 6;
wtf6[0] = Clover[0];
wtf6[1] = Clover[1];
wtf6[2] = Clover[2];
wtf6[3] = Clover[3];
wtf6[4] = Clover[4];
wtf6[5] = Clover[5];
wtf6[6] = Clover[6];
}
//c1
getNum(0, 6);
if (nuM == 1)
{
v[6] = 1;
wtf7[0] = Seven[0];
wtf7[1] = Seven[1];
wtf7[2] = Seven[2];
wtf7[3] = Seven[3];
wtf7[4] = Seven[4];
wtf7[5] = Seven[5];
wtf7[6] = Seven[6];
}
else if (nuM == 2)
{
v[6] = 2;
wtf7[0] = Money[0];
wtf7[1] = Money[1];
wtf7[2] = Money[2];
wtf7[3] = Money[3];
wtf7[4] = Money[4];
wtf7[5] = Money[5];
wtf7[6] = Money[6];
}
else if (nuM == 3)
{
v[6] = 3;
wtf7[0] = Diamond[0];
wtf7[1] = Diamond[1];
wtf7[2] = Diamond[2];
wtf7[3] = Diamond[3];
wtf7[4] = Diamond[4];
wtf7[5] = Diamond[5];
wtf7[6] = Diamond[6];
}
else if (nuM == 4)
{
v[6] = 4;
wtf7[0] = Bolt[0];
wtf7[1] = Bolt[1];
wtf7[2] = Bolt[2];
wtf7[3] = Bolt[3];
wtf7[4] = Bolt[4];
wtf7[5] = Bolt[5];
wtf7[6] = Bolt[6];
}
else if (nuM == 5)
{
v[6] = 5;
wtf7[0] = Cherry[0];
wtf7[1] = Cherry[1];
wtf7[2] = Cherry[2];
wtf7[3] = Cherry[3];
wtf7[4] = Cherry[4];
wtf7[5] = Cherry[5];
wtf7[6] = Cherry[6];
}
else
{
v[6] = 6;
wtf7[0] = Clover[0];
wtf7[1] = Clover[1];
wtf7[2] = Clover[2];
wtf7[3] = Clover[3];
wtf7[4] = Clover[4];
wtf7[5] = Clover[5];
wtf7[6] = Clover[6];
}
//c2
getNum(0, 6);
if (nuM == 1)
{
v[7] = 1;
wtf8[0] = Seven[0];
wtf8[1] = Seven[1];
wtf8[2] = Seven[2];
wtf8[3] = Seven[3];
wtf8[4] = Seven[4];
wtf8[5] = Seven[5];
wtf8[6] = Seven[6];
}
else if (nuM == 2)
{
v[7] = 2;
wtf8[0] = Money[0];
wtf8[1] = Money[1];
wtf8[2] = Money[2];
wtf8[3] = Money[3];
wtf8[4] = Money[4];
wtf8[5] = Money[5];
wtf8[6] = Money[6];
}
else if (nuM == 3)
{
v[7] = 3;
wtf8[0] = Diamond[0];
wtf8[1] = Diamond[1];
wtf8[2] = Diamond[2];
wtf8[3] = Diamond[3];
wtf8[4] = Diamond[4];
wtf8[5] = Diamond[5];
wtf8[6] = Diamond[6];
}
else if (nuM == 4)
{
v[7] = 4;
wtf8[0] = Bolt[0];
wtf8[1] = Bolt[1];
wtf8[2] = Bolt[2];
wtf8[3] = Bolt[3];
wtf8[4] = Bolt[4];
wtf8[5] = Bolt[5];
wtf8[6] = Bolt[6];
}
else if (nuM == 5)
{
v[7] = 5;
wtf8[0] = Cherry[0];
wtf8[1] = Cherry[1];
wtf8[2] = Cherry[2];
wtf8[3] = Cherry[3];
wtf8[4] = Cherry[4];
wtf8[5] = Cherry[5];
wtf8[6] = Cherry[6];
}
else
{
v[7] = 6;
wtf8[0] = Clover[0];
wtf8[1] = Clover[1];
wtf8[2] = Clover[2];
wtf8[3] = Clover[3];
wtf8[4] = Clover[4];
wtf8[5] = Clover[5];
wtf8[6] = Clover[6];
}
//c3
getNum(0, 6);
if (nuM == 1)
{
v[8] = 1;
wtf9[0] = Seven[0];
wtf9[1] = Seven[1];
wtf9[2] = Seven[2];
wtf9[3] = Seven[3];
wtf9[4] = Seven[4];
wtf9[5] = Seven[5];
wtf9[6] = Seven[6];
}
else if (nuM == 2)
{
v[8] = 2;
wtf9[0] = Money[0];
wtf9[1] = Money[1];
wtf9[2] = Money[2];
wtf9[3] = Money[3];
wtf9[4] = Money[4];
wtf9[5] = Money[5];
wtf9[6] = Money[6];
}
else if (nuM == 3)
{
v[8] = 3;
wtf9[0] = Diamond[0];
wtf9[1] = Diamond[1];
wtf9[2] = Diamond[2];
wtf9[3] = Diamond[3];
wtf9[4] = Diamond[4];
wtf9[5] = Diamond[5];
wtf9[6] = Diamond[6];
}
else if (nuM == 4)
{
v[8] = 4;
wtf9[0] = Bolt[0];
wtf9[1] = Bolt[1];
wtf9[2] = Bolt[2];
wtf9[3] = Bolt[3];
wtf9[4] = Bolt[4];
wtf9[5] = Bolt[5];
wtf9[6] = Bolt[6];
}
else if (nuM == 5)
{
v[8] = 5;
wtf9[0] = Cherry[0];
wtf9[1] = Cherry[1];
wtf9[2] = Cherry[2];
wtf9[3] = Cherry[3];
wtf9[4] = Cherry[4];
wtf9[5] = Cherry[5];
wtf9[6] = Cherry[6];
}
else
{
v[8] = 6;
wtf9[0] = Clover[0];
wtf9[1] = Clover[1];
wtf9[2] = Clover[2];
wtf9[3] = Clover[3];
wtf9[4] = Clover[4];
wtf9[5] = Clover[5];
wtf9[6] = Clover[6];
}
}
int main()
{
srand(time(0));
bool on = true;
while (on)
{
for (int i = 0; i < 10; i++)
{
system("cls");
randomCheck();
slotMachine(wtf1, wtf2, wtf3, wtf4, wtf5, wtf6, wtf7, wtf8, wtf9);
}
system("pause");
}
return 0;
}
Suggestion for randomCheck (I didn't check if it compiles, but hopefully there shouldn't be any problem):
void randomCheck()
{
static string* images[] = {
Seven, Money, Diamond, Bolt, Cherry, Clover
};
static string* wtfs[] = {
wtf1, wtf2, wtf3, wtf4, wtf5, wtf6, wtf7, wtf8, wtf9
};
for (int i = 0; i < 9; ++i) {
v[i] = getNum(0, 6);
string* image = images[v[i]];
string* wtf = wtfs[i];
for (int j = 0; j < 7; ++j)
wtf[j] = image[j];
}
}
Use loops and arrays to reduce code size.
I want to draw a circle. but anything isn't drawn.
for (int i = 0; i < 1080; i += 3) {
angle = (float)M_PI / 180.0f * i;
vertices[i] = sinf((float)angle) * 0.5f;
vertices[i + 1] = cosf((float)angle) * 0.5f;
vertices[i + 2] = 0.0f;
}
bool isFirst = true;
int temp;
for (int i = 0, j = 1; i < 360; i++) {
if (i % 3 == 0) {
indices[i] = 0;
}
else {
if (!isFirst) {
temp = ++j;
isFirst = true;
}
else {
temp = j;
isFirst = false;
}
indices[i] = (GLushort)temp;
}
}
.
.
.
glDrawElements(GL_TRIANGLE_STRIP, 360, GL_UNSIGNED_SHORT, 0);
Please help me......
first code works fine on android java gl es2.0.
for (int i = 0; i < 1080; i += 3) {
angle = (float)M_PI / 180.0f * i;
vertices[i] = sinf((float)angle) * 0.5f;
vertices[i + 1] = cosf((float)angle) * 0.5f;
vertices[i + 2] = 0.25f;
}
bool isFirst = true;
int temp;
for (int i = 0, j = 1; i < 360; i++) {
if (i % 3 == 0) {
indices[i] = 0;
}
else {
if (!isFirst) {
temp = ++j;
isFirst = true;
}
else {
temp = j;
isFirst = false;
}
indices[i] = (GLushort)temp;
}
}
I got some memory allocated in a loop - how to free it when I am done with tr_data variable ?
(I am fairly new to C++)
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
struct svm_problem tr_data;
tr_data.l = (int) prm_num_samples_anchored.array[bar];
tr_data.y = Malloc(double, tr_data.l);
tr_data.x = Malloc(struct svm_node*, tr_data.l);
for (int row = 0; row < tr_data.l; row++)
{
tr_data.y[row] = ta0.array[bar-row-1];
//leak
svm_node* tr_data_x_onerow = Malloc(svm_node, num_features+1);
tr_data_x_onerow[0].index = 1; tr_data_x_onerow[0].value = in0.array[bar-row-1]; tr_data_x_onerow[1].index = 2; tr_data_x_onerow[1].value = in1.array[bar-row-1]; tr_data_x_onerow[2].index = 3; tr_data_x_onerow[2].value = in2.array[bar-row-1]; tr_data_x_onerow[3].index = 4; tr_data_x_onerow[3].value = in3.array[bar-row-1]; tr_data_x_onerow[4].index = 5; tr_data_x_onerow[4].value = in4.array[bar-row-1]; tr_data_x_onerow[5].index = 6; tr_data_x_onerow[5].value = in5.array[bar-row-1]; tr_data_x_onerow[6].index = 7; tr_data_x_onerow[6].value = in6.array[bar-row-1]; tr_data_x_onerow[7].index = 8; tr_data_x_onerow[7].value = in7.array[bar-row-1]; tr_data_x_onerow[8].index = 9; tr_data_x_onerow[8].value = in8.array[bar-row-1]; tr_data_x_onerow[9].index = 10;
tr_data_x_onerow[num_features].index = -1; //Each row of properties should be terminated with a -1 according to the readme
tr_data.x[row] = tr_data_x_onerow;
}
... few operation on tr_data
... and this does not work
for (int row = 0; row <tr_data.l; row++)
{
free(tr_data_x_onerow);
}
for (int row = 0; row <tr_data.l; row++)
{
free(tr_data.x[row]);
}
But please, just don't do this. This is C++. Use a vector or some other sane collection.
I am trying to train an svm for a simple xor problem programmatically using libsvm to understand how the library works. The problem (i think) seems to be that i construct svm_node incorrectly; maybe i have trouble understanding the whole pointers to pointers thing. Could anybody help with this? I first construct a matrix for the xor problem then try to assign values from the matrix to svm_node (i am using 2 steps here because my real data will be in matrix format).
When testing the model i get incorrect values (always -1).
In a previous question i got help with the parameters C and gamma; these should be OK now since i got correct classifications for the xor problem using other code. Thanks again Pedrom!
I have searched in several places for answers, e.g. the Readme and in the SvmToy example; no luck however.
Here is the code that outputs incorrect classifications...
Thanks in advance!
//Parameters---------------------------------------------------------------------
svm_parameter param;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0.5;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
//Problem definition-------------------------------------------------------------
svm_problem prob;
//Length, 4 examples
prob.l = 4;
//x values matrix of xor values
QVector< QVector<double> >matrix;
QVector<double>row(2);
row[0] = 1;row[1] = 1;
matrix.push_back(row);
row[0] = 1;row[1] = 0;
matrix.push_back(row);
row[0] = 0;row[1] = 1;
matrix.push_back(row);
row[0] = 0;row[1] = 0;
matrix.push_back(row);
//This part i have trouble understanding
svm_node* x_space = new svm_node[3];
svm_node** x = new svm_node *[prob.l];
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row < matrix.size(); row++){
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = new double[prob.l];
prob.y[0] = -1;
prob.y[1] = 1;
prob.y[2] = 1;
prob.y[3] = -1;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//Test model----------------------------------------------------------------------
svm_node* testnode = new svm_node[3];
testnode[0].index = 0;
testnode[0].value = 1;
testnode[1].index = 1;
testnode[1].value = 0;
testnode[2].index = -1;
//Should return 1 but returns -1
double retval = svm_predict(model,testnode);
qDebug()<<retval;
It seems you've been trying to get this example to work for weeks. I followed the style in svm-train.c that comes with libsvm. I used your values for C and gamma. It is working. I tried all points in the XOR example and it is giving correct results.
The summary of the problem you're having is that you're not allocating space for the 4 data points you train with, so you just over-write the data. This is a typical mistake with pointers in C. It may help you brushed up on pointers in C/C++.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "svm.h"
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
struct svm_parameter param; // set by parse_command_line
struct svm_problem prob; // set by read_problem
struct svm_model *model;
struct svm_node *x_space;
int main(int argc, char **argv)
{
char input_file_name[1024];
char model_file_name[1024];
const char *error_msg;
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0.5;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 1;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
//Problem definition-------------------------------------------------------------
prob.l = 4;
//x values matrix of xor values
double matrix[prob.l][2];
matrix[0][0] = 1;
matrix[0][1] = 1;
matrix[1][0] = 1;
matrix[1][1] = 0;
matrix[2][0] = 0;
matrix[2][1] = 1;
matrix[3][0] = 0;
matrix[3][1] = 0;
//This part i have trouble understanding
svm_node** x = Malloc(svm_node*,prob.l);
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row <prob.l; row++){
svm_node* x_space = Malloc(svm_node,3);
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = Malloc(double,prob.l);
prob.y[0] = -1;
prob.y[1] = 1;
prob.y[2] = 1;
prob.y[3] = -1;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//Test model----------------------------------------------------------------------
svm_node* testnode = Malloc(svm_node,3);
testnode[0].index = 0;
testnode[0].value = 1;
testnode[1].index = 1;
testnode[1].value = 0;
testnode[2].index = -1;
//This works correctly:
double retval = svm_predict(model,testnode);
printf("retval: %f\n",retval);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return 0;
}