Пример #1
0
void GraphEdge::_print(std::ostream& os, const GraphGC &gc) const
{
    // Don't print if we're hidden
    if (hidden())
	return;
    
    // Fetch the regions
    BoxRegion start = from()->region(gc);
    BoxRegion end   = to()->region(gc);

    // Don't print edges with zero length
    if (start <= end)
	return;
    
    BoxPoint startc = start.origin() + (start.space() / BoxPoint(2));
    BoxPoint endc   = end.origin()   + (end.space()   / BoxPoint(2));

    BoxPoint startp = crosspoint (start, endc);
    BoxPoint endp   = crosspoint (end, startc);

    // This should come from gc.edgeGC
    BoxCoordinate line_width = 1;

    if (gc.printGC->isFig()) {
	if (!gc.drawArrowHeads || to()->isHint()) {
	    os << EDGEHEAD1 << line_width;
	    os << EDGEHEAD2 ;
	    os << startp[X] << " " << startp[Y] << " " ;
	    os << endp[X] << " " << endp[Y] << " " ;
	    os << "9999 9999\n" ;
	} else {
	    os << ARROWHEAD1 << line_width;
	    os << ARROWHEAD2 ;
	    os << startp[X] << " " << startp[Y] << " " ;
	    os << endp[X] << " " << endp[Y] << " " ;
	    os << "9999 9999\n" ;
	}
    } else if (gc.printGC->isPostScript()) {
	if (!gc.drawArrowHeads || to()->isHint()) {
	    os << startp[X] << " " << startp[Y] << " " ;
	    os << endp[X] << " " << endp[Y] << " " ;
	    os << line_width << " line*\n";
	} else {
	    os << gc.arrowAngle << " " << gc.arrowLength << " ";
	    os << startp[X] << " " << startp[Y] << " " ;
	    os << endp[X] << " " << endp[Y] << " " ;
	    os << line_width << " arrowline*\n";
	}
    }
}
Пример #2
0
void process() {
    long i, j, k;
    line l, l2;
    point la, lb, c;
    ans = 0;
    p[n] = p[0];
    for (i = 0; i < n; i++)
        for (j = i + 1; j < n; j++) {
            l = midline(p[i], p[j]);
            for (k = i; k != j; k = (k + 1) % n) {
                if (cross(p[k], p[k + 1], l)) {
                    la = crosspoint(straightline(p[k], p[k + 1]), l);
                    break;
                }
            }
            if (k == j) printf("ERROR\n");
            for (k = j; k != i; k = (k + 1) % n) {
                if (cross(p[k], p[k + 1], l)) {
                    lb = crosspoint(straightline(p[k], p[k + 1]), l);
                    break;
                }
            }
            if (k == i) printf("ERROR\n");
            check(la, 'e');
            check(lb, 'e');
            for (k = 0; k < n; k++) 
                if (k != i && k != j) {
                    l2 = midline(p[i], p[k]);
                    if (cross(la, lb, l2)) {
                        c = crosspoint(l2, l);
                        check(c, 'i');
                    }
                }
        }
    printf("%.3lf\n", sqrt(ans));
}
/**
* desc: create a brand new population using the current population
* param: mutationRate - rate int which mutation mill effect children. (e.g. 0.05 = 5%)
* pre-cond: fitness values for current generation must already be set.
*           parents from previous generation must already be set.
* post-cond: current generation will be incremented
* ret: 0 if successful
*     -1 if an error occured
*/
int CGeneticAlgorithm::addNewPopulation(double mutationRate)
{
	// check that generation list has been initialized
	if (mGenerationList == nullptr)
	{
		return -1;
	}

	// create new population node
	PopulationNode* pNode = new PopulationNode;

	// set up node
	pNode->generation = mCurrentGeneration + 1;
	pNode->population = new CPopulation;
	pNode->data = new CPopulationData;
	pNode->next = nullptr;
	pNode->prev = nullptr;

	// initialize each organism in the population
	for (int i = 0; i < LIST_SIZE; ++i)
	{
		COrganism* pChild = new COrganism;
		OrganismParameters* pParam = new OrganismParameters;
		std::string childString;
		int crosspnt = crosspoint((mParent1->getBinaryString()).length());

		// create new child and check for errors
		if (createChild(mParent1->getBinaryString(), 
		                mParent2->getBinaryString(), 
		                childString, crosspnt) < 0)
		{
			return -1;
		}

		// mutate the child string
		std::string mutatedChild = mutateString(childString, mutationRate);

		// set up new child to be added to population
		pChild->setBinaryString(mutatedChild);
		pChild->setParameters(pParam);
		if (pChild->ConvertStringToParameters() < 0)
		{
			return -1;
		}

		// add child to population
		if (pNode->population->addOrganism(i, pChild) < 0)
		{   // failed to add organism
			return -1;
		}
	}

	// add new generation to end of population list
	PopulationNode* current = mGenerationList;

	// TODO: add tail pointer to last node in GeneticAlgorithm class
	while (current != nullptr)
	{
		// check if current generation matches current
		if ((current->generation) == (pNode->generation))
		{   // not a unique generation id
			return -1;
		}

		// check if at end of list
		if (current->next == nullptr)
		{   // add new node to list
			current->next = pNode;
			current->prev = current;
			current = nullptr;  // set NULL to end loop
		}
		else
		{   // move to next node
			current = current->next;
		}
	}

	// new generation added correctly, so increment
	incrementGeneration();
	return 0;
}