Beispiel #1
0
bool MIQPSolver::addLink(int num, int a, int b, const ContigLink &link)
{
	// See paper for formulae
	bool e = link.EqualOrientation;
	bool r = link.ForwardOrder;
	double mu = link.Mean;
	double sigma = link.Std;
	double w = link.Weight;
	bestObjective += w;
	optimized[a] = optimized[b] = true;

	if (!addDistanceConstraint(a, b, e, r, sigma, mu))
		return false;
	if (!addOrderConstraint(a, b, e, r))
		return false;
	if (!appendOrientationObjective(a, b, e, w))
		return false;
	if (!appendDistanceObjective(a, b, e, w, xi_f[num], xi_r[num]))
		return false;
	if (!appendOrderObjective(a, b, e, w, delta_f[num], delta_r[num]))
		return false;
	return true;
}
Beispiel #2
0
int main(int argc, char * argv[]) {
	int i,j;

	time(&initial);
	
	srand(SEED);
	
	/* Default values */
    outFile = stdout;
	maxAlpha = 2;
	maxIter = 100;
	maxTime = 30;
	randomSeed = SEED;
	simpleOutput = 0;
	/* Read arguments */
	if( argc > 7 )
		argc = 7;
	switch(argc) {
	case 7:
		simpleOutput = atoi(argv[6]);
	case 6:
		if( !(randomSeed = atoi(argv[5])) )
			leave(argv[0]);
	case 5:
		if( !(maxTime = atoi(argv[4])) )
			leave(argv[0]);
	case 4:
		if( !(maxIter = atoi(argv[3])) )
			leave(argv[0]);
	case 3:
		if( !(maxAlpha = atoi(argv[2])) )
			leave(argv[0]);
	case 2:
		if( simpleOutput ) {
            if( !(outFile = fopen(argv[1],"a")) )
				leave(argv[0]);
			break;
		}
		if( !(outFile = fopen(argv[1],"w")) )
			leave(argv[0]);
	}
	
	readInput(stdin);
	
	/* Initiate positions */
	for( i = 0 ; i < n ; ++i ) {
   		pOrd[i].ideal = planes[i].ideal;
  		pOrd[i].pos = i;
	}
	qsort (pOrd, n, sizeof(struct planeOrder), compIdealT);
	for( i = 0 ; i < n ; ++i ) {
  		planes[pOrd[i].pos].pos = i;
	}

	/* Create lp instance */
	glp_prob * Prob;
	Prob = glp_create_prob();
	glp_set_prob_name(Prob, "Airplane Landing Problem");
	glp_set_obj_name(Prob, "Cost");
	
	/* Create basic constraints */
	for( i = 0 ; i < n ; ++i ) {
        addBasicRestriction(Prob,i);
	}
	
	glp_create_index(Prob);
	
	/* Create separation constraints and order variables (&ij) if necessary */
	for( i = 0 ; i < n ; ++i ) {
		for( j = i+1 ; j < n ; ++j ) {
			if( planes[i].latest >= planes[j].earliest &&
			    planes[j].latest >= planes[i].earliest ) {
                addOrderConstraint(Prob,i,j);
			} else if ( planes[i].latest < planes[j].earliest &&
						planes[i].latest + planes[i].sep[j] >= planes[j].earliest ) {
                addSeparationConstraint(Prob, i, j);
			} else if ( planes[j].latest < planes[i].earliest &&
						planes[j].latest + planes[j].sep[i] >= planes[i].earliest ) {
                addSeparationConstraint(Prob, j, i);
			}
		}
	}

	/* Write problem in MPS format so glpsol can (try to) solve it */
	glp_write_mps(Prob, GLP_MPS_FILE, NULL,"mpsProblem.txt");
	
	glp_delete_index(Prob);
	glp_create_index(Prob);
	
	/* GRASP */
	
	/* Data to handle glp solving, time checking and solution generating */
	glp_smcp * param = malloc(sizeof(glp_smcp));
	glp_init_smcp(param);
	param->msg_lev = GLP_MSG_ERR;
	int solution[MAXSIZE], timeAux[MAXSIZE], t;
	double currResult = DBL_MAX, bestResult = DBL_MAX;
	alpha = 0;
	time_t start, curr;
	time(&start);
	
	for( t = 0 ; t < maxIter ; ++t ) {
		/* Greedy solution generation */
		while(createSolution(solution,timeAux,0))
			alpha = n;
		
		/* Building the right constraints */
		mapSolution(Prob,solution);
		
		/* Solving with glpsol */
		param->presolve = GLP_ON;
		glp_simplex(Prob,param);
		param->presolve = GLP_OFF;
		currResult = glp_get_obj_val(Prob);
		
		/* Local search using the first increase */
		for( i = 0 ; i < n-1 ; ++i ) {

			/* Swap two adjacent planes */
			swapConstraint(Prob,i,solution,0);
			glp_simplex(Prob,param);
			
			/* Check for improvements */
			if( GLP_OPT == glp_get_status(Prob) && glp_get_obj_val(Prob) < currResult ) {
				
				currResult = glp_get_obj_val(Prob);
				
				/* Changing the solution */
				int swp;
				swp = solution[i];
				solution[i] = solution[i+1];
				solution[i+1] = swp;
				
				/* Restarting */
				i = -1;
			} else
				swapConstraint(Prob,i,solution,1);
		}
		
		/* Checking improvements */
		if( bestResult > currResult ) {
		    bestResult = currResult;
		    for( i = 0 ; i < n ; ++i )
				planes[solution[i]].pos = i;
		}
		
		/* Choosing alpha */
		alpha = rand()%(maxAlpha+1);
		
		/* Is our time up? */
		time(&curr);
		if( difftime(curr,start) > maxTime )
		    break;
	}
	
	/* Print Answer */
	printResult(Prob, stdout);
	if( outFile ) {
		printResult(Prob, outFile);
		fclose(outFile);
	}

	return 0;
}