Esempio n. 1
0
void Tree::generate(uint8_t limit)
{
    uint8_t darkness=1;

    srand((uint32_t)time(NULL));

    uint8_t m_trunkHeight = getRandInt(MIN_TRUNK,limit);

    bool smalltree=false;

    //in this implementation we generate the trunk and as we do we call branching and canopy
    if(m_trunkHeight<BRANCHING_HEIGHT)
    {
        smalltree=true;
        darkness=0;
    }
    uint8_t th=m_trunkHeight-1;
    uint8_t i;
    for(i = 0; i < th; i++)
    {
        if(smalltree)
        {
            Trunk* v = new Trunk(_x,_y+i,_z,darkness);
            if(i>=MIN_TRUNK-1){
                m_Branch[n_branches]= v;
                n_branches++;
            }
            else
            {
                delete v;
            }
        }
        else
        {
            Trunk* v = new Trunk(_x,_y+i,_z,darkness);
            if(i>BRANCHING_HEIGHT-1)
            {
                generateBranches(v);
            }
            else
            {
                delete v;
            }
        }
    }
    Trunk* v = new Trunk(_x,_y+i,_z,darkness);
    m_Branch[n_branches]= v;
    n_branches++;
    generateBranches(v);
    generateCanopy();
}
Esempio n. 2
0
void Tree::generate(uint8_t limit)
{

  const uint8_t m_trunkHeight = uniformUINT8(MIN_TRUNK, limit);

  bool smalltree = false;
  uint8_t type = 0;

  if (m_trunkHeight < BRANCHING_HEIGHT)
  {
    smalltree = true;
  }

  if (uniform01() > 0.5) // 1/2 chance
  {
    ++type;
    if (uniform01() > 0.5) // 1/4
    {
      ++type;
    }
  }

  for (unsigned int i = 0; i + 1 < m_trunkHeight /* Trunk Height */; ++i)
  {
    if (smalltree)
    {
      const TrunkPtr v(new Trunk(_x, _y + i, _z, _map, type));
      if (i >= MIN_TRUNK - 1)
      {
        m_Branch[n_branches++] = v;
      }
    }
    else
    {
      const TrunkPtr v(new Trunk(_x, _y + i, _z, _map, type));
      if (i > BRANCHING_HEIGHT - 1)
      {
        generateBranches(v);
        m_Branch[n_branches++] = v;
      }
    }
  }

  const TrunkPtr v(new Trunk(_x, _y + m_trunkHeight - 1, _z, _map, type));
  generateBranches(v);
  generateCanopy();
  m_Branch[n_branches++] = v;
}
Esempio n. 3
0
void Tree::generateBranches(TrunkPtr wrap)
{
  int32_t x = wrap->_x;
  uint8_t y = wrap->_y;
  int32_t z = wrap->_z;

  uint32_t schanse = BRANCHING_CHANCE;

  if (uniform01() > 1.0 - (1.0 / BRANCHING_CHANCE))
  {
    const double r = uniform01();
    if (r < 0.2)
    {
      x--;
    }
    else if (r < 0.4)
    {
      x++;
    }
    else if (r < 0.6)
    {
      z++;
    }
    else if (r < 0.8)
    {
      z--;
    }
    if (r > 0.5)
    {
      y++;
    }

    const TrunkPtr v(new Trunk(x, y, z, _map));
    m_Branch[n_branches++] = v;
    generateBranches(v);
  }
}
std::size_t BppSolver::branchAndPrice(BinPackingProblem& bpp)
{
	TreeNodes nodes;
	nodes.emplace_back();
	nbGeneratedNodes = 0;
	while (!nodes.empty())
	{
		BranchNode& node = nodes.back();
		if (node.toPrune)
		{
			for (auto i : *node.invalidColumns)
				BPmodel.setVarUB(i, 1.0);
			if (node.id >= 0)
				//pricerBranchConstraints[node.id].end();
				pricerModel.removeConstraint(node.id);

#ifdef EXPORT_MODEL
			BPSolver.exportModel("model.lp");
			pricerSolver.exportModel("pricer.lp");
#endif // !EXPORT_MODEL

			nodes.pop_back(); // delete the node.
		}
		else if (std::ceil(node.incumbentLPValue) <= upperBound)
		{
			// modify the master and pricer to solve the specific node
			// desable ~current and incumbent invalide variables
			for (auto i : *node.invalidColumns)
				BPmodel.disableVar(i);

			// add the specific pricer branch constraint
			IloExpr expr(pricerModel.getEnv());
			if (node.brType == TOGETHER)
			{
				expr += pricerModel.getVar(node.couple_ij.first) - pricerModel.getVar(node.couple_ij.second);
				pricerModel.addConstraint(expr, 0.0);
			}
			else if (node.brType == SEPARATE)
			{
				expr += pricerModel.getVar(node.couple_ij.first) + pricerModel.getVar(node.couple_ij.second);
				pricerModel.addConstraint(expr, 1.0);
			}
			expr.end();

#ifdef EXPORT_MODEL
			BPSolver.exportModel("model.lp");
			pricerSolver.exportModel("pricer.lp");
#endif // !EXPORT_MODEL


			// generate columns
			real master_lp_value = columnGeneration(bpp);
			//std::cout << master_lp_value << "\n";
			// is solution integral
			if (BPmodel.checkIntegrality())
			{
				std::cout << "---------------- INTEGRAL -----------------\n";
				// possible new upper bound
				if (std::ceil(master_lp_value - EPS) < upperBound)
				{
					// update upper bound
					upperBound = master_lp_value;
					// copy the current solution
					bestSolution = BPmodel.getPrimalValues();
				}
				node.toPrune = true;
				//else if (master_lp_value == upperBound) // optimal 
				//	return upperBound;
			}
			else if (std::ceil(master_lp_value) < upperBound) // generate branchs
			{
				auto couple = ryanFosterCouple(node);
				node.toPrune = true;
				generateBranches(master_lp_value, couple, node, nodes);
			}
			else
			{
				// no node to generate, then this node must be pruned 
				node.toPrune = true;
			}
		}
		else
			// to prune
			node.toPrune = true;

	} while (!nodes.empty());

	return (std::size_t)upperBound;
}