How to draw new graph with new point? ROOT Cern - root-framework

I wanted to ask for a information.
I am studying ROOT.
I have 100 files and each file has 9 columns.
I need to take the x data of the first column and for every other y the relative values.
using the push_back method i create the vectors X and Y[8].
Now to normalize it, I need to take the smallest value among the elements of X and translate all the points and make a graph with all the points translated.
I wanted to ask where I am going wrong.
M= 8 columns
I = 100 file with 9 columns-- 1 for X and 8 for Y — for (int i=0; i<N; i++){ etc…
int npoints = (int)X.size();
for (int k=0; k<M; k++) {
g[i][k]= new TGraph();
g[i][k]->SetNameTitle( Form("graphic_name_%d_%d",i,k), Form("graphic_name_ %d_%d",i,k) );
}
for (int p=0; p<M; p++) {
for( int b=0; b<npoints; b++){
float a=X[0];
double t;
t=b-a;
g[i][p]->SetPoint(b,X[t],Y[p][t]);
}
}
please,can you help me?
thanks

This code snippet might give you some ideas on how to approach your problem:
#define NFILES 100
#define NROWS 20
#define NCOLUMNS 9
void graphs()
{
// Create some fake data
float data[NFILES][NROWS][NCOLUMNS];
for (int k=0; k<NFILES; k++) {
for (int i=0; i<NROWS; i++) {
for (int j=0; j<NCOLUMNS; j++) {
data[k][i][j] = k*10+i*2.5+j*0.01;
}
}
}
// Allocate graphs
TGraph* graphs[NFILES][NCOLUMNS-1];
for (int k=0; k<NFILES; k++) {
for (int j=0; j<NCOLUMNS-1; j++) {
graphs[k][j]= new TGraph();
graphs[k][j]->SetNameTitle( Form("graphic_name_%d_%d",k,j), Form("graphic_name_ %d_%d",k,j) );
}
}
// Fill graphs
for (int k=0; k<NFILES; k++) {
const float a = data[k][0][0];
for (int j=0; j<NCOLUMNS-1; j++) {
for( int i=0; i<NROWS; i++) {
graphs[k][j]->SetPoint(i,data[k][i][0]-a,data[k][i][j+1]);
}
}
}
}

Related

Rubik's cube genetic algorithm solver?

Is it possible Rubik's cube to be efficiently solved by genetic algorithms?
What kind of chromosome encoding should be used? How the crossover and mutation should be done?
I am using this model of the cube:
#ifndef RUBIKSCUBE_H_INCLUDED
#define RUBIKSCUBE_H_INCLUDED
#include "Common.h"
#include "RubiksSide.h"
#include "RubiksColor.h"
#include "RotationDirection.h"
class RubiksCube {
private:
int top[3][3];
int left[3][3];
int right[3][3];
int front[3][3];
int back[3][3];
int down[3][3];
int (*sides[6])[3][3];
std::string result;
void spinSide(RubiksSide side) {
static int buffer[ 3 ];
if (side == TOP) {
for (int i = 0; i < 3; i++) {
buffer[i] = left[i][2];
}
for (int i = 0; i < 3; i++) {
left[i][2] = front[0][i];
}
for (int i = 0; i < 3; i++) {
front[0][i] = right[3 - i - 1][0];
}
for (int i = 0; i < 3; i++) {
right[i][0] = back[2][i];
}
for (int i = 0; i < 3; i++) {
back[2][3 - i - 1] = buffer[i];
}
} else if (side == LEFT) {
for (int i = 0; i < 3; i++) {
buffer[i] = down[i][2];
}
for (int i = 0; i < 3; i++) {
down[3 - i - 1][2] = front[i][0];
}
for (int i = 0; i < 3; i++) {
front[i][0] = top[i][0];
}
for (int i = 0; i < 3; i++) {
top[i][0] = back[i][0];
}
for (int i = 0; i < 3; i++) {
back[3 - i - 1][0] = buffer[i];
}
} else if (side == BACK) {
for (int i = 0; i < 3; i++) {
buffer[i] = down[0][i];
}
for (int i = 0; i < 3; i++) {
down[0][i] = left[0][i];
}
for (int i = 0; i < 3; i++) {
left[0][i] = top[0][i];
}
for (int i = 0; i < 3; i++) {
top[0][i] = right[0][i];
}
for (int i = 0; i < 3; i++) {
right[0][i] = buffer[i];
}
} else if (side == RIGHT) {
for (int i = 0; i < 3; i++) {
buffer[i] = down[i][0];
}
for (int i = 0; i < 3; i++) {
down[i][0] = back[3 - i - 1][2];
}
for (int i = 0; i < 3; i++) {
back[i][2] = top[i][2];
}
for (int i = 0; i < 3; i++) {
top[i][2] = front[i][2];
}
for (int i = 0; i < 3; i++) {
front[3 - i - 1][2] = buffer[i];
}
} else if (side == FRONT) {
for (int i = 0; i < 3; i++) {
buffer[i] = down[2][i];
}
for (int i = 0; i < 3; i++) {
down[2][i] = right[2][i];
}
for (int i = 0; i < 3; i++) {
right[2][i] = top[2][i];
}
for (int i = 0; i < 3; i++) {
top[2][i] = left[2][i];
}
for (int i = 0; i < 3; i++)
left[2][i] = buffer[i];
} else if (side == DOWN) {
for (int i = 0; i < 3; i++) {
buffer[i] = front[2][i];
}
for (int i = 0; i < 3; i++) {
front[2][i] = left[i][0];
}
for (int i = 0; i < 3; i++) {
left[i][0] = back[0][3 - i - 1];
}
for (int i = 0; i < 3; i++) {
back[0][i] = right[i][2];
}
for (int i = 0; i < 3; i++) {
right[3 - i - 1][2] = buffer[i];
}
}
}
void spinClockwise(int side[3][3], int times, RubiksSide index) {
static int buffer[3][3];
static int newarray[3][3];
if (times == 0) {
return;
}
/*
* Transponse.
*/
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
newarray[j][i] = side[i][j];
}
}
/*
* Rearrange.
*/
for (int i = 0; i < 3; i++) {
static int cache = 0;
cache = newarray[i][0];
newarray[i][0] = newarray[i][2];
newarray[i][2] = cache;
}
spinSide(index);
memcpy(buffer, newarray, sizeof(int)*3*3);
for (int t = 1; t < times; t++) {
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
newarray[j][i] = buffer[i][j];
}
}
for (int i = 0; i < 3; i++) {
static int cache = 0;
cache = newarray[i][0];
newarray[i][0] = newarray[i][2];
newarray[i][2] = cache;
}
spinSide(index);
memcpy(buffer, newarray, sizeof(int)*3*3);
}
memcpy(side, buffer, sizeof(int)*3*3);
}
double euclidean(const RubiksCube &cube) const {
double difference = 0.0;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
difference += abs(top[i][j]-cube.top[i][j]);
difference += abs(left[i][j]-cube.left[i][j]);
difference += abs(right[i][j]-cube.right[i][j]);
difference += abs(front[i][j]-cube.front[i][j]);
difference += abs(back[i][j]-cube.back[i][j]);
difference += abs(down[i][j]-cube.down[i][j]);
}
}
return difference;
}
double colors(const RubiksCube &cube) const {
//TODO Change array with STL maps.
static const double coefficients[7][7] = {
{0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 2, 2, 2, 4},
{0, 2, 1, 2, 4, 2, 2},
{0, 2, 2, 1, 2, 4, 2},
{0, 2, 4, 2, 1, 2, 2},
{0, 2, 2, 4, 2, 1, 2},
{0, 4, 2, 2, 2, 2, 1},
};
double difference = 0.0;
/*
* Count matches for all sides.
*/
for(int s=0; s<6; s++) {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
/*
* If colors are equal calculate distance.
*/
difference += coefficients[(*sides[s])[1][1]][(*sides[s])[i][j]];
}
}
}
return difference;
}
double hausdorff(const RubiksCube &cube) const {
long ha = 0;
long hb = 0;
long result = 0;
for(int m=0; m<3; m++) {
for(int n=0; n<3; n++) {
int distances[] = {0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i=0, d=0; i<3; i++) {
for(int j=0; j<3; j++) {
distances[d++] = abs(top[m][n]-cube.top[i][j]);
distances[d++] = abs(left[m][n]-cube.left[i][j]);
distances[d++] = abs(right[m][n]-cube.right[i][j]);
distances[d++] = abs(front[m][n]-cube.front[i][j]);
distances[d++] = abs(back[m][n]-cube.back[i][j]);
distances[d++] = abs(down[m][n]-cube.down[i][j]);
}
}
int min = distances[0];
for(int d=0; d<54; d++) {
if(distances[d] < min) {
min = distances[d];
}
}
if(min > ha) {
ha = min;
}
}
}
for(int m=0; m<3; m++) {
for(int n=0; n<3; n++) {
int distances[] = {0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i=0, d=0; i<3; i++) {
for(int j=0; j<3; j++) {
distances[d++] = abs(top[i][j]-cube.top[m][n]);
distances[d++] = abs(left[i][j]-cube.left[m][n]);
distances[d++] = abs(right[i][j]-cube.right[m][n]);
distances[d++] = abs(front[i][j]-cube.front[m][n]);
distances[d++] = abs(back[i][j]-cube.back[m][n]);
distances[d++] = abs(down[i][j]-cube.down[m][n]);
}
}
int min = distances[0];
for(int d=0; d<54; d++) {
if(distances[d] < min) {
min = distances[d];
}
}
if(min > hb) {
hb = min;
}
}
}
result = std::max(ha, hb);
return(result);
}
friend std::ostream& operator<< (std::ostream &out, const RubiksCube &cube);
public:
RubiksCube() {
reset();
sides[0] = &top;
sides[1] = &left;
sides[2] = &right;
sides[3] = &front;
sides[4] = &back;
sides[5] = &down;
}
void reset() {
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
top[i][j] = GREEN;
left[i][j] = PURPLE;
right[i][j] = RED;
front[i][j] = WHITE;
back[i][j] = YELLOW;
down[i][j] = BLUE;
}
}
}
double compare(const RubiksCube &cube) const {
return euclidean(cube);
}
void callSpin(RubiksSide side, RotationDirection direction, int numberOfTimes) {
if (numberOfTimes < 0) {
numberOfTimes = -numberOfTimes;
if(direction == CLOCKWISE) {
direction = COUNTERCLOCKWISE;
} else if(direction == COUNTERCLOCKWISE) {
direction = CLOCKWISE;
}
}
numberOfTimes %= 4;
if (direction == CLOCKWISE) {
if (side == NONE) {
/*
* Do nothing.
*/
}
if (side == TOP) {
spinClockwise(top, numberOfTimes, TOP);
}
if (side == LEFT) {
spinClockwise(left, numberOfTimes, LEFT);
}
if (side == RIGHT) {
spinClockwise(right, numberOfTimes, RIGHT);
}
if (side == FRONT) {
spinClockwise(front, numberOfTimes, FRONT);
}
if (side == BACK) {
spinClockwise(back, numberOfTimes, BACK);
}
if (side == DOWN) {
spinClockwise(down, numberOfTimes, DOWN);
}
}
}
void execute(std::string commands) {
for(int i=0; i<commands.length(); i++) {
callSpin((RubiksSide)commands[i], CLOCKWISE, 1);
}
}
std::string shuffle(int numberOfMoves=0) {
std::string commands = "";
for(int i=0; i<numberOfMoves; i++) {
switch(rand()%6) {
case 0:
commands+=(char)TOP;
break;
case 1:
commands+=(char)LEFT;
break;
case 2:
commands+=(char)RIGHT;
break;
case 3:
commands+=(char)FRONT;
break;
case 4:
commands+=(char)BACK;
break;
case 5:
commands+=(char)DOWN;
break;
}
}
execute(commands);
return commands;
}
const std::string& toString() {
result = "";
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(top[i][j]) + " ";
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(left[i][j]) + " ";
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(right[i][j]) + " ";
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(front[i][j]) + " ";
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(back[i][j]) + " ";
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
result += std::to_string(down[i][j]) + " ";
}
}
/*
* Trim spaces.
*/
result.erase(result.size()-1, 1);
result += '\0';
return result;
}
void fromString(const char text[]) {
std::string buffer(text);
std::istringstream in(buffer);
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> top[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> left[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> right[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> front[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> back[i][j];
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
in >> down[i][j];
}
}
}
};
std::ostream& operator<< (std::ostream &out, const RubiksCube &cube) {
for(int i=0; i<3; i++) {
out << " ";
for(int j=0; j<3; j++) {
out << cube.back[i][j] << " ";
}
out << std::endl;
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
out << cube.left[i][j] << " ";
}
for(int j=0; j<3; j++) {
out << cube.top[i][j] << " ";
}
for(int j=0; j<3; j++) {
out << cube.right[i][j] << " ";
}
for(int j=0; j<3; j++) {
out << cube.down[i][j] << " ";
}
out << std::endl;
}
for(int i=0; i<3; i++) {
out << " ";
for(int j=0; j<3; j++) {
out << cube.front[i][j] << " ";
}
out << std::endl;
}
return out;
}
#endif
Disclaimer: I'm by far no expert on Rubik's cubes, I even never solved one
Solving a given Rubik's cube by GA
You have a given configuration and you want to use GA to produce the sequence of steps that solve this particular configuration.
Representation
For each of the axes there are three possible rotations, each of them in two directions. This gives following moves:
X1+, X1-, X2+, X2-, X3+, X3-,
Y1+, Y1-, Y2+, Y2-, Y3+, Y3-,
Z1+, Z1-, Z2+, Z2-, Z3+, Z3-
The genotype is then a sequence of these moves.
Genotype
There are two possibilites:
variable-length genotype
fixed-length genotype
Variable-length genotype follows the idea that we don't know, in advance, how many moves will the particular configuration take to solve. I'll come back to this variant later.
Fixed-length genotype can be used too. Even though we don't know how many moves will the particular configuration take to solve, we know that any configuration can be solved in 20 moves or less. Since 20 would mean that for some positions the algorithm would be forced to find the optimal solution (which could be quite hard), we can set the genotype length to, say, 50 to give the algorithm some flexibility.
Fitness
You need to find a good fitness measure for judging the quality of the solutions. Currently I can't think of a nice quality measure as I'm no expert on Rubik's cubes. However, you probably should incorporate the number of moves in your fitness measure. I'll come back to it a little bit later.
Also you should decide whether you are looking for any solution or for a good one. If you are looking for any solution, you can stop at the first solution found. If you are looking for a good solution, you don't stop at first solution found but instead you then optimize its length. You then stop when you decide to stop (i.e. after a desired amount of time spent searching).
Operators
The two basic operators - crossover and mutation - can be basically identical to classical GA. The only difference is that you don't have two states for a "bit" but 18. Even when using a variable-length genotype, the crossover can remain the same - you just cut the both genotypes in two pieces and swap the tails. The only difference from the fixed-length case is that the cuts won't be at the same positions, but rather independent of each other.
Bloat
Using the variable-length genotypes brings an unpleasant phenomenon, which is an excessive growth of the size of the solution, without a big impact on fitness. In Genetic Programming (which is quite different topic) this is called bloat. This can be controlled by two mechanisms.
The first mechanism I already mentioned - incorporate the length of the solution into the fitness. If you look for a good solution (i.e. you don't stop at finding the first one), the length is, of course, only the length of the effective part (i.e. the number of moves from the start up to the move that finishes the cube), not counting the rest.
The other mechanism I borrow from Grammatical Evolution (a Genetic Programming algorithm that also uses linear, variable-length genotypes), which is pruning. A pruning operator takes a solution and deletes the non-effective part of the genotype.
Other possibilites
You could also evolve something like a "policy" or strategy - a general rule that would, given a cube, decide what move to do next. That is far more difficult task, but definitely evolvable (meaning that you can use evolution to try to find it, not that you will find it eventually). You might use e.g. neural networks as a representation of the policy and use some neuro-evolution concepts and frameworks to achieve this task.
Or you could evolve a heuristic (using Genetic Progamming) for a given search algorithm, e.g. A*. The A* algorithm needs a heuristic to estimate the distance of a state to the final state. The more accurate this heuristic is, the faster the A* algorithm finds the solution.
Final remarks
From my experience, if you know something about the problem (which in case of Rubik's cube you do know), it is far better to use a dedicated or at least an informed algorithm to solve the problem rather than using an almost blind one like GA. In my opinion, Rubik's cube is not a good problem for GAs, or rather GAs are not a good algorithms for solving Rubik's cube.
I have done a lot of experiments and I have found that such chromosome representation is good enough, but genetic algorithm gets trapped in local optimum. Solution of the Rubik's cube is highly combinatorial and genetic algorithms are not strong enough to be used for such problem.
A post doc at my university wrote his thesis about this topic and solved it basically.
As far as I remember, he used some combined moves as operators.
https://link.springer.com/chapter/10.1007/978-3-642-12239-2_9
Nail El-Sourani, Sascha Hauke, Markus Borschbach
Solutions calculated by Evolutionary Algorithms have come to surpass
exact methods for solving various problems. The Rubik’s Cube
multiobjective optimization problem is one such area. In this work we
present an evolutionary approach to solve the Rubik’s Cube with a low
number of moves by building upon the classic Thistlethwaite’s
approach. We provide a group theoretic analysis of the subproblem
complexity induced by Thistlethwaite’s group transitions and design an
Evolutionary Algorithm from the ground up including detailed
derivation of our custom fitness functions. The implementation
resulting from these observations is thoroughly tested for integrity
and random scrambles, revealing performance that is competitive with
exact methods without the need for pre-calculated lookup-tables.

TLE in Foe Pairs (Educational Codeforces Round 10)

On implementing O(N+M) complexity code for Foe Pairs problem
http://codeforces.com/contest/652/problem/C, I am getting TLE in Test Case 12.
Constraint : (1 ≤ N, M ≤ 3·105)
I am not getting, why for this constraint O(N+M) is getting TLE.
Here, is the code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
std::vector<int> v(n+1);
for (int i = 0; i < n; ++i)
{
int x;
cin>>x;
v[x] = i;
}
std::vector<int> dp(n,0);
for (int i = 0; i < m; ++i)
{
int a,b;
cin>>a>>b;
if(v[a]>v[b])
swap(a,b);
dp[v[b]] = max(dp[v[b]], v[a]+1);
}
for (int i = 1; i < n; ++i)
{
dp[i] = max(dp[i], dp[i-1]);
}
long long s = 0;
for (int i = 0; i < n; ++i)
{
s+=(i+1-dp[i]);
}
cout<<s;
}
Is there anything, I am missing?
I changed all cin to scanf, it passed all test cases : http://codeforces.com/contest/652/submission/17014495
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,m;
scanf("%d%d", &n, &m);
//cin>>n>>m;
std::vector<int> v(n+1);
for (int i = 0; i < n; ++i)
{
int x;
//cin>>x;
scanf("%d", &x);
v[x] = i;
}
std::vector<int> dp(n,0);
for (int i = 0; i < m; ++i)
{
int a,b;
//cin>>a>>b;
scanf("%d%d", &a, &b);
if(v[a]>v[b])
swap(a,b);
dp[v[b]] = max(dp[v[b]], v[a]+1);
}
for (int i = 1; i < n; ++i)
{
dp[i] = max(dp[i], dp[i-1]);
}
long long s = 0;
for (int i = 0; i < n; ++i)
{
s+=(i+1-dp[i]);
}
cout<<s;
return 0;
}
You should always try to use scanf when the amount of input is large as it is faster.
You can read more about scanf being faster here : Using scanf() in C++ programs is faster than using cin?

ArrayList Method Returns a null ArrayList, main program cannot access

I'm trying to create a basic function that calls on a method that creates the 2D ArrayList that will be used further in the main program to do things like calculate the row and column sums as well as print out the triangle.
However, after it runs the ArrayList returns null. What's going on?
Thanks,
public class Trib
{
private ArrayList<ArrayList<Integer>> triangle;
private int Asize;
public Trib (int size)
{
// convert the argument to type 'int' to be used in the program
Asize = size;
// create an ArrayList of ArrayLists, it will have 'size' number ArrayLists contained within
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>(size);
// create the inner ArrayLists
for (int i = 0; i < size; i++)
{
// add to index 'i' of our ArrayList a new ArrayList of size (i+1)
triangle.add(new ArrayList<Integer>(i+1));
for (int j = 0; j <= i; j++)
{
if (j==0 || j == i)
{
triangle.get(i).add(1);
}
else
triangle.get(i).add(triangle.get(i-1).get(j-1)+triangle.get(i-1).get(j));
System.out.print(triangle.get(i).get(j) + " ");
}
System.out.println();
}
triangle.clone();
}
public void printTriangle()
{
System.out.print(triangle.get(1).get(1));
/*for (int i = 0; i < Asize; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(triangle.get(1).get(1) + " ");
}
System.out.println();
}*/
}
/*public Trib()
{
this(5);
}*/
/*public int Psize()
{
return triangle.size();
}
public ArrayList<Integer> sumRows()
{
ArrayList<Integer> row_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
row_sum.add(triangle.get(i).get(j));
}
}
return row_sum;
}
public ArrayList<Integer> sumCols()
{
ArrayList<Integer> col_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
col_sum.add(triangle.get(i).get(i));
}
}
return col_sum;
}*/
public static void main(String[] args)
{
if(args.length < 1)
{
System.err.println("Sorry, this program needs an integer argument.");
System.exit(1);
}
Trib pt = new Trib(Integer.parseInt(args[0]));
pt.printTriangle();
//ArrayList<Object> sum_rows = new ArrayList<Object>(pt.Psize());
// sum_rows.add;
System.out.println("\nHere are the sum of rows:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.println(sum_rows.get(i));
//ArrayList<Integer> sum_cols = new ArrayList<Integer>(pt.Psize());
System.out.println("\nHere are the sum of columns:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.printf("%-5d", sum_cols.get(i));
}
}
Watch out what's what you are doing: Notice that you have TWO variables named "triangle": The first one is an instance variable and the second is a local variable, which is the only one you have initialized.
My suggestion to avoid this common mistake is to pre-pend "this." to any use of what you intend must be an instance variable. And, if in doubt, if you use a development environment as Eclipse, press CTRL and click on your variable to navigate to the point where it is declared.

image segmentation based on colors using k means, opencv [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to
1) Read a 3 channel image
2) Using K-Means method, create k different classes
3) I will tag pixels accordingly to show which class they belong to and store them in a matrix.
4) And after that I'm thinking using Connected Component Labeling to determine if they are in the same segment or not.
I'm new in opencv so I wanted to ask you for the implementation , some code snippet to to get me started.
Any help will be appreciated. Thank you
typedef struct {
int red;
int green;
int blue;
int b64;
int groupNo;
} IMAGE;
typedef struct _MEANS
{
int r, g, b;
int groupNo;
} point_t, *point;
Mat img = imread("C:\\253027.jpg",CV_LOAD_IMAGE_COLOR);
int clusterCount=5;
IMAGE **strct= {0};
strct = allocate_matrix(img.rows, img.cols);
int i,j,k;
if(! img.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
for ( i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
strct[i][j].red = img.at<Vec3b>(i,j)[2];
strct[i][j].green = img.at<Vec3b>(i,j)[1];
strct[i][j].blue = img.at<Vec3b>(i,j)[0];
/* printf("%u",strct[i][j].red);
printf("%u",strct[i][j].green);
printf("%u",strct[i][j].blue);
*/
}
}
double *tempuz= (double *)malloc(sizeof(double) * clusterCount);
point_t* p = (point_t *)malloc(sizeof(point_t) * clusterCount);
for( k = 0; k < clusterCount; k++ )
{
p[k].r= rand()%255;
p[k].g=rand()%255;
p[k].b=rand()%255;
p[k].groupNo=k;
}
int max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
// cout<<"tempuz"<<tempuz[k]<<endl;
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
// cout<<"grup no"<<strct[i][j].groupNo<<endl;
}
}
i=0;
int r=0,b=0,g=0,counter=0;
while(i<200){
r=0,b=0,g=0,counter=0;
for(k=0;k<clusterCount;k++){
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
r+=strct[i][j].red;
b+=strct[i][j].blue;
g+=strct[i][j].green;
counter++;
}
}
}
if(counter!=0){
p[k].r=r/counter;
p[k].b=b/counter;
p[k].g=g/=counter;
}
}
i++;
max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
}
}
}//end of while
for(int k=0;k<clusterCount;k++){
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
strct[i][j].red=p[k].r;
strct[i][j].blue=p[k].b;
strct[i][j].green=p[k].r;
}
}
}
}
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
img.at<Vec3b>(i,j)[2] = strct[i][j].red;
img.at<Vec3b>(i,j)[1] = strct[i][j].green;
img.at<Vec3b>(i,j)[0] = strct[i][j].blue;
}
}

CUDA optimization question

Here's a simple program:
void multiply(const int* v_in, const int* w_in, int n_v, int n_w, int* w_out)
{
for(int i=0; i<n_w; i++)
{
int sum=0;
for(int j=0; j<n_v; j++)
sum += (w_in[i]*v_in[j])>>1;
w_out[i]=sum;
}
}
Presume n_v, n_w ~10^6. Clearly, there's at least a dozen equivalent ways to do this in CUDA, with different ways to subdivide (n_v*n_w) operations into threads, with and without shared memory... Which way should, theoretically speaking, be the fastest?
simplest:
void multiply(const int* v_in, const int* w_in, int n_v, int n_w, int* w_out)
{
int *v = shared; // dynamic
for(int i = block.rank; i < n_w; i += block.size)
{
int w = w_in[i]; // coalesced
int sum=0;
for(int j=0; j<n_v; j += block.size) { // assumption
v[block.rank] = v_in[j+block.rank];
__synch();
for(int k = 0; k < block.size; ++k)
sum += (w*v[k])>>1; //
__synch(); // ouch
}
w_out[i] = sum; // ditto
}
}