示例#1
0
文件: main.c 项目: JohnXinhua/UVa
/*
void init(Medicine *medicine);
int addActive(Medicine *medicine, char *name, int weight);
int addInactive(Medicine *medicine, char *name, int weight);
void print(Medicine *medicine);
int totalWeight(Medicine *medicine);
char *maxIngredient(Medicine *medicine);
*/
void test_spec0() {
	int n, m;
	scanf("%d %d", &n, &m);
	Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n);
	int cmd, mid, weight;
	char name[128];
	for (int i = 0; i < n; i++)
		init(&medi[i]);
	for (int i = 0; i < m; i++) {
		scanf("%d %d", &cmd, &mid);
		if (cmd == 1) { 		// addActive
			scanf("%s %d", name, &weight);
			int af = addActive(&medi[mid], name, weight);
			printf("af %d\n", af);
		} else if (cmd == 2) {	// addInactive
			assert(false);
		} else if (cmd == 3) {	// print
			assert(false);
		} else if (cmd == 4) {	// totalWeight
			printf("weight = %d\n", totalWeight(&medi[mid]));
		} else if (cmd == 5) {	// maxIngredient
			assert(false);
		}
	}
}
示例#2
0
文件: main.c 项目: JohnXinhua/UVa
void test_spec4() {
	int n, m;
	scanf("%d %d", &n, &m);
	
	Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n);
	int cmd, mid, weight;
	char name[128];
	for (int i = 0; i < n; i++)
		init(&medi[i]);
	for (int i = 0; i < m; i++) {
		scanf("%d %d", &cmd, &mid);
		if (cmd == 1) { 		// addActive
			scanf("%s %d", name, &weight);
			int af = addActive(&medi[mid], name, weight);
			printf("af %d\n", af);
		} else if (cmd == 2) {	// addInactive
			scanf("%s %d", name, &weight);
			int bf = addInactive(&medi[mid], name, weight);
			printf("bf %d\n", bf);
		} else if (cmd == 3) {	// print
			print(&medi[mid]);
		} else if (cmd == 4) {	// totalWeight
			printf("weight = %d\n", totalWeight(&medi[mid]));
		} else if (cmd == 5) {	// maxIngredient
			char *ret = maxIngredient(&medi[mid]);
			printf("main ingredient = %s\n", ret == NULL ? "NOT FOUND" : ret);
		}
	}
}
示例#3
0
uint_t StaticLevelwiseCurveBalanceWeighted::operator()( SetupBlockForest & forest, const uint_t numberOfProcesses, const memory_t /*perProcessMemoryLimit*/ )
{
   // TODO: take per process memory limit into account?

   std::vector< SetupBlock * > blocks;
   if( hilbert_ )
      forest.getHilbertOrder( blocks );
   else
      forest.getMortonOrder( blocks );

   uint_t usedProcesses( uint_t(0) );

   for( uint_t level = uint_t(0); level < forest.getNumberOfLevels(); ++level )
   {
      std::vector< SetupBlock * > blocksOnLevel;

      for( auto block = blocks.begin(); block != blocks.end(); ++block )
         if( (*block)->getLevel() == level )
            blocksOnLevel.push_back( *block );

      workload_t totalWeight( 0 );
      for( auto block = blocksOnLevel.begin(); block != blocksOnLevel.end(); ++block )
      {
         WALBERLA_ASSERT( !( (*block)->getWorkload() < workload_t(0) ) );
         totalWeight += (*block)->getWorkload();
      }

      uint_t c( uint_t(0) );
      for( uint_t p = uint_t(0); p != numberOfProcesses; ++p )
      {
         const workload_t minWeight = totalWeight / workload_c( numberOfProcesses - p );
         workload_t weight( 0 );
         while( weight < minWeight && c < blocksOnLevel.size() )
         {
            blocksOnLevel[c]->assignTargetProcess(p);

            WALBERLA_ASSERT_LESS_EQUAL( p, usedProcesses );
            usedProcesses = p + uint_t(1);

            const workload_t addedWeight = blocksOnLevel[c]->getWorkload();
            weight += addedWeight;
            totalWeight -= addedWeight;
            ++c;
         }
      }
      while( c < blocksOnLevel.size() )
      {
         blocksOnLevel[c]->assignTargetProcess( numberOfProcesses - uint_t(1) );

         WALBERLA_ASSERT_LESS_EQUAL( numberOfProcesses - uint_t(1), usedProcesses );
         usedProcesses = numberOfProcesses;

         ++c;
      }


   }

   return usedProcesses;
}
示例#4
0
void prim()
{
    auto V = std::vector<Vertex>(N);
    auto E = std::vector<Edge>{ };
    auto T = std::vector<Vertex>{ };

    // generate city graph based on distance table
    createGraph(E, V);

    for(int t = V.size()-1; t >= 0; t--){
      V[t].key = Vertex::max_key;
      V[t].parent_index = Vertex::undef;
    }

    //int randomIndex = rand() % V.size();
    //V[randomIndex].key = 0;
	V[0].key = 0;

    auto Q = V;
    auto A = std::vector<Edge>{ };
    while(Q.size()-1 > 0){
      int smallest = Vertex::max_key;
      int erase = 0;
      int pos = 0;
      for (int x = 0; x < Q.size(); x++) {
          if (Q[x].key < smallest) {
            smallest = Q[x].key;
            pos = x;
          }
        }
        auto u = Q[pos];
        Q.erase(Q.begin()+pos);

	if(u.parent_index != Vertex::undef){
			A.push_back(Edge(u.index, u.parent_index));
		}
	adjacency(E,u.index,T);
	int smallestEdge = Vertex::max_key;
	int indicator = 0;
		for(int j = 0; j < T.size(); j++){
			if(weight(u.index,T[j].index) < smallestEdge){
				smallestEdge = weight(u.index,T[j].index);
                indicator = j;
        }
  }
  T[indicator].parent_index = u.index;
  T[indicator].key = weight(u.index,T[indicator].index);
  A.push_back(Edge(u.index, T[indicator].index));
  eraseEdge(E,u.index,T[indicator].index);
  }
  std::cout << "Minimal graph: E = {";
	for (int i = 0; i < A.size(); i++){
		std::cout << "(" << A[i].vi1 << "," << A[i].vi2 << ")" << ",";
	}
	std::cout << "\b" << "}";
	std::cout << "\n" << "Minimal costs: " << totalWeight(A) << "\n";
}
void MultiplicativeWeightsLearningModel::updateWeights(std::vector<Value> profits, Value, double phi) {

    struct StratUsage {
        Value profits;
        MinerCount minersUsing;
        
        StratUsage() : profits(0), minersUsing(0) {}
        
        void addProfit(Value profit) {
            profits += profit;
            minersUsing++;
        }
        
        void normalize() {
            if (minersUsing > MinerCount(1)) {
                profits /= Value(minersUsing);
                minersUsing = MinerCount(1);
            }
        }
        
        bool isUnused() {
            return minersUsing == MinerCount(0);
        }
        
        double profitRatio(Value maxProfits) {
            assert(profits <= maxProfits);
            return rawValue(profits/maxProfits);
        }
    };
    
    std::vector<StratUsage> stratUsages;
    stratUsages.resize(stratCount);
    
    for (size_t i = 0; i < minerCount; i++) {
        stratUsages[getChosenStrat(i)].addProfit(profits[i]);
    }
    
    Value maxProfits(0);
    for (auto &stratUsage : stratUsages) {
        stratUsage.normalize();
        if (maxProfits < stratUsage.profits) {
            maxProfits = stratUsage.profits;
        }
    }
    
    std::vector<StratWeight> newWeights;
    newWeights.reserve(stratCount);
    
    StratWeight totalWeight(0);
    for (size_t i = 0; i < stratUsages.size(); i++) {
        double c_t = 1.1 - stratUsages[i].profitRatio(maxProfits);
        if (stratUsages[i].isUnused()) {
            //means no one tried the strategy, keep weight unchanged (encourage exploration)?
            c_t = 0;
        }
        
        StratWeight newWeight = getCurrentWeight(i) * StratWeight(pow((1-phi), c_t));
        updateWeight(i, newWeight);
        totalWeight += newWeight;
        newWeights.push_back(newWeight);
    }
    
    for (size_t i = 0; i < newWeights.size(); i++) {
        updateWeight(i, newWeights[i] / totalWeight);
    }
    
    std::vector<StratWeight> weights = getCurrentWeights();
    
    probabilities.clear();
    probabilities.reserve(stratCount);
    std::transform(begin(weights), end(weights), std::back_inserter(probabilities), [](const auto &weight) { return rawWeight(weight); });
}