Пример #1
0
int optimal(int weight, int idx, const item_t item[]) {
	if (idx < 0) {
		return 0;
	}
 
    __assume_aligned(memo, 64);
    if (weight < item[idx].weight) {
        if( idx - 1 >= 0 ) {
            if( memo[(idx-1) * (capacity+1) + weight] == 0 ) {
                memo[(idx-1) * (capacity+1) + weight] = optimal(weight, idx-1, item); 
            }
            return memo[(idx-1) * (capacity+1) + weight];
        }
        return 0;
    }
	
    if( idx - 1 >= 0 ) {
        if( memo[(idx-1) * (capacity+1) + weight] == 0 ) {
            memo[(idx-1) * (capacity+1) + weight] = optimal(weight, idx-1, item);
        }
        if( weight - item[idx].weight >= 0 ) {
            if( memo[(idx-1) * (capacity+1) + (weight - item[idx].weight)] == 0 ) {
                memo[(idx-1) * (capacity+1) + (weight - item[idx].weight)] = optimal(weight - item[idx].weight, idx-1, item);
            }
            int aux = ( memo[(idx-1) * (capacity+1) + weight] >= ( memo[(idx-1) * (capacity+1) + (weight - item[idx].weight)] + item[idx].value ) ) ?
                memo[(idx-1) * (capacity+1) + weight] : ( memo[(idx-1) * (capacity+1) + (weight - item[idx].weight)] + item[idx].value );
            return aux;
        }
        else {
            return ( memo[(idx-1) * (capacity+1) + weight] >= item[idx].value ) ? memo[(idx-1) * (capacity+1) + weight] : item[idx].value;
        }
    }
	return item[idx].value;
}
Пример #2
0
int main(int argc, char *argv[]) {
	int i;
	
	if( argc < 4 ) {
		printf("./%s filename n m\n", argv[0]);
		exit(0);
	}

    const char *filename = argv[1];
    int n = atoi(argv[2]);
    int m = atoi(argv[3]);
    int max = ( n > m ? n : m );

	item_t item[max];
	FILE *f = fopen(filename, "r");
	for(i=0;i<max;i++)
		fscanf(f, "%d,%d", &item[i].weight, &item[i].value);

    struct timespec tstart, tend;
    clock_gettime( CLOCK_REALTIME, &tstart );
    memo = (int *) mmap( NULL, max*(capacity+1)*sizeof(int), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NONBLOCK, -1, 0 );
    assert( memo != MAP_FAILED );
    const int ret = madvise( (void *) memo, max*(capacity+1)*sizeof(int), MADV_SEQUENTIAL );
    assert( ret == 0 );
	int res = optimal(capacity, n-1, item);
	int res2 = optimal(capacity, m-1, item);
    clock_gettime( CLOCK_REALTIME, &tend );

    printf( "res=%d, res2=%d\n", res, res2 );
    printf( "Time %f\n", (double) ( tend.tv_sec - tstart.tv_sec ) + ( tend.tv_nsec - tstart.tv_nsec )/1E9 ); 
}
Пример #3
0
// Finds the maximum value
arma::vec OptimalValue(const arma::mat& state,
                       const arma::mat& subgradient) {
  const arma::mat t_state = state.t();
  const std::size_t n_state = state.n_rows;
  arma::vec optimal(n_state);
#pragma omp parallel for
  for (std::size_t ii = 0; ii < n_state; ii++) {
    optimal(ii) = (subgradient * t_state.col(ii)).max();
  }
  return optimal;
}
Пример #4
0
int main(void)
{
        srand(423423423);
        item = malloc(sizeof(item_t)*n_items);
        int i;
        for (i = 0; i < n_items; i++){
            item[i].weight = (rand() % 20) + 1;
            item[i].value = (rand() % 30) + 1;
            //printf("i: %d\tw: %d\tv:%d \n", i, items[i].weight, items[i].value);
        }
        i = 0;
        int w = 0;
        solution s = {0, 0};
        optimal(start_weight, n_items - 1, &s);
 
        for (i = 0; i < n_items; i++) {
                if (s.bits & (1 << i)) {
                        printf("%d ", i);
                        w += item[i].weight;
                }
        }
        printf("\n");
        for (i = 0; i < n_items; i++){
            printf("i: %d\tw: %d\tv:%d \n", i, item[i].weight, item[i].value);
        }
        printf("Total value: %d; weight: %d\n", s.value, w);
        return 0;
}
Пример #5
0
int optimal(int i, Array S,int count)
{
  if(count==0)
    return C[i][0];
  else
    {
      int min=999999,j=0,k=0,temp=0,find=0;
      for(k=0;k<count;k++)
      {
         
         int temp = S.myArray[0];
         S.myArray[0] = S.myArray[k];
         S.myArray[k] = temp;

         Array T;
         for(j=1;j<count;j++)
             T.myArray[j-1]=S.myArray[j];
 
         find = C[i][S.myArray[0]] + optimal(S.myArray[0],T,count-1);
         
         

         if(find < min)
           {
              min = find;
           }
      }
      
      return min;
    }
}
Пример #6
0
int main()
{
int i,ch;
printf("\nEnter total number of pages:");
scanf("%d",&n);
printf("\nEnter sequence:");
for(i=0;i<n;i++) //accepting sequence
scanf("%d",&pg[i]);
do
{
printf("\n\tMENU\n");
printf("\n1)FIFO");
printf("\n2)OPTIMAL");
printf("\n3)LRU");
printf("\n4)Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: fifo();
break;

case 2: optimal();
break;

case 3: lru();
break;

}
}while(ch!=4);
getchar();
}
Пример #7
0
void optimal(int weight, int idx, solution *s)
{
        solution v1, v2;
        if (idx < 0) {
                s->bits = s->value = 0;
                return;
        }
 
        if (weight < item[idx].weight) {
                optimal(weight, idx - 1, s);
                return;
         }
 
        optimal(weight, idx - 1, &v1);
        optimal(weight - item[idx].weight, idx - 1, &v2);
 
        v2.value += item[idx].value;
        v2.bits |= (1 << idx);
 
        *s = (v1.value >= v2.value) ? v1 : v2;
}
Пример #8
0
int main(void)
{
	int frameSize, i = 0;
	char file[32], *requests = NULL, ch;
	FILE *fp;
	initscr();
	werase(stdscr);
	getmaxyx(stdscr, maxy, maxx);
	mvprintw(maxy/2 - 1, (maxx - 40)/2, "Enter the name of file to be read: ");
	scanw("%s", file);
	if((fp = fopen(file, "r")) == NULL)
	{
		mvprintw(maxy/2 + 1, (maxx - 20) / 2, "Error opening file!!!");
		getch();
		endwin();
		return -1;
	}
	ch = getc(fp);
	while(ch != EOF)
	{
		i++;
		requests = (char *) realloc(requests, sizeof(char) * i);
		requests[i - 1] = ch;
		ch = getc(fp);
	};
	requests = (char *) realloc(requests, sizeof(char) * (i+1));
	requests[i] = '\0';
	fclose(fp);

	mvprintw(maxy/2 + 1, (maxx - 30) / 2, "Enter the number of frames: ");
	scanw("%d", &frameSize);
	while(1)
	{
		switch(menu())
		{
			case 1: fcfs(requests, frameSize);
				break;

			case 2: lru(requests, frameSize);
				break;

			case 3: optimal(requests, frameSize);
				break;

			case 4: endwin();
				return 0;
		}
	}
	return 0;
}
Пример #9
0
void main()
{
  int n,i;
  printf("Enter the number of cities: ");
  scanf("%d",&n);

  printf("\nThe adjacency matrix of the random Hamiltonian Graph is: \n");
  fillMatrix(n);

  Array init;

  for(i=1;i<n;i++)
  {
    init.myArray[i-1]=i;
  }
 
  int result;
  result = optimal(0,init,n-1);
  printf("\n The optimal route cost is %d \n",result); 
} 
Пример #10
0
void main()
{
   int dist=0,i,j;
   float p,q;
   printf("\nEnter the no of cities\n");
   scanf("%d",&n);
   printf("\nEnter\n");
   for(i=1;i<=n;i++)
   for(j=1;j<=n;j++)
   scanf("%d",&a[i][j]);
   printf("\nEnter the source node\n");
   scanf("%d",&source);
   visited[source]=1;
   path[1]=source;

   distance(a,dist,2,source);

   p=optimal();
   q=nearest();
   printf("\n\nResult=%f",(q/p));
 }
Пример #11
0
void print_path()
{
	int i,dest,src;
	
	printf("Enter the source\n");
	scanf("%d",&src);
	
	for(dest=2;dest<=n;dest++)
	{	optimal(src,dest);
		if(d[dest]==9999)
		printf("No path\n");
		else
		{
			i=dest;
			while(i!=src)
			{
				printf("%d <--- ",i);
				i=p[i];
			}
			printf("%d\n",src);
		}
		printf("Cost = %d \n", d[dest]);
	}	
}