//==================================================================================================
// G r a p h _ p r o b l e m                                                              Interface
// M i n _ c o s t _ t e n s i o n
// C o n f o r m i n g _ l i n e a r
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module implements the conforming algorithm to solve the minimum cost tension problem in
   graphs. The cost functions of the arcs in the graph must be linear as defined by the
   <CODE>clLinearArcData</CODE> class of the <CODE>Structure</CODE> module. */

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "graph_problem/min_cost_tension/conforming_linear.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guGraphProblemMinCostTensionConformingLinear
#define guGraphProblemMinCostTensionConformingLinear

// Headers //---------------------------------------------------------------------------------------
#include <bpp/graph_problem/min_cost_tension/conforming.hpp> /*INCLUDE*/

namespace bpp {

// Importation/Exportation //-----------------------------------------------------------------------
#ifdef GRAPH_PROBLEM_MIN_COST_TENSION_DLL
 #define dll_export DLL_EXPORT
#else
 #define dll_export DLL_IMPORT
#endif

// Namespaces //------------------------------------------------------------------------------------
#define public_area  graphProblemMinCostTensionConformingLinear
#define private_area graphProblemMinCostTensionConformingLinear_private

namespace public_area { /*NAMESPACE*/ using namespace graphProblemMinCostTensionConforming; }

namespace private_area {
 using namespace public_area;
 using namespace graphProblemMinCostTensionConforming_private;
}

extern_module_name;

// Initialization //--------------------------------------------------------------------------------

// Macrocommands //---------------------------------------------------------------------------------
/*ALIAS*/
#define tdGraph class prArcData,class prNodeData //

/*ALIAS*/
#define tuGraph prArcData,prNodeData //

// Types & Classes //-------------------------------------------------------------------------------
namespace public_area {
 //------------------------------------------------------------------------------------------Classes
 template <tdGraph> class clSolveAlgo;
 template <tdGraph> class clSolveAlgoI;
 template <tdGraph> class clSolveAlgoII;
 template <tdGraph> class clCostScaleAlgo;
 template <tdGraph> class clCapacityScaleAlgo;
}

namespace private_area { template <tdGraph> class clConformity; }

// Functions Interface //---------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// S o l v e A l g o  Interface //-----------------------------------------------------------------
namespace public_area {
 /*CLASS clSolveAlgo */
 /* Represents an algorithm to solve the minimum cost tension problem in a graph using the
    conforming method. It is an abstract class. */
 template <tdGraph> class clSolveAlgo : public clLinearSolver<tuGraph> {
  //-----------------------------------------------------------------------------------------Private
  private_property constructor clSolveAlgo(const clSolveAlgo &);
  private_property clSolveAlgo & operator = (const clSolveAlgo &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clSolveAlgo(void);
  public_property virtual destructor clSolveAlgo(void);

  /*AMETHOD clSolveAlgo */
  /* Solves the minimum cost tension problem of a graph. Abstract method. */
  public_property virtual tyInteger run(clGraph<tuGraph> & agGraph) const = 0;

  public_property static tyInteger defaultRun(clGraph<tuGraph> &);
 };
}

// S o l v e A l g o I  Interface //----------------------------------------------------------------
namespace public_area {
 /*CLASS clSolveAlgoI */
 /* Represents an algorithm to solve the minimum cost tension problem in a graph using the
    conforming method (version I). */
 template <tdGraph> class clSolveAlgoI : public clSolveAlgo<tuGraph> {
  //-----------------------------------------------------------------------------------------Private
  private_property constructor clSolveAlgoI(const clSolveAlgoI &);
  private_property clSolveAlgoI & operator = (const clSolveAlgoI &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clSolveAlgoI(void);
  public_property destructor clSolveAlgoI(void);

  public_property tyInteger run(clGraph<tuGraph> &) const;
 };
}

// S o l v e A l g o I I  Interface //--------------------------------------------------------------
namespace public_area {
 /*CLASS clSolveAlgoII */
 /* Represents an algorithm to solve the minimum cost tension problem in a graph using the
    conforming method (version II). */
 template <tdGraph> class clSolveAlgoII : public clSolveAlgo<tuGraph> {
  //-----------------------------------------------------------------------------------------Private
  private_property constructor clSolveAlgoII(const clSolveAlgoII &);
  private_property clSolveAlgoII & operator = (const clSolveAlgoII &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clSolveAlgoII(void);
  public_property destructor clSolveAlgoII(void);

  public_property tyInteger run(clGraph<tuGraph> &) const;
 };
}

// C o s t S c a l e A l g o  Interface //----------------------------------------------------------
namespace public_area {
 /*CLASS clCostScaleAlgo */
 /* Represents an algorithm to solve the minimum cost tension problem in a graph using the
    conforming method (cost-scaling version). */
 template <tdGraph> class clCostScaleAlgo : public clSolveAlgo<tuGraph> {
  //-----------------------------------------------------------------------------------------Private
  private_property constructor clCostScaleAlgo(const clCostScaleAlgo &);
  private_property clCostScaleAlgo & operator = (const clCostScaleAlgo &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clCostScaleAlgo(void);
  public_property destructor clCostScaleAlgo(void);

  public_property tyInteger run(clGraph<tuGraph> &) const;
 };
}

// C a p a c i t y S c a l e A l g o  Interface //--------------------------------------------------
namespace public_area {
 /*CLASS clCapacityScaleAlgo */
 /* Represents an algorithm to solve the minimum cost tension problem in a graph using the
    conforming method (capacity-scaling version). */
 template <tdGraph> class clCapacityScaleAlgo : public clSolveAlgo<tuGraph> {
  //-----------------------------------------------------------------------------------------Private
  private_property constructor clCapacityScaleAlgo(const clCapacityScaleAlgo &);
  private_property clCapacityScaleAlgo & operator = (const clCapacityScaleAlgo &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clCapacityScaleAlgo(void);
  public_property destructor clCapacityScaleAlgo(void);

  public_property tyInteger run(clGraph<tuGraph> &) const;
 };
}

// C o n f o r m i t y  Interface //----------------------------------------------------------------
namespace private_area {
 template <tdGraph> class clConformity {
  //-----------------------------------------------------------------------------------------Private
  private_property clConformity(const clConformity &);
  private_property clConformity & operator = (const clConformity &);
  //------------------------------------------------------------------------------------------Public
  public_property constructor clConformity(void) {}
  public_property destructor  clConformity(void) {}

  public_property tyMintyColor arcColor(const clArc<tuGraph> &) const;
  public_property tyBoolean    conformedArc(const clArc<tuGraph> &) const;
  public_property tyReal       derivative(const clArc<tuGraph> &,tyReal) const { return (0.0); }

  public_property tyReal maximumDecreaseFlow(const clArc<tuGraph> &) const;
  public_property tyReal maximumIncreaseFlow(const clArc<tuGraph> &) const;
  public_property tyReal maximumDecreaseTension(const clArc<tuGraph> &) const;
  public_property tyReal maximumIncreaseTension(const clArc<tuGraph> &) const;
 };
}

// Functions Inline //------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// S o l v e A l g o  Inline //---------------------------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clSolveAlgo */ /* Builds an algorithm to solve the minimum cost tension problem. */
 template <tdGraph> inline clSolveAlgo<tuGraph>::clSolveAlgo(void) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clSolveAlgo */ /* Destructs the algorithm. */
 template <tdGraph> inline clSolveAlgo<tuGraph>::~clSolveAlgo(void) {}
 //---------------------------------------------------------------------------------------DefaultRun
 /*METHOD clSolveAlgo */
 /* Solves the minimum cost tension problem of a graph using the default version of the algorithm
    (version II). Static method. */
 template <tdGraph> inline tyInteger clSolveAlgo<tuGraph>::defaultRun(clGraph<tuGraph> & agGraph)
 { return (clSolveAlgoII<tuGraph>().run(agGraph)); }
}

// S o l v e A l g o I  Inline //-------------------------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clSolveAlgoI */
 /* Builds an algorithm to solve the minimum cost tension problem. */
 template <tdGraph> inline clSolveAlgoI<tuGraph>::clSolveAlgoI(void) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clSolveAlgoI */ /* Destructs the algorithm. */
 template <tdGraph> inline clSolveAlgoI<tuGraph>::~clSolveAlgoI(void) {}
}

// S o l v e A l g o I I  Inline //-----------------------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clSolveAlgoII */
 /* Builds an algorithm to solve the minimum cost tension problem. */
 template <tdGraph> inline clSolveAlgoII<tuGraph>::clSolveAlgoII(void) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clSolveAlgoII */ /* Destructs the algorithm. */
 template <tdGraph> inline clSolveAlgoII<tuGraph>::~clSolveAlgoII(void) {}
}

// C o s t S c a l e A l g o  Inline //-------------------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clCostScaleAlgo */
 /* Builds an algorithm to solve the minimum cost tension problem. */
 template <tdGraph> inline clCostScaleAlgo<tuGraph>::clCostScaleAlgo(void) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clCostScaleAlgo */ /* Destructs the algorithm. */
 template <tdGraph> inline clCostScaleAlgo<tuGraph>::~clCostScaleAlgo(void) {}
}

// C a p a c i t y S c a l e A l g o  Inline //-----------------------------------------------------
namespace public_area {
 //--------------------------------------------------------------------------------------Constructor
 /*METHOD clCapacityScaleAlgo */
 /* Builds an algorithm to solve the minimum cost tension problem. */
 template <tdGraph> inline clCapacityScaleAlgo<tuGraph>::clCapacityScaleAlgo(void) {}
 //---------------------------------------------------------------------------------------Destructor
 /*METHOD clCapacityScaleAlgo */ /* Destructs the algorithm. */
 template <tdGraph> inline clCapacityScaleAlgo<tuGraph>::~clCapacityScaleAlgo(void) {}
}

// C o n f o r m i t y  Inline //-------------------------------------------------------------------
namespace private_area {
 //-----------------------------------------------------------------------------------------ArcColor
 template <tdGraph>
 inline tyMintyColor clConformity<tuGraph>::arcColor(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()<agArc.data().minimum()) return (black);
  else if (agArc.data().tension()>agArc.data().maximum()) return (blue);
  else if (agArc.data().minimum()==agArc.data().maximum()) return (green);
  else if (-agArc.data().shrinkingCost()==agArc.data().stretchingCost()) {
   if (agArc.data().flow()<agArc.data().stretchingCost()) {
    if (agArc.data().tension()==agArc.data().minimum()) return (green);
    else return (blue);
   }
   else if (agArc.data().flow()==agArc.data().stretchingCost()) {
    if (agArc.data().tension()==agArc.data().minimum()) return (black);
    else if (agArc.data().tension()<agArc.data().maximum()) return (red);
    else return (blue);
   }
   else {
    if (agArc.data().tension()==agArc.data().maximum()) return (green);
    else return (black);
   }
  }
  else {
   if (agArc.data().flow()<-agArc.data().shrinkingCost()) {
    if (agArc.data().tension()==agArc.data().minimum()) return (green);
    else return (blue);
   }
   else if (agArc.data().flow()==-agArc.data().shrinkingCost()) {
    if (agArc.data().minimum()==agArc.data().optimum()) {
     if (agArc.data().tension()>agArc.data().minimum()) return (blue);
     else return (green);
    }
    else {
     if (agArc.data().tension()==agArc.data().minimum()) return (black);
     else if (agArc.data().tension()<agArc.data().optimum()) return (red);
     else return (blue);
    }
   }
   else if (agArc.data().flow()<agArc.data().stretchingCost()) {
    if (agArc.data().tension()<agArc.data().optimum()) return (black);
    else if (agArc.data().tension()==agArc.data().optimum()) return (green);
    else return (blue);
   }
   else if (agArc.data().flow()==agArc.data().stretchingCost()) {
    if (agArc.data().maximum()==agArc.data().optimum()) {
     if (agArc.data().tension()<agArc.data().maximum()) return (black);
     else return (green);
    }
    else {
     if (agArc.data().tension()<=agArc.data().optimum()) return (black);
     else if (agArc.data().tension()<agArc.data().maximum()) return (red);
     else return (blue);
    }
   }
   else {
    if (agArc.data().tension()<agArc.data().maximum()) return (black);
    else return (green);
   }
  }
 }
 //-------------------------------------------------------------------------------------ConformedArc
 template <tdGraph>
 inline tyBoolean clConformity<tuGraph>::conformedArc(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()<agArc.data().minimum()) return (false);
  else if (agArc.data().tension()>agArc.data().maximum()) return (false);
  else if (agArc.data().minimum()==agArc.data().maximum()) return (true);
  else if (-agArc.data().shrinkingCost()==agArc.data().stretchingCost()) {
   if (agArc.data().flow()<agArc.data().stretchingCost())
    return (agArc.data().tension()==agArc.data().minimum());
   else if (agArc.data().flow()==agArc.data().stretchingCost())
    return (true);
   else
    return (agArc.data().tension()==agArc.data().maximum());
  }
  else {
   if (agArc.data().flow()<-agArc.data().shrinkingCost())
    return (agArc.data().tension()==agArc.data().minimum());
   else if (agArc.data().flow()==-agArc.data().shrinkingCost())
    return (agArc.data().tension()>=agArc.data().minimum()
            and agArc.data().tension()<=agArc.data().optimum());
   else if (agArc.data().flow()<agArc.data().stretchingCost())
    return (agArc.data().tension()==agArc.data().optimum());
   else if (agArc.data().flow()==agArc.data().stretchingCost())
    return (agArc.data().tension()>=agArc.data().optimum()
            and agArc.data().tension()<=agArc.data().maximum());
   else return (agArc.data().tension()==agArc.data().maximum());
  }
 }
 //------------------------------------------------------------------------------MaximumDecreaseFlow
 template <tdGraph> inline
 tyReal clConformity<tuGraph>::maximumDecreaseFlow(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()<agArc.data().minimum()) return (realMax());
  else if (agArc.data().flow()<=-agArc.data().shrinkingCost()) return (realMax());
  else if (agArc.data().tension()<=agArc.data().optimum())
   return (agArc.data().flow()+agArc.data().shrinkingCost());
  else return (agArc.data().flow()-agArc.data().stretchingCost());
 }
 //------------------------------------------------------------------------------MaximumIncreaseFlow
 template <tdGraph> inline
 tyReal clConformity<tuGraph>::maximumIncreaseFlow(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()>agArc.data().maximum()) return (realMax());
  else if (agArc.data().flow()>=agArc.data().stretchingCost()) return (realMax());
  else if (agArc.data().tension()>=agArc.data().optimum())
   return (agArc.data().stretchingCost()-agArc.data().flow());
  else return (-agArc.data().shrinkingCost()-agArc.data().flow());
 }
 //---------------------------------------------------------------------------MaximumDecreaseTension
 template <tdGraph> inline
 tyReal clConformity<tuGraph>::maximumDecreaseTension(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()>agArc.data().maximum())
   return (agArc.data().tension()-agArc.data().maximum());
  else if (agArc.data().flow()<=-agArc.data().shrinkingCost())
   return (agArc.data().tension()-agArc.data().minimum());
  else return (agArc.data().tension()-agArc.data().optimum());
 }
 //---------------------------------------------------------------------------MaximumIncreaseTension
 template <tdGraph> inline
 tyReal clConformity<tuGraph>::maximumIncreaseTension(const clArc<tuGraph> & agArc) const {
  if (agArc.data().tension()<agArc.data().minimum())
   return (agArc.data().minimum()-agArc.data().tension());
  else if (agArc.data().flow()>=agArc.data().stretchingCost())
   return (agArc.data().maximum()-agArc.data().tension());
  else return (agArc.data().optimum()-agArc.data().tension());
 }
}

// S o l v e A l g o I  Implementation //-----------------------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*METHOD clSolveAlgoI */ /* Solves the minimum cost tension problem of a graph. */
 template <tdGraph> tyInteger clSolveAlgoI<tuGraph>::run(clGraph<tuGraph> & agGraph) const {
  typedef typename clGraph<tuGraph>::cpArcX::const_iterator cpIterator;

  cpIterator lcCurrentArc = agGraph.arcs().begin();
  cpIterator lcLastArc    = agGraph.arcs().end();

  tyInteger lcIteration = 0;

  private_area::clConformity<tuGraph> lcConformity;

  graphProblemTensionAlgorithm::clCompatibleTensionAlgo<tuGraph>::defaultRun(agGraph);
  if (agGraph.solved()==false) return (-1);
  agGraph.solved()=false;
  nullFlow(agGraph);

  while (lcCurrentArc!=lcLastArc) {
   lcIteration+=private_area::conformArc((*(*lcCurrentArc).second),lcConformity);
   lcCurrentArc++;
  }

  agGraph.solved()=true;
  return (lcIteration);
 }
}

// S o l v e A l g o I I  Implementation //---------------------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*METHOD clSolveAlgoII */ /* Solves the minimum cost tension problem of a graph. */
 template <tdGraph> tyInteger clSolveAlgoII<tuGraph>::run(clGraph<tuGraph> & agGraph) const {
  typedef clArc<tuGraph> *                                  cpArc;
  typedef typename clGraph<tuGraph>::cpArcX::const_iterator cpIterator;

  cpArc *    lcArc1S      = new_array(cpArc,agGraph.arcs().size());
  cpArc *    lcArc2S      = new_array(cpArc,agGraph.arcs().size());
  cpIterator lcCurrentArc = agGraph.arcs().begin();
  tyInteger  lcIteration  = 0;
  cpIterator lcLastArc    = agGraph.arcs().end();

  cpArc      lcArc;
  tyCardinal lcCounter1;
  tyCardinal lcCounter2;

  private_area::clConformity<tuGraph> lcConformity;

  graphProblemMinCostTensionConforming_private::
  clDirectMintyColor<tuGraph,private_area::clConformity<tuGraph> >
  lcDirectMintyColor(lcConformity);

  graphProblemMinCostTensionConforming_private::
  clInverseMintyColor<tuGraph,private_area::clConformity<tuGraph> >
  lcInverseMintyColor(lcConformity);

  // Compatible Tension Search //
  graphProblemTensionAlgorithm::clCompatibleTensionAlgo<tuGraph>::defaultRun(agGraph);

  if (agGraph.solved()==false) {
   delete_array(lcArc1S);
   delete_array(lcArc2S);
   return (-1);
  }

  agGraph.solved()=false;

  // Null Flow Setting //
  nullFlow(agGraph);

  // Non Conformed Arcs List //
  lcCounter1=0;

  while (lcCurrentArc!=lcLastArc) {
   lcArc1S[lcCounter1]=(*lcCurrentArc).second;
   ++lcCounter1;
   ++lcCurrentArc;
  }

  // Conforming //
  while (lcCounter1!=0) {
   standard::swap(lcArc1S,lcArc2S);
   lcCounter2=lcCounter1;
   lcCounter1=0;

   while (lcCounter2>0) {
    lcCounter2--;
    lcArc=lcArc2S[lcCounter2];

    if (not lcConformity.conformedArc(*lcArc)) {
     lcIteration++;
     private_area::improveArc(*lcArc,lcDirectMintyColor,lcInverseMintyColor,lcConformity);
     lcArc1S[lcCounter1]=lcArc;
     lcCounter1++;
    }
   }
  }

  delete_array(lcArc1S);
  delete_array(lcArc2S);
  agGraph.solved()=true;
  return (lcIteration);
 }
}

// C o s t S c a l e A l g o  Implementation //-----------------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*METHOD clCostScaleAlgo */ /* Solves the minimum cost tension problem of a graph. */
 template <tdGraph> tyInteger clCostScaleAlgo<tuGraph>::run(clGraph<tuGraph> & agGraph) const {
  typedef typename clGraph<tuGraph>::cpArcX::const_iterator cpArcIterator;
  typedef std_vector(tyReal)                                clRealS;

  tyInteger lcCounter;
  tyReal    lcCost;
  tyInteger lcPrecision;
  clRealS   lcShrinkingCostS;
  clRealS   lcStretchingCostS;

  cpArcIterator lcCurrentArc = agGraph.arcs().begin();
  cpArcIterator lcLastArc    = agGraph.arcs().end();

  tyInteger lcIteration = 0;
  tyReal    lcMaximum   = 0.0;

  private_area::clConformity<tuGraph> lcConformity;

  // Compatible Tension Search //
  graphProblemTensionAlgorithm::clCompatibleTensionAlgo<tuGraph>::defaultRun(agGraph);
  if (agGraph.solved()==false) return (-1);
  agGraph.solved()=false;
  nullFlow(agGraph);

  // Maximum Cost Value Search //
  while (lcCurrentArc!=lcLastArc) {
   lcCost=(*lcCurrentArc).second->data().shrinkingCost();
   if (lcCost>lcMaximum) lcMaximum=lcCost;
   lcShrinkingCostS.push_back(lcCost);
   lcCost=(*lcCurrentArc).second->data().stretchingCost();
   if (lcCost>lcMaximum) lcMaximum=lcCost;
   lcStretchingCostS.push_back(lcCost);
   lcCurrentArc++;
  }

  // Precision Computation //
  lcCounter=tyInteger(standard::floor(std::log(lcMaximum)/std::log(2.0)));
  lcPrecision=1;

  while (lcCounter>0) {
   lcCounter--;
   lcPrecision*=2;
  }

  // Problem Resolution //
  while (lcPrecision>=1) {

   // Costs Setting //
   lcCurrentArc=agGraph.arcs().begin();
   lcCounter=0;

   while (lcCurrentArc!=lcLastArc) {
    if (lcPrecision==1) {
     (*lcCurrentArc).second->data().shrinkingCost()=lcShrinkingCostS[lcCounter];
     (*lcCurrentArc).second->data().stretchingCost()=lcStretchingCostS[lcCounter];
    }
    else {
     (*lcCurrentArc).second->data().shrinkingCost()=
     standard::floor(lcShrinkingCostS[lcCounter]/tyReal(lcPrecision));

     (*lcCurrentArc).second->data().stretchingCost()=
     standard::floor(lcStretchingCostS[lcCounter]/tyReal(lcPrecision));
    }

    (*lcCurrentArc).second->data().flow()*=2.0;
    lcCounter++;
    lcCurrentArc++;
   }

   // Conforming //
   lcCurrentArc=agGraph.arcs().begin();

   while (lcCurrentArc!=lcLastArc) {
    lcIteration+=private_area::conformArc((*(*lcCurrentArc).second),lcConformity);
    lcCurrentArc++;
   }

   lcPrecision/=2;
  }

  agGraph.solved()=true;
  return (lcIteration);
 }
}

// C a p a c i t y S c a l e A l g o  Implementation //---------------------------------------------
namespace public_area {
 //----------------------------------------------------------------------------------------------Run
 /*METHOD clCapacityScaleAlgo */ /* Solves the minimum cost tension problem of a graph. */
 template <tdGraph> tyInteger clCapacityScaleAlgo<tuGraph>::run(clGraph<tuGraph> & agGraph) const {
  typedef typename clGraph<tuGraph>::cpArcX::const_iterator cpArcIterator;
  typedef std_vector(tyReal)                                clRealS;

  tyInteger lcCounter;
  clRealS   lcMaximumS;
  clRealS   lcMinimumS;
  clRealS   lcOptimumS;
  tyInteger lcPrecision;
  tyReal    lcTension;

  cpArcIterator lcCurrentArc = agGraph.arcs().begin();
  cpArcIterator lcLastArc    = agGraph.arcs().end();

  tyInteger lcIteration = 0;
  tyReal    lcMaximum   = 0.0;

  private_area::clConformity<tuGraph> lcConformity;

  // Maximum Cost Value Search //
  while (lcCurrentArc!=lcLastArc) {
   lcTension=(*lcCurrentArc).second->data().maximum();
   if (lcTension>lcMaximum) lcMaximum=lcTension;
   lcMaximumS.push_back(lcTension);
   lcMinimumS.push_back((*lcCurrentArc).second->data().minimum());
   lcOptimumS.push_back((*lcCurrentArc).second->data().optimum());
   lcCurrentArc++;
  }

  // Precision Computation //
  lcCounter=tyInteger(standard::floor(std::log(lcMaximum)/std::log(2.0)));
  lcPrecision=1;

  while (lcCounter>0) {
   lcCounter--;
   lcPrecision*=2;
  }

  // Capacity Setting //
  lcCurrentArc=agGraph.arcs().begin();
  lcCounter=0;

  while (lcCurrentArc!=lcLastArc) {
   if (lcPrecision==1) {
    (*lcCurrentArc).second->data().maximum()=lcMaximumS[lcCounter];
    (*lcCurrentArc).second->data().minimum()=lcMinimumS[lcCounter];
    (*lcCurrentArc).second->data().optimum()=lcOptimumS[lcCounter];
   }
   else {
    (*lcCurrentArc).second->data().maximum()=
    standard::ceiling(lcMaximumS[lcCounter]/tyReal(lcPrecision));

    (*lcCurrentArc).second->data().minimum()=
    standard::floor(lcMinimumS[lcCounter]/tyReal(lcPrecision));

    (*lcCurrentArc).second->data().optimum()=
    standard::floor(lcOptimumS[lcCounter]/tyReal(lcPrecision));
   }

   lcCounter++;
   lcCurrentArc++;
  }

  // Compatible Tension Search //
  graphProblemTensionAlgorithm::clCompatibleTensionAlgo<tuGraph>::defaultRun(agGraph);
  if (agGraph.solved()==false) return (-1);
  agGraph.solved()=false;
  nullFlow(agGraph);

  // Problem Resolution //
  while (lcPrecision>=1) {
   // Conforming //
   lcCurrentArc=agGraph.arcs().begin();

   while (lcCurrentArc!=lcLastArc) {
    try { lcIteration+=private_area::conformArc((*(*lcCurrentArc).second),lcConformity); }
    catch (ctError &) { return (-1); }
    lcCurrentArc++;
   }

   lcPrecision/=2;

   // Capacity Setting //
   if (lcPrecision>0) {
    lcCurrentArc=agGraph.arcs().begin();
    lcCounter=0;

    while (lcCurrentArc!=lcLastArc) {
     if (lcPrecision==1) {
      (*lcCurrentArc).second->data().maximum()=lcMaximumS[lcCounter];
      (*lcCurrentArc).second->data().minimum()=lcMinimumS[lcCounter];
      (*lcCurrentArc).second->data().optimum()=lcOptimumS[lcCounter];
     }
     else {
      (*lcCurrentArc).second->data().maximum()=
      standard::ceiling(lcMaximumS[lcCounter]/tyReal(lcPrecision));

      (*lcCurrentArc).second->data().minimum()=
      standard::floor(lcMinimumS[lcCounter]/tyReal(lcPrecision));

      (*lcCurrentArc).second->data().optimum()=
      standard::floor(lcOptimumS[lcCounter]/tyReal(lcPrecision));
     }

     (*lcCurrentArc).second->data().tension()*=2.0;
     lcCounter++;
     lcCurrentArc++;
    }
   }
  }

  agGraph.solved()=true;
  return (lcIteration);
 }
}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef tdGraph
#undef tuGraph
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// G r a p h _ p r o b l e m                                                         Implementation
// M i n _ c o s t _ t e n s i o n
// C o n f o r m i n g _ l i n e a r
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU Library General Public License for more details (http://www.gnu.org).

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "graph_problem/min_cost_tension/conforming_linear.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define GRAPH_PROBLEM_MIN_COST_TENSION_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/graph_problem/min_cost_tension/conforming_linear.hpp> /*INTERFACE*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  graphProblemMinCostTensionConformingLinear
#define private_area graphProblemMinCostTensionConformingLinear_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

static_module_name("Graph_problem/Min_cost_tension/Conforming_linear");

// Initialization //--------------------------------------------------------------------------------

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Static Members //--------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Implementation //----------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Implementation //-------------------------------------------------------------------------
namespace {}

// End //-------------------------------------------------------------------------------------------
}