Ejemplo n.º 1
0
void printMarcel(int x, int y) {
	printM(x,y);
	printA(x+5*SIZE+GAP,y);
	printR(x+8*SIZE+2*GAP,y);
	printC(x+12*SIZE+3*GAP,y);
	printE(x+16*SIZE+4*GAP,y);
	printL(x+19*SIZE+5*GAP,y);
}
Ejemplo n.º 2
0
void printDevina(int x, int y) {
	printD(x,y);
	printE(x+4*SIZE+GAP,y);
	printV(x+7*SIZE+2*GAP,y);
	printI(x+11*SIZE+3*GAP,y);
	printN(x+12*SIZE+4*GAP,y);
	printA(x+16*SIZE+5*GAP,y);
}
Ejemplo n.º 3
0
int main(void)
{
	char str[100] = "";
	printA();
	printB(str);
	printC();
	return 0;
}
Ejemplo n.º 4
0
int main()
{
	A sample(44, 54.888);
	
	printA(sample);


	return 0;
}
Ejemplo n.º 5
0
void printWilliam(int x, int y) {
	printW(x,y);
	printI(x+5*SIZE+GAP,y);
	printL(x+6*SIZE+2*GAP,y);
	printL(x+9*SIZE+3*GAP,y);
	printI(x+12*SIZE+4*GAP,y);
	printA(x+13*SIZE+5*GAP,y);
	printM(x+16*SIZE+6*GAP,y);
}
Ejemplo n.º 6
0
void printGameOver(int x, int y) {
	printG(x,y);
	printA(x+5*SIZE+GAP, y);
	printM(x+8*SIZE+2*GAP, y);
	printE(x+13*SIZE+3*GAP, y);
	printO(x+16*SIZE+5*GAP, y);
	printV(x+20*SIZE+6*GAP, y);
	printE(x+24*SIZE+7*GAP, y);
	printR(x+27*SIZE+8*GAP, y);
}
Ejemplo n.º 7
0
int main(int argc, char *argv[]) {
	
	int ar[N] = {1,4,5,9,13,16,19,28,40,100};
	printA(ar, N -1 );
	printf("Enter a numbber to insert: \n");
	int num;
	scanf("%d", &num);
	int i = 0;
	while (ar[i] < num)
		i++;
	if (i <= 9) {
		for (int k = N - 1; k >= i; k--)
			ar[k + 1] = ar[k];
		ar[i] = num;
	} else 
		ar[10] = num;

	printA(ar, N);

	return 0;
}
Ejemplo n.º 8
0
// Lomuto partitioning scheme
void solve(int n)
{
	int piv = A[0];
	int a = 1;
	for (int b = 1; b < n; b++) {
		if (A[b] < piv) {
			swap(a, b);
			a++;
		}
	}
	swap(0, a - 1);
	printA(n);
}
void floyd(int n)   
{  

    int i, j, k; 
    for(i=0; i<n; i++) 
        for(j=0; j<n; j++) 
            A[i][j]=weight[i][j]; 

        for(k=0; k<n; k++){ 
        for(i=0; i<n; i++) 
               for(j=0; j<n; j++) 
                  if (A[i][k]+A[k][j] < A[i][j]) 
                     A[i][j] = A[i][k]+A[k][j]; 
                printA(n); 
        } 
}
Ejemplo n.º 10
0
void bubbleSort(int *a,int n) {
    int t;
    printf("size %d\n",n);
    if(n <=1) {
        return;
    }

    for (int i=0; i < n-1; i++) { //Feeds value of i to the next loop
        for(int j=0; j < n-1-i; j++) { //Every loop move highest value to the 'right most' location.
            //Loop shrink every time by one element at right , which is sorted now.
            if (a[j] > a[j+1]) {
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
            }
        }
    }
    printA(a,n);

}
Ejemplo n.º 11
0
void main ()
{
    int A[] = {20, 40, 10, 30, 0, 90, 70, 60, 50, 80};
    int i, min, max = 0;

    printA(A);

    /**
     * Loop Invariant: 
     *
     * Initialization: 
     *     . The first iteration of the loop we have the max and min values
     * pointed to the first element of A. If the i element in A is greater 
     * then max element then max become i. Otherwise if the i element in A
     * is less the min element then min become i. The iteration is true 
     * prior to the first iteration. 
     *
     * Maintenance:
     *     . For each iteration the loop invariant is maintained. For i 
     * from 0 to the length of A ([i=0 .. i=9]), if there is a number 
     * less or greater then max or min, it will be assigned to max or min
     * respectively. 
     *
     * Termination:
     *     . The iteration finishes when i becomes A_size that represents
     * the last index inside A. Then, if the entire array A was traversed, 
     * then min and max has the lowest and highest values of the array A.
     *
     */
    for(; i<A_size; i++)
    {
        if(A[i] > A[max])
            max = i;
        else if(A[i] < A[min])
            min = i;
    }

    printf("Min: %d, Max: %d\n", A[min], A[max]);
}
Ejemplo n.º 12
0
int main(int argc, char **argv) {
  test(1);
  printA();
  test(2);
  printA();
}
Ejemplo n.º 13
0
int main()
{
  printA();
  printB();
  return 0;
}
Ejemplo n.º 14
0
int main(){
  /*struct CharachterBuffer test;
  test.defaultBuffer(7);
  char pull;
  printf("empty pull (output riding input) test result: %d\n",test.pull(&pull));
  printf("empty push test result: %d\n",test.push('$'));
  printf("immenent output riding input occupied pull test result: %d\n",test.pull(&pull));
  printf("data integrity check: %c\n",pull);
  printf("output riding input push test result: %d\n",test.push('H'));
  printf("input && output riding nothing push test result: %d\n",test.push('I'));
  printf("input && output riding nothing push test result: %d\n",test.push(' '));
  printf("input && output riding nothing push test result: %d\n",test.push('M'));
  printf("input && output riding nothing push test result: %d\n",test.push('O'));
  printf("input && output riding nothing push test result: %d\n",test.push('M'));
  printf("immenent input riding output push test result: %d\n",test.push('!'));
  printf("input riding output push test result: %d\n",test.push('?'));
  
  
  int flow = 0;
  std::string gather = "";
  while((flow = test.pull(&pull)) != test.ERROR_COLLISION){
    printf("The input int is: %d\n",flow);
    gather += pull;
    if(flow == test.ERROR_COLLISION){
      printf("WHOA, DUDE!");
      break;
    }
  }
  printf("%s\n\n\n\n**********\n\n\n",gather.c_str());
  printf("bufferOutput: %d\n",test.bufferOutputPosition);
  printf("bufferInput: %d\n",test.bufferInputPosition);
  */
  gpsBuffer.defaultBuffer(1024);
  
  setup();
  
  /* //Tests Buffer and getNMEAstr()
  gpsBuffer.push(' ');
  gpsBuffer.push('$');
  gpsBuffer.push('T');
  gpsBuffer.push('E');
  gpsBuffer.push('S');
  gpsBuffer.push('T');
  gpsBuffer.push('\n');
  
  std::string gather="";
  getNMEAstr(&gather);
  printf("%s\n",gather.c_str());*/
  
  
  int overflow = readGPS();
  while( overflow != ERR_SHTAP){
    overflow = readGPS();
  }
  
  //printf(gpsBuffer.buffer);
  
  std::string gather="";

  getNMEAstr(&gather);
  struct nmea_data *gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);



  gather = "";
  getNMEAstr(&gather);
  gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);
  
  gather = "";
  getNMEAstr(&gather);
  gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);
  
  gather = "";
  getNMEAstr(&gather);
  gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);
  
  gather = "";
  getNMEAstr(&gather);
  gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);
  
  gather = "";
  getNMEAstr(&gather);
  gathered_data = new nmea_data(gather);
  printf("%s\n",gather.c_str());
  printA(gathered_data);
  
  
  gather = "";
  
  /*setup();
  
  char input;
  while(1){
    read(serialHandle, &input, 1);
    if(input != 0){
      printf("%c\n",input);
    }
  }*/
  
  
}
Ejemplo n.º 15
0
int main(int argc, const char *argv[])
{
    int N,
		    M,
		    T,
		    maxIters,
		    seed,
		    i,
		    j,
		    iter,
        str_len;

    char **alphabet;

    double logProb,
           newLogProb;

  	double *pi,
           *piBar, 
           **A,
           **Abar,
           **B,
           **Bbar;

    struct stepStruct *step;

    FILE *in,
         *out;
    
    char s[80];

	int wantTraining = 1;
    
    if(argc != 10)
    {
        fprintf(stderr, "\nUsage: %s N M T maxIters filename alphabet modelfile seed\n\n", argv[0]);
        fprintf(stderr, "where N == number of states of the HMM\n");
        fprintf(stderr, "      M == number of observation symbols\n");
        fprintf(stderr, "      T == number of observations in the training set\n");
        fprintf(stderr, "      maxIters == max iterations of re-estimation algorithm\n");
        fprintf(stderr, "      filename == name of input file\n");
        fprintf(stderr, "      alphabet == name of file defining the alphabet\n");
        fprintf(stderr, "      modelfile == name of model output file\n");
        fprintf(stderr, "      seed == seed value for pseudo-random number generator (PRNG)\n\n");
		fprintf(stderr, "      wantTraining == to train enter 1, otherwise 0 \n\n");
        fprintf(stderr, "For example:\n\n      %s 2 10 10000 500 datafile alphabet modelfile 1241\n\n", argv[0]);
        fprintf(stderr, "will create a HMM with 2 states and 10 observation symbols,\n");
        fprintf(stderr, "will read in the first 10000 observations from `datafile',\n");
        fprintf(stderr, "will use the observation symbols defined in file `alphabet', and\n");
        fprintf(stderr, "will write the model (pi, A, B) to `modelfile', and\n");
        fprintf(stderr, "will seed the PRNG with 1241 and train the HMM with a maximum of 500 iterations.\n\n");
        exit(0);
    }

    N = atoi(argv[1]);
    M = atoi(argv[2]);
    T = atoi(argv[3]);
    maxIters = atoi(argv[4]);
    seed = atoi(argv[8]);
	wantTraining = atoi(argv[9]);

    pi = (double *)malloc(N * sizeof(double));
    piBar = (double *)malloc(N * sizeof(double));

    A = (double **)malloc(N * sizeof(double*));
    Abar =static_cast<double **>(malloc(N * sizeof(double*)));
    for (i=0; i<N; ++i)
    {
      A[i] = static_cast<double *>(malloc(N * sizeof(double)));
      Abar[i] = static_cast<double *>(malloc(N * sizeof(double)));
    }

    B = static_cast<double **>(malloc(N * sizeof(double*)));
    Bbar = static_cast<double **>(malloc(N * sizeof(double*)));
    for (i=0; i<N; ++i)
    {
      B[i] = static_cast<double *>(malloc(M * sizeof(double)));
      Bbar[i] = static_cast<double *>(malloc(M * sizeof(double)));
    }
    
   
    ////////////////////////
    // read the data file //
    ////////////////////////

    // allocate memory
    printf("allocating %d bytes of memory... ", (T + 1) * sizeof(struct stepStruct));
    fflush(stdout);
    if((step = static_cast<stepStruct *>(calloc(T + 1, sizeof(struct stepStruct)))) == NULL)
    {
        fprintf(stderr, "\nUnable to allocate alpha\n\n");
        exit(0);
    }
    for (i=0; i<T+1; ++i)
    {
      step[i].alpha = static_cast<double *>(malloc(N * sizeof(double)));
      step[i].beta = static_cast<double *>(malloc(N * sizeof(double)));
      step[i].gamma = static_cast<double *>(malloc(N * sizeof(double)));
      step[i].diGamma = static_cast<double **>(malloc(N * sizeof(double*)));
      for (j=0; j<N; ++j)
      {
        step[i].diGamma[j] = static_cast<double *>(malloc(N * sizeof(double)));
      }
    }
    printf("done\n");

    // read in the observations from file
    printf("GetObservations... ");
    fflush(stdout);
    in = fopen(argv[5], "r"); // argv[5] = filename
    if(in == NULL)
    {
        fprintf(stderr, "\nError opening file %s\n\n", argv[5]);
        exit(0);
    }
    i = 0;
    fgets(s,80,in); // get rid of the first line
    while (i < T)
    {
      fgets(s,80,in);
      step[i].obs = atoi(s);
      ++i;
    }
    fclose(in);
    printf("done\n");

    // read in the alphabet from file
    printf("GetAlphabet... ");
    fflush(stdout);
    alphabet = static_cast<char **>(malloc(M * sizeof (char*)));
    in = fopen(argv[6], "r"); // argv[6] = alphabet
    if(in == NULL)
    {
        fprintf(stderr, "\nError opening file %s\n\n", argv[6]);
        exit(0);
    }
    i = 0;
    fgets(s,80,in); // get rid of the first line
    while (i < M)
    {
      fgets(s,80,in);
	    str_len = strlen(s);
      alphabet[i] = static_cast<char *>(malloc(str_len * sizeof(char)));
      strncpy(alphabet[i], s, str_len-1);
      alphabet[i][str_len-1] = '\0';
      ++i;
    }
    fclose(in);
    printf("done\n");


    /////////////////////////
    // hidden markov model //
    /////////////////////////

    srand(seed);

    // initialize pi[], A[][] and B[][]
    initMatrices(pi, A, B, N, M, seed);

    // print pi[], A[][] and B[][] transpose
    printf("\nN = %d, M = %d, T = %d\n", N, M, T);
    printf("initial pi =\n");
    printPi(pi, N);
    printf("initial A =\n");
    printA(A, N);
    printf("initial B^T =\n");
    printBT(B, N, M, alphabet);

    // initialization
    iter = 0;
    logProb = -1.0;
    newLogProb = 0.0;

	if (wantTraining) {

		// main loop
		while((iter < maxIters) && (newLogProb > logProb))
		{
			printf("\nbegin iteration = %d\n", iter);

			logProb = newLogProb;

			// alpha (or forward) pass
			printf("alpha pass... ");
			fflush(stdout);
			alphaPass(step, pi, A, B, N, T);
			printf("done\n");

			// beta (or backwards) pass
			printf("beta pass... ");
			fflush(stdout);
			betaPass(step, pi, A, B, N, T);
			printf("done\n");

			// compute gamma's and diGamma's
			printf("compute gamma's and diGamma's... ");
			fflush(stdout);
			computeGammas(step, pi, A, B, N, T);
			printf("done\n");

			// find piBar, reestimate of pi
			printf("reestimate pi... ");
			fflush(stdout);
			reestimatePi(step, piBar, N);
			printf("done\n");

			// find Abar, reestimate of A
			printf("reestimate A... ");
			fflush(stdout);
			reestimateA(step, Abar, N, T);
			printf("done\n");

			// find Bbar, reestimate of B
			printf("reestimate B... ");
			fflush(stdout);
			reestimateB(step, Bbar, N, M, T);
			printf("done\n");

	#ifdef PRINT_REESTIMATES
			printf("piBar =\n");
			printPi(piBar, N);
			printf("Abar =\n");
			printA(Abar, N);
			printf("Bbar^T = \n");
			printBT(Bbar, N, M, alphabet);
	#endif // PRINT_REESTIMATES

			// assign pi, A and B corresponding "bar" values
			for(i = 0; i < N; ++i)
			{
				pi[i] = piBar[i];

				for(j = 0; j < N; ++j)
				{
					A[i][j] = Abar[i][j];
				}

				for(j = 0; j < M; ++j)
				{
					B[i][j] = Bbar[i][j];
				}

			}// next i

			// compute log [P(observations | lambda)], where lambda = (A,B,pi)
			newLogProb = 0.0;
			for(i = 0; i < T; ++i)
			{
				newLogProb += log(step[i].c);
			}
			newLogProb = -newLogProb;

			// a little trick so that no initial logProb is required
			if(iter == 0)
			{
				logProb = newLogProb - 1.0;
			}

			printf("completed iteration = %d, log [P(observation | lambda)] = %f\n",
					iter, newLogProb);

			++iter;

		}// end while
    
		out = fopen(argv[7], "w"); // argv[7] = modelfile
		writeModel(pi, A, B, N, M, T, alphabet, out);
		fclose(out);
    
		printf("\nT = %d, N = %d, M = %d, iterations = %d\n\n", T, N, M, iter);
		printf("final pi =\n");
		printPi(pi, N);
		printf("\nfinal A =\n");
		printA(A, N);
		printf("\nfinal B^T =\n");
		printBT(B, N, M, alphabet);
		printf("\nlog [P(observations | lambda)] = %f\n\n", newLogProb);

	} // end of training
	else { //want to do testing


		out = fopen(argv[7], "r"); // argv[7] = modelfile
		readModelFile(pi, A, B, N, M, T, alphabet, out);
		
		// alpha (or forward) pass
		printf("alpha pass... ");
		fflush(stdout);
		alphaPass(step, pi, A, B, N, T);
		printf("done\n");
		printf("logProb %f\n", computeLogProb(step, T)/T);
		
	//	FILE * newFile = fopen("testing.txt", "a");
		//writeModel(pi, A, B, N, M, T, alphabet, newFile);

		//fclose(newFile);

		fclose(out);
	} // end of testing
}// end hmm
Ejemplo n.º 16
0
int main(int argc, char *argv[]) {
   printA();
   printB();

   return 0;
}
Ejemplo n.º 17
0
void printAB()
{
    printA();
	printB();
}
Ejemplo n.º 18
0
/********* end of lrs_getfirstbasis  ***************/
long 
getabasis2 (lrs_dic * P, lrs_dat * Q, lrs_dic * P2orig, long order[])

/* Pivot Ax<=b to standard form */
/*Try to find a starting basis by pivoting in the variables x[1]..x[d]        */
/*If there are any input linearities, these appear first in order[]           */
/* Steps: (a) Try to pivot out basic variables using order                    */
/*            Stop if some linearity cannot be made to leave basis            */
/*        (b) Permanently remove the cobasic indices of linearities           */
/*        (c) If some decision variable cobasic, it is a linearity,           */
/*            and will be removed.                                            */

{
  long i, j, k;
/* assign local variables to structures */
  lrs_mp_matrix A = P->A;
  long *B = P->B;
  long *C = P->C;
  long *Row = P->Row;
  long *Col = P->Col;
  long *linearity = Q->linearity;
  long *redundcol = Q->redundcol;
  long m, d, nlinearity;
  long nredundcol = 0L;		/* will be calculated here */

  static long firsttime=TRUE;
  static long *linindex;

  m = P->m;
  d = P->d;
  nlinearity = Q->nlinearity;

  if(firsttime)
  {
    firsttime = FALSE;
    linindex = calloc ((m + d + 2), sizeof (long));
  }
  else     /* after first time we update the change in linearities from the last time, saving many pivots */
  {
    for(i=1;i<=m+d;i++)
	  linindex[i]=FALSE;
    if(Q->debug)
        fprintf(lrs_ofp,"\nlindex =");
    for(i=0;i<nlinearity;i++)
    {
       	  linindex[d+linearity[i]]=TRUE;
	  if(Q->debug)
             fprintf(lrs_ofp,"  %ld",d+linearity[i]);		   
    }
	  
    for(i=1;i<=m;i++)
    {
	  if(linindex[B[i]])  /* pivot out unwanted linearities */
	  {
		  k=0;
		  while(k<d && (linindex[C[k]] ||  zero (A[Row[i]][Col[k]])))
			  k++;

                  if (k < d)
                  {
	            j=i;   /* note this index changes in update, cannot use i!)*/

		    if(C[k] > B[j])  /* decrease i or we may skip a linearity */
		       i--;
	            pivot (P, Q, j, k);
		    update (P, Q, &j, &k);
                  }
		   else
                  {
                     /* this is not necessarily an error, eg. two identical rows/cols in payoff matrix */
                     if(! zero(A[Row[i]][0]))    /* error condition */
                       {
                         if(Q->debug || Q->verbose)
                              {
                               fprintf(lrs_ofp,"\n*Infeasible linearity i=%ld B[i]=%ld",i,B[i]);
                               if (Q->debug)
                                     printA(P,Q);
                              }
                        return(FALSE);
                       }
                     if(Q->debug || Q->verbose)
                       {
		        fprintf(lrs_ofp,"\n*Couldn't remove linearity i=%ld B[i]=%ld",i,B[i]);		   
                       }
                   }

           } /* if linindex */
    }   /* for i   ..*/
   goto hotstart;
  }

/* standard lrs processing is done on only the first call to getabasis2 */

  if (Q->debug)
    {
      fprintf (lrs_ofp, "\ngetabasis from inequalities given in order");
      for (i = 0; i < m; i++)
	fprintf (lrs_ofp, " %ld", order[i]);
    }
  for (j = 0; j < m; j++)
    {
      i = 0;
      while (i <= m && B[i] != d + order[j])
	i++;			/* find leaving basis index i */
      if (j < nlinearity && i > m)	/* cannot pivot linearity to cobasis */
	{
	  if (Q->debug)
	    printA (P, Q);
#ifndef LRS_QUIET
	  fprintf (lrs_ofp, "\nCannot find linearity in the basis");
#endif
	  return FALSE;
	}
      if (i <= m)
	{			/* try to do a pivot */
	  k = 0;
	  while (C[k] <= d && zero (A[Row[i]][Col[k]]))
	    k++;

	  if (C[k] <= d)
	    {
	      pivot (P, Q, i, k);
	      update (P, Q, &i, &k);
	    }
	  else if (j < nlinearity)
	    {			/* cannot pivot linearity to cobasis */
	      if (zero (A[Row[i]][0]))
		{
#ifndef LRS_QUIET
		  fprintf (lrs_ofp, "\n*Input linearity in row %ld is redundant--skipped", order[j]);
#endif
		  linearity[j] = 0;
		}
	      else
		{
		  if (Q->debug)
		    printA (P, Q);
		  if (Q->verbose)
		    fprintf (lrs_ofp, "\nInconsistent linearities");
		  return FALSE;
		}
	    }			/* end if j < nlinearity */

	}			/* end of if i <= m .... */
    }				/* end of for   */

/* update linearity array to get rid of redundancies */
  i = 0;
  k = 0;			/* counters for linearities         */
  while (k < nlinearity)
    {
      while (k < nlinearity && linearity[k] == 0)
	k++;
      if (k < nlinearity)
	linearity[i++] = linearity[k++];
    }

  nlinearity = i;

/* column dependencies now can be recorded  */
/* redundcol contains input column number 0..n-1 where redundancy is */
  k = 0;
  while (k < d && C[k] <= d)
    {
      if (C[k] <= d)		/* decision variable still in cobasis */
	redundcol[nredundcol++] = C[k] - Q->hull;	/* adjust for hull indices */
      k++;
    }

/* now we know how many decision variables remain in problem */
  Q->nredundcol = nredundcol;
  Q->lastdv = d - nredundcol;

  /* if not first time we continue from here after loading dictionary */

hotstart:

  if (Q->debug)
    {
      fprintf (lrs_ofp, "\nend of first phase of getabasis2: ");
      fprintf (lrs_ofp, "lastdv=%ld nredundcol=%ld", Q->lastdv, Q->nredundcol);
      fprintf (lrs_ofp, "\nredundant cobases:");
      for (i = 0; i < nredundcol; i++)
	fprintf (lrs_ofp, " %ld", redundcol[i]);
      printA (P, Q);
    }

/* here we save dictionary for use next time, *before* we resize */

  copy_dict(Q,P2orig,P);

/* Remove linearities from cobasis for rest of computation */
/* This is done in order so indexing is not screwed up */

  for (i = 0; i < nlinearity; i++)
    {				/* find cobasic index */
      k = 0;
      while (k < d && C[k] != linearity[i] + d)
	k++;
      if (k >= d)
	{
          if(Q->debug || Q->verbose)
           {
	    fprintf (lrs_ofp, "\nCould not remove cobasic index");
           }
          /* not neccesarily an error as eg., could be repeated row/col in payoff */
	}
      else
         { 
              removecobasicindex (P, Q, k);
              d = P->d;
         }
    }
  if (Q->debug && nlinearity > 0)
    printA (P, Q);
/* set index value for first slack variable */

/* Check feasability */
  if (Q->givenstart)
    {
      i = Q->lastdv + 1;
      while (i <= m && !negative (A[Row[i]][0]))
	i++;
      if (i <= m)
	fprintf (lrs_ofp, "\n*Infeasible startingcobasis - will be modified");
    }
  return TRUE;
}				/*  end of getabasis2 */
Ejemplo n.º 19
0
long 
lrs_getfirstbasis2 (lrs_dic ** D_p, lrs_dat * Q, lrs_dic * P2orig, lrs_mp_matrix * Lin, long no_output)
/* gets first basis, FALSE if none              */
/* P may get changed if lin. space Lin found    */
/* no_output is TRUE supresses output headers   */
{
  long i, j, k;

/* assign local variables to structures */

  lrs_mp_matrix A;
  long *B, *C, *Row, *Col;
  long *inequality;
  long *linearity;
  long hull = Q->hull;
  long m, d, lastdv, nlinearity, nredundcol;

  static long ocount=0;


  m = D->m;
  d = D->d;
  lastdv = Q->lastdv;

  nredundcol = 0L;		/* will be set after getabasis        */
  nlinearity = Q->nlinearity;	/* may be reset if new linearity read */
  linearity = Q->linearity;

  A = D->A;
  B = D->B;
  C = D->C;
  Row = D->Row;
  Col = D->Col;
  inequality = Q->inequality;

/* default is to look for starting cobasis using linearies first, then     */
/* filling in from last rows of input as necessary                         */
/* linearity array is assumed sorted here                                  */
/* note if restart/given start inequality indices already in place         */
/* from nlinearity..d-1                                                    */

  for (i = 0; i < nlinearity; i++)      /* put linearities first in the order */
    inequality[i] = linearity[i];


  k = 0;			/* index for linearity array   */

  if (Q->givenstart)
    k = d;
  else
    k = nlinearity;
  for (i = m; i >= 1; i--)
    {
      j = 0;
      while (j < k && inequality[j] != i)
	j++;			/* see if i is in inequality  */
      if (j == k)
	inequality[k++] = i;
    }
  if (Q->debug)
    {
      fprintf (lrs_ofp, "\n*Starting cobasis uses input row order");
      for (i = 0; i < m; i++)
	fprintf (lrs_ofp, " %ld", inequality[i]);
    }

  if (!Q->maximize && !Q->minimize)
    for (j = 0; j <= d; j++)
      itomp (ZERO, A[0][j]);

/* Now we pivot to standard form, and then find a primal feasible basis       */
/* Note these steps MUST be done, even if restarting, in order to get         */
/* the same index/inequality correspondance we had for the original prob.     */
/* The inequality array is used to give the insertion order                   */
/* and is defaulted to the last d rows when givenstart=FALSE                  */

  if (!getabasis2 (D, Q,P2orig, inequality))
          return FALSE;

  if(Q->debug)
  {
    fprintf(lrs_ofp,"\nafter getabasis2");
    printA(D, Q);
  }
  nredundcol = Q->nredundcol;
  lastdv = Q->lastdv;
  d = D->d;

/********************************************************************/
/* now we start printing the output file  unless no output requested */
/********************************************************************/
  if (!no_output || Q->debug)
    {
      fprintf (lrs_ofp, "\nV-representation");

/* Print linearity space                 */
/* Don't print linearity if first column zero in hull computation */

      k = 0;

     if (nredundcol > k)
	{
	  fprintf (lrs_ofp, "\nlinearity %ld ", nredundcol - k);	/*adjust nredundcol for homog. */
	  for (i = 1; i <= nredundcol - k; i++)
	    fprintf (lrs_ofp, " %ld", i);
	}			/* end print of linearity space */

      fprintf (lrs_ofp, "\nbegin");
      fprintf (lrs_ofp, "\n***** %ld rational", Q->n);

    }				/* end of if !no_output .......   */

/* Reset up the inequality array to remember which index is which input inequality */
/* inequality[B[i]-lastdv] is row number of the inequality with index B[i]              */
/* inequality[C[i]-lastdv] is row number of the inequality with index C[i]              */

  for (i = 1; i <= m; i++)
    inequality[i] = i;
  if (nlinearity > 0)		/* some cobasic indices will be removed */
    {
      for (i = 0; i < nlinearity; i++)	/* remove input linearity indices */
	inequality[linearity[i]] = 0;
      k = 1;			/* counter for linearities         */
      for (i = 1; i <= m - nlinearity; i++)
	{
	  while (k <= m && inequality[k] == 0)
	    k++;		/* skip zeroes in corr. to linearity */
	  inequality[i] = inequality[k++];
	}
    }				/* end if linearity */
  if (Q->debug)
    {
      fprintf (lrs_ofp, "\ninequality array initialization:");
      for (i = 1; i <= m - nlinearity; i++)
	fprintf (lrs_ofp, " %ld", inequality[i]);
    }
  if (nredundcol > 0)
    {
      *Lin = lrs_alloc_mp_matrix (nredundcol, Q->n);

      for (i = 0; i < nredundcol; i++)
	{
	  if (!(Q->homogeneous && Q->hull && i == 0))	/* skip redund col 1 for homog. hull */
	    {
	      lrs_getray (D, Q, Col[0], D->C[0] + i - hull, (*Lin)[i]);		/* adjust index for deletions */
	    }

	  if (!removecobasicindex (D, Q, 0L))
	    return FALSE;
	}
    }				/* end if nredundcol > 0 */

      if (Q->verbose)
      {
      fprintf (lrs_ofp, "\nNumber of pivots for starting dictionary: %ld",Q->count[3]);
      ocount=Q->count[3];
      }

/* Do dual pivots to get primal feasibility */
  if (!primalfeasible (D, Q))
    {
     if ( Q->verbose )
      {
          fprintf (lrs_ofp, "\nNumber of pivots for feasible solution: %ld",Q->count[3]);
          fprintf (lrs_ofp, " - No feasible solution");
          ocount=Q->count[3];
      }
      return FALSE;
    }

    if (Q->verbose)
     {
      fprintf (lrs_ofp, "\nNumber of pivots for feasible solution: %ld",Q->count[3]);
      ocount=Q->count[3];
     }


/* Now solve LP if objective function was given */
  if (Q->maximize || Q->minimize)
    {
      Q->unbounded = !lrs_solvelp (D, Q, Q->maximize);

      /* check to see if objective is dual degenerate */
      j = 1;
      while (j <= d && !zero (A[0][j]))
      j++;
      if (j <= d)
	    Q->dualdeg = TRUE;
    }
  else
/* re-initialize cost row to -det */
    {
      for (j = 1; j <= d; j++)
	{
	  copy (A[0][j], D->det);
	  storesign (A[0][j], NEG);
	}

      itomp (ZERO, A[0][0]);	/* zero optimum objective value */
    }


/* reindex basis to 0..m if necessary */
/* we use the fact that cobases are sorted by index value */
  if (Q->debug)
    printA (D, Q);
  while (C[0] <= m)
    {
      i = C[0];
      j = inequality[B[i] - lastdv];
      inequality[B[i] - lastdv] = inequality[C[0] - lastdv];
      inequality[C[0] - lastdv] = j;
      C[0] = B[i];
      B[i] = i;
      reorder1 (C, Col, ZERO, d);
    }

  if (Q->debug)
    {
      fprintf (lrs_ofp, "\n*Inequality numbers for indices %ld .. %ld : ", lastdv + 1, m + d);
      for (i = 1; i <= m - nlinearity; i++)
	fprintf (lrs_ofp, " %ld ", inequality[i]);
      printA (D, Q);
    }



  if (Q->restart)
    {
      if (Q->debug)
	fprintf (lrs_ofp, "\nPivoting to restart co-basis");
      if (!restartpivots (D, Q))
	return FALSE;
      D->lexflag = lexmin (D, Q, ZERO);		/* see if lexmin basis */
      if (Q->debug)
	printA (D, Q);
    }
/* Check to see if necessary to resize */
  if (Q->inputd > D->d)
    *D_p = resize (D, Q);

  return TRUE;
}