Example #1
0
    int solve(vector<int> scores){
        int vertexCnt = scores.size() + 1;
        if(vertexCnt == 2){
            return scores[0];
        }

        vector<Node> graph(vertexCnt);
        buildChain(graph);

        int maxDegree = vertexCnt - 1;
        int currentMaxDegree = 2;
        int currentMaxScore = calcScore(scores, graph);

        while(currentMaxDegree < maxDegree){
            vector<Node*> exceptions;
            Node* nextAddedTarget = findNode(graph, currentMaxDegree);
            while(nextAddedTarget != NULL){
                exceptions.push_back(nextAddedTarget);
                Node* nextDepartedTarget = findNotConnected1DegreeNode(graph, exceptions);
                if(nextDepartedTarget != NULL){
                    departNode(graph, nextDepartedTarget);
                    connectNode(nextAddedTarget, nextDepartedTarget);
                    currentMaxScore = max(currentMaxScore, calcScore(scores, graph));
                    nextAddedTarget = findNode(graph, currentMaxDegree);
                }else{
                    break;
                }
            }
            currentMaxDegree++;
        }
        return currentMaxScore;
    }
void drawWinnerBlur()
{
	drawRectangle(width,height);
	drawComponents();
	drawBoundary(width,height);
	calcScore();

	drawLeftBlock(x[1],y[1],height);
	drawRightBlock(x[2],y[2],height);
	drawBall(x[0],y[0],radius,radius);

	if(scoreRight==7)
	{
		glColor4d(1,1,1,0.7);
		glBegin(GL_POLYGON);
		glVertex2d(width/2,0);
		glVertex2d(width,0);
		glVertex2d(width,height);
		glVertex2d(width/2,height);
		glEnd();

		glColor4d(0,0,0,0.7);
		glBegin(GL_POLYGON);
		glVertex2d(0,0);
		glVertex2d(width/2,0);
		glVertex2d(width/2,height);
		glVertex2d(0,height);
		glEnd();
	}else
	{
		glColor4d(1,1,1,0.7);
		glBegin(GL_POLYGON);
		glVertex2d(0,0);
		glVertex2d(width/2,0);
		glVertex2d(width/2,height);
		glVertex2d(0,height);
		glEnd();

		glColor4d(0,0,0,0.7);
		glBegin(GL_POLYGON);
		glVertex2d(width/2,0);
		glVertex2d(width,0);
		glVertex2d(width,height);
		glVertex2d(width/2,height);
		glEnd();
	}
}
Example #3
0
void ib::Completer::completeCommand(std::vector<ib::CompletionValue*> &candidates, const std::string &value){ // {{{
  auto controller = ib::Singleton<ib::Controller>::getInstance();
  method_command_->beforeMatch(candidates, value);
  double score;

  if(candidates.size() == 0){
    for(auto &pair : controller->getCommands()) {
      pair.second->setScore(0.0);
      score = method_command_->match(pair.second->getName(), value);
      if(score > -1) {
        pair.second->setScore(score);
        candidates.push_back(pair.second);
      }
    }
  }else{
    for(auto it = candidates.begin(); it != candidates.end();){
      auto base_command = dynamic_cast<ib::BaseCommand*>(*it);
      if(base_command != nullptr){
        base_command->setScore(0.0);
        score = method_command_->match(base_command->getName(), value);
        if(score > -1){
          base_command->setScore(score);
          ++it;
          continue;
        }
      }
      it = candidates.erase(it);
    }
  }

  auto history = ib::Singleton<ib::History>::getInstance();
  const auto average = history->getAverageScore();
  const auto se      = history->calcScoreSe();
  const auto hfactor  = ib::Singleton<ib::Config>::getInstance()->getHistoryFactor();
  const auto rfactor  = 1 - hfactor;

  for(const auto &candidate : candidates) {
    auto *base_command = dynamic_cast<ib::BaseCommand*>(candidate);
    if(base_command != nullptr) {
      const auto hist_score = history->calcScore(base_command->getName(), average, se);
      base_command->setScore(base_command->getScore()*rfactor + hist_score * hfactor);
    }
  }

  method_command_->afterMatch(candidates, value);
} // }}}
vector<char> Chromosome::generateGRCSolution(int size){
	_solution = vector<char>(size);

	int firstPosition = rand() % size;
	_solution[firstPosition] = 1;

	calcScore();

	//swap half of the bits
	for (int selectedCount = 1; selectedCount < size / 2; selectedCount++){
		int toSelect = size - selectedCount;

		vector<Candidate> candidates(toSelect);

		int canId = 0; // candidate id for the array, may be faster than vectors
		for (int i = 0; i < size; ++i) {
			if (_solution[i] == 0) {
				int connections = _scoreContribution[i];

				candidates[canId]._connections = connections;
				candidates[canId]._score = connections;
				candidates[canId]._id = i;

				canId++;
			}
		}

		sort(candidates.begin(), candidates.end(), [](const Candidate & a, const Candidate & b) {return a._score > b._score; });

		double lowestConnectionCount = candidates[0]._score;

		int partSize;
		for (partSize = 1; partSize < toSelect; partSize++){
			if (lowestConnectionCount > candidates[partSize]._score){
				break;
			}
		}
		int addId = rand() % partSize;

		flipNodeAtIdx(candidates[addId]._id);
		//_solution[candidates[addId]._id] = 1;
	}

	_score = accumulate(_scoreContribution.begin(), _scoreContribution.end(), 0) / 2;
	return _solution;
}
Example #5
0
void Network::suggestFriends(string name) {
    int id= get_id(name);
    int fptr, ffptr;
    for(int i=0; i<users.size(); i++)
        users[i].added=0;
    vector<int> strong;
    vector<int> scores;
    int maxScore=0;
    for(int i=0; i<users[id].friends.size(); i++) {
        fptr= users[id].friends[i];
        f:for(int j=0; j<users[fptr].friends.size(); j++) {
            ffptr= users[fptr].friends[j];
            if(ffptr==id)
                continue;
            int skip=0;
            for(int k=0; k<users[id].friends.size(); k++) {
                if(users[id].friends[k] == ffptr) {
                    skip=1;
                    break;
                }
            }
            if(skip)
                continue;
            if(users[ffptr].added==0) {
                users[ffptr].added=1;
                strong.push_back(ffptr);
            }
        }
    }//Find all strong users and add to a vector
    if(strong.empty()) {
        cout<<"No suggestions for "<<name<<"."<<endl;
        return;
    }//No suggestions
    for(int i=0; i<strong.size(); i++) {
        scores.push_back(calcScore(id, strong[i]));
        if(scores[i]>maxScore)
            maxScore= scores[i];
    }//calculate each user's score
    cout<<"The strongest candidate(s) is/are:"<<endl;
    for(int i=0; i<strong.size(); i++) {
        if(scores[i]==maxScore)
            cout<<left<<setw(20)<<*(users[strong[i]].getName())<<endl;
    }//print suggestions
    cout<<"They scored "<<maxScore<<endl;
}
Chromosome::Chromosome(int size, Chromosome::GenerationType gt)
{
	switch (gt)
	{
	case Chromosome::OPTIMAL:
		_solution = generateOptimalSolutionForAssignmentData(size);
		break;
	case Chromosome::RANDOM:
		_solution = generateRandomSolution(size);
		break;
	case Chromosome::GREEDY:
	default:
		_solution = generateGRCSolution(size);
		return;
		//_solution = Chromosome::GRCsolution();
		break;
	}
	
	_score = calcScore();
}
Example #7
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  eval
 *  Description:  The entrance to eval the world  
 * =====================================================================================
 */
int eval ( ResultRecord* output, int count, int interval, int i ) {
	TraceNode input[MAXRECORD];
	ResultRecord answer[MAXRECORD];
	char ansfile[1024];
	float anscount;
	FILE *result = fopen("E:\\zhanlj\\out\\result", "w");
	double score;
	
	sprintf(ansfile, "E:\\zhanljData\\GPSData\\output_%ds\\output_%d.txt", interval, i);
	
	anscount = readOutput( answer, ansfile );
	score = calcScore(output, answer, count, 1, result);

	/*printf("count of input: %d\n", readInput( input, "/home/geohpc/mapmatch/training_data/input/input_01.txt" ));*/
	/*printf("count of output: %d\n", readOutput( output, "/home/geohpc/mapmatch/training_data/output/output_02.txt" ));*/
	printf("score is: %f\n", score);
	fprintf(result, "score is: %f\n", score);

	fclose(result);
	return 0;
}				/* ----------  end of function eval ---------- */
Example #8
0
void drawPauseSpace()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0,0,0,0);
	drawRectangle(width,height);

	drawComponents();
	drawBoundary(width,height);
	calcScore();

	drawLeftBlock(x[1],y[1],height);
	drawRightBlock(x[2],y[2],height);
	drawBall(x[0],y[0],radius,radius);

	glColor4d(1,1,1,0.7);
	glBegin(GL_POLYGON);
	glVertex2d(-50,-50);
	glVertex2d(width+50,-50);
	glVertex2d(width+50,height+100);
	glVertex2d(-50,height+100);
	glEnd();
}
Example #9
0
// class Completer {{{
void ib::Completer::completeHistory(std::vector<ib::CompletionValue*> &candidates, const std::string &value){ // {{{
  method_history_->beforeMatch(candidates, value);

  auto history = ib::Singleton<ib::History>::getInstance();

  const auto &commands = history->getOrderedCommands();
  const auto average = history->getAverageScore();
  const auto se     = history->calcScoreSe();
  std::map<std::string, bool> found;
  for(auto it = commands.rbegin(), last = commands.rend(); it != last; ++it){
    auto cmd = (*it);
    if(method_history_->match(cmd->getPath(), value) > -1){
      if(found.find(cmd->getPath()) == found.end()){
        cmd->setScore(history->calcScore(cmd->getPath(), average, se));
        candidates.push_back(cmd);
      }
      found[cmd->getPath()] = true;
    }
  }

  method_history_->afterMatch(candidates, value);

} // }}}
Example #10
0
main(int argc, char * argv[]) {
	int unit4_test_trials = 10000;
	
	if (argc > 1) {
		unit4_test_trials = atoi(argv[1]);
		if (unit4_test_trials < 1) {
			printf("Usage: unittest3 <trials>\r\n");
			exit(1);
		}
	}
	
	srand(time(NULL));
	
	//new gameState
	struct gameState * gs = malloc(sizeof(struct gameState));
	struct gameState * stateCopy = malloc(sizeof(struct gameState));
	
	int i;
	int trial;
	int returnValue;
	int numberOfErrors = 0;
	int playerNum;
	
	printf("Unit test 4\r\n");
	printf("Conducting %d random trials.\r\n", unit4_test_trials);
	
	for (trial = 0; trial < unit4_test_trials; trial++) {
		fuzzState(gs);

		//semi-randomize inputs (within reason)
		playerNum = randomNumber(2,MAX_PLAYERS);
		gs->deckCount[playerNum] = randomNumber(0,MAX_DECK);
		gs->handCount[playerNum] = randomNumber(0,MAX_HAND);
		gs->discardCount[playerNum] = randomNumber(0,MAX_DECK);
		
		for (i = 0; i < gs->deckCount[playerNum]; i++) {
			gs->deck[playerNum][i] = randomNumber(0, treasure_map);
		}
		
		for (i = 0; i < gs->handCount[playerNum]; i++) {
			gs->hand[playerNum][i] = randomNumber(0, treasure_map);
		}
		
		for (i = 0; i < gs->discardCount[playerNum]; i++) {
			gs->discard[playerNum][i] = randomNumber(0, treasure_map);
		}
		
		
		//for later comparison
		memcpy(stateCopy, gs, sizeof(struct gameState));
		
		//perform function
		returnValue = scoreFor(playerNum, gs);

		//compare states
		if (memcmp( gs, stateCopy, sizeof(struct gameState)) != 0) {
			printf("Modification detected in state!\r\n");
			numberOfErrors++;
		} else if (returnValue != -9999) {
		// Run check
			if (calcScore(playerNum, gs) != returnValue) {
				printf("Mismatch in score calculations.  scoreFor(): %d, Expected: %d\r\n", returnValue, calcScore(playerNum, gs));
				numberOfErrors++;
			}
		}
	}	
	
	printf("Unit Test 4 Complete\r\n");
	printf("Number of errors found: %d\r\n", numberOfErrors);
	

	free(gs);
	free(stateCopy);
	return 0;
}
 void recompute() {
   score = calcScore();
   convex = isLeft(prev, this, next);
   failed = false;
 }
Example #12
0
void Board::rearange()
{
  // resize the array with the number of row
  balls.resize(all_indices.size());
  // for each one
  for(auto &b : all)
  {
    // where the row location of this ball ?
    int index = std::distance(all_indices.begin(), all_indices.find(b.point.y));
    balls[index].push_back(&b);
  }

  // determine if the order of ball for each rows
  for(auto &b : balls)
  {
    std::sort(b.begin(), b.end(),
              [](Ball const* a, Ball const* b){return a->point.x < b->point.x;});
    if(b.size() > 1)
    {
      for(int i=0;i<b.size()-1;++i)
      {
        // this ball is close to the current ?
        if(distance(b[i]->point, b[i+1]->point) <= radius*2)
        {
          b[i]->right = b[i+1];
          b[i+1]->left = b[i];
        }
      }
    }
  }

  // create link with up/down balls (like an hexagon)
  for(int i=0;i<balls.size();++i)
  {
    Ball::PtrList *cur = &balls[i];
    if(i < balls.size()-1)
    {
      Ball::PtrList *next = &balls[i+1];
      for(auto ball : *cur)
      {
        for(auto ball_next : *next)
        {
          if(distance(ball_next->point, ball->point) <= radius*2)
          {
            if(ball_next->is_left_of(ball))
            {
              ball->down_left = ball_next;
            }
            else if(ball_next->is_right_of(ball))
            {
              ball->down_right = ball_next;
            }
          }
        }
      }
    }
    if(i > 0)
    {
      Ball::PtrList *previous = &balls[i-1];
      for(auto ball : *cur)
      {
        for(auto ball_next : *previous)
        {
          if(distance(ball_next->point, ball->point) <= radius*2)
          {
            if(ball_next->is_left_of(ball))
            {
              ball->up_left = ball_next;
            }
            else if(ball_next->is_right_of(ball))
            {
              ball->up_right = ball_next;
            }
          }
        }
      }
    }
  }

  for(auto & row : balls)
  {
    for(auto b : row)
    {
      // the ball is falling down ?
      b->disable = !hasTopParent(b);
      // am i in interessant place ?
      b->count = howSameBallInGroup(b->similar, b, b->type);
    }
  }
  Ball::PtrList done;
  for(auto & row : balls)
  {
    for(auto b : row)
    {
      if(std::find(done.begin(), done.end(), b) != done.end())
        continue ;
      if(b->disable) continue ;
      // compute the score
      b->score = calcScore(b->similar, b);
      for(auto p : b->similar)
      {
        p->score = b->score;
        done.push_back(p);
      }
    }
  }


  int ok = 0;
  for(auto & row : balls)
  {
    for(auto b : row)
    {
        if(b->count > 1)
            ok ++;
    }
  }
  ratio = double(ok) / double(all.size());
}
Example #13
0
void stateRunning()
{
	char c;
	char score[7];
//	if(loopTime==0){
	glClear(GL_COLOR_BUFFER_BIT );
	glClearColor(0,0,0,0);

	drawRectangle(width,height);

	drawComponents();

	///////////////////for Ball///////////////////

	if(y[0]+radius>=500)
	{
    	speedy*=-1; 
    	y[0]=500-radius;
	}
	if(y[0]-radius<=0)
	{
		speedy*=-1; 
    	y[0]=radius;
	}

	if(x[0]-radius-5<=x[1] && x[0]+radius+5>=x[2] )
	{
		if((y[0]>=y[1]-height/10 && y[0]<=y[2]+height/10) || (y[0]>=y[2]-height/10 && y[0]<=y[1]+height/10))
		{
			x[1]=x[0]-2*radius;
			x[2]=x[0]+2*radius;
		}
	}

    if((x[0]-radius>x[1] && x[0]+speedx-radius<x[1]) || (x[0]+radius<x[1]-10 && x[0]+speedx+radius>x[1]-10))
	{
		yi=(speedy/speedx)*(x[1]-x[0])+y[0];
		xi=x[0];
		if(yi<y[1]+height/10 && yi>y[1]-height/10)
		{
			if(x[0]>x[1])
				x[0]=x[1]+radius;
			else
				x[0]=x[1]-radius;
			y[0]=(speedy/speedx)*(x[0]-xi)+y[0];
		}
	}

	if(y[0]-radius<=y[1]+height/10 && y[0]+radius>=y[1]-height/10){
    if((x[0]-radius<=x[1] && x[0]-radius>=x[1]-10))
    {
    	x[0]=x[1]+radius;
    	speedx=abs(speedx);
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }else if(x[0]+radius>=x[1]-10 && x[0]+radius<=x[1])
    {
    	x[0]=x[1]-radius-10;
    	speedx=abs(speedx)*-1;
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }
}
	if(x[0]<=x[1] && x[0]>=x[1]-10){
    if((y[0]-radius<=y[1]+height/10 && y[0]-radius>=y[1]))
    {
    	y[0]=y[1]+height/10+radius;
    	speedy=abs(speedy);
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }else if(y[0]+radius>=y[1]-height/10 && y[0]-radius<=y[1])
    {
    	y[0]=y[1]-height/10-radius;
    	speedy=abs(speedy)*-1;
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }
}

	if((x[0]-radius>x[2]+10 && x[0]+speedx-radius<x[2]+10) || (x[0]+radius<x[2] && x[0]+speedx+radius>x[2]))
	{
		yi=(speedy/speedx)*(x[2]-x[0])+y[0];
		xi=x[0];
		if(yi<y[2]+height/10 && yi>y[2]-height/10)
		{
			if(x[0]>x[2])
				x[0]=x[2]+radius+10;
			else
				x[0]=x[2]-radius;
			y[0]=(speedy/speedx)*(x[0]-xi)+y[0];
		}
	}

	if(y[0]-radius<=y[2]+height/10 && y[0]+radius>=y[2]-height/10){
    if((x[0]+radius>=x[2] && x[0]+radius<=x[2]+10))
    {
    	x[0]=x[2]-radius;
    	speedx=abs(speedx)*-1;
    	speedx+=xspeeder;
    	speedy+=yspeeder;
    	
    }else if(x[0]-radius<=x[2]+10 && x[0]-radius>=x[2])
    {
    	x[0]=x[2]+10+radius;
    	speedx=abs(speedx);
    	speedx+=xspeeder;
    	speedy+=yspeeder;

    }
}

if(x[0]>=x[2] && x[0]<=x[2]+10){
    if((y[0]-radius<=y[2]+height/10 && y[0]-radius>=y[2]))
    {
    	y[0]=y[2]+height/10+radius;
    	speedy=abs(speedy);
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }else if(y[0]+radius>=y[2]-height/10 && y[0]-radius<=y[2])
    {
    	y[0]=y[2]-height/10-radius;
    	speedy=abs(speedy)*-1;
    	speedx+=xspeeder;
    	speedy+=yspeeder;    	
    }
}

    x[0]=x[0]+speedx;
	y[0]=y[0]+speedy;	

	if(x[0]-radius<=0)
	{
		if((y[0]>=height*3/4 && y[0]<=height) || (y[0]<=height/4 && y[0]>=0))
		{
		speedx*=-1;
		x[0]=radius;
	}
	}

	if(x[0]-radius==0 && x[0]+radius>=x[1]-10)
	{
		if((y[0]>=y[1]-height/10 && y[0]<=y[1]+height/10))
		{
			x[1]=x[0]+3*radius;
		}
	}

	if(x[0]+radius>=width)
	{
		if((y[0]>=height*3/4 && y[0]<=height) || (y[0]<=height/4 && y[0]>=0))
		{
		speedx*=-1;
		x[0]=width-radius;
	}
	}

	if(x[0]+radius==width && x[0]-radius<=x[2]+10)
	{
		if((y[0]>=y[2]-height/10 && y[0]<=y[2]+height/10))
		{
			x[2]=x[0]-3*radius;
		}
	}

	drawBall(x[0],y[0],radius,radius);
   
	/////////////////for Block 1///////////////////////

	if(x[1]<500 && x[1]-10>0){
	if(y[1]+height/10<500 && y[1]-height/10>0)
	{
	if(up[0]==1)
	{
		y[1]+=5;
		yspeeder+=0.1;
    }
    if(down[0]==1)
    {
    	y[1]-=5;
    	yspeeder-=0.1;
    }
    if(left[0]==1)
    {
    	x[1]-=5;
    	xspeeder-=0.1;
    }
    if(right[0]==1)
    {
    	x[1]+=5;
    	xspeeder+=0.1;
    }
}else {
	if(y[1]+height/10>=500)
		y[1]-=6;
	else if(y[1]-height/10<=0)
		y[1]+=6;
}
}else{
	if(x[1]>=500)
		x[1]-=6;
	else if(x[1]-10<=0)
		x[1]+=6;
	
}
    drawLeftBlock(x[1],y[1],height);

    /////////////////////for block 2//////////////////

   if(x[2]+10<1000 && x[2]>500){
	if(y[2]+height/10<500 && y[2]-height/10>0)
	{
	if(up[1]==1)
	{
		y[2]+=5;
		yspeeder+=0.1;
    }
    if(down[1]==1)
    {
    	y[2]-=5;
    	yspeeder-=0.1;
    }
    if(left[1]==1)
    {
    	x[2]-=5;;
    	xspeeder-=0.1;
    }
    if(right[1]==1)
    {
    	x[2]+=5;
    	xspeeder+=0.1;
    }
}else {
	if(y[2]+height/10>=500)
		y[2]-=6;
	else if(y[2]-height/10<=0)
		y[2]+=6;
}
}else{
	if(x[2]<=500)
		x[2]+=6;
	else if(x[2]>=990)
		x[2]-=6;
	
}
	drawRightBlock(x[2],y[2],height); 

	drawBoundary(width,height); 
	
	calcScore();

	loopTime++;	
}
Chromosome::Chromosome(vector<char> & solution)
{
	_solution = solution;
	_score = calcScore();
}
Chromosome::Chromosome(int size)
{
	_solution = Chromosome::GRCsolution();
	_score = calcScore();
}