100 #ifndef __GCOPTIMIZATION_H__ 101 #define __GCOPTIMIZATION_H__ 104 #if defined(_MSC_VER) && _MSC_VER < 1400 105 #error Requires Visual C++ 2005 (VC8) compiler or later. 110 #pragma warning(push, 0) // no warnings from includes 111 #pragma warning(disable: 4706) // assignment within conditional expression 112 #pragma warning(disable:4701) // potentially uninitialized local variable used 114 #pragma GCC diagnostic push 115 #pragma GCC diagnostic ignored "-Wreorder" 143 #ifdef _MSC_EXTENSIONS 144 #define OLGA_INLINE __forceinline 146 #define OLGA_INLINE inline 149 #ifndef GCO_MAX_ENERGYTERM 150 #define GCO_MAX_ENERGYTERM 10000000 // maximum safe coefficient to avoid integer overflow 155 #if defined(GCO_ENERGYTYPE) && !defined(GCO_ENERGYTERMTYPE) 156 #define GCO_ENERGYTERMTYPE GCO_ENERGYTYPE 158 #if !defined(GCO_ENERGYTYPE) && defined(GCO_ENERGYTERMTYPE) 159 #define GCO_ENERGYTYPE GCO_ENERGYTERMTYPE 171 #ifdef GCO_ENERGYTYPE 175 #ifdef GCO_ENERGYTYPE32 176 typedef int EnergyType;
186 typedef EnergyTermType (*SmoothCostFn)(SiteID s1, SiteID s2, LabelID l1, LabelID l2);
187 typedef EnergyTermType (*DataCostFn)(SiteID s, LabelID l);
188 typedef EnergyTermType (*SmoothCostFnExtra)(SiteID s1, SiteID s2, LabelID l1, LabelID l2,
void *);
189 typedef EnergyTermType (*DataCostFnExtra)(SiteID s, LabelID l,
void *);
196 EnergyType expansion(
int max_num_iterations=-1);
199 bool alpha_expansion(LabelID alpha_label);
203 EnergyType swap(
int max_num_iterations=-1);
206 void alpha_beta_swap(LabelID alpha_label, LabelID beta_label);
211 void alpha_beta_swap(LabelID alpha_label, LabelID beta_label, SiteID *alphaSites,
212 SiteID alpha_size, SiteID *betaSites, SiteID beta_size);
218 void setDataCost(DataCostFn fn);
219 void setDataCost(DataCostFnExtra fn,
void *extraData);
220 void setDataCost(EnergyTermType *dataArray);
221 void setDataCost(SiteID s, LabelID l, EnergyTermType e);
224 virtual EnergyTermType compute(SiteID s, LabelID l) = 0;
236 void setSmoothCost(SmoothCostFn fn);
237 void setSmoothCost(SmoothCostFnExtra fn,
void *extraData);
238 void setSmoothCost(LabelID l1, LabelID l2, EnergyTermType e);
239 void setSmoothCost(EnergyTermType *smoothArray);
242 virtual EnergyTermType compute(SiteID s1, SiteID s2, LabelID l1, LabelID l2) = 0;
247 void setLabelCost(EnergyTermType cost);
248 void setLabelCost(EnergyTermType* costArray);
249 void setLabelSubsetCost(LabelID* labels, LabelID numLabels, EnergyTermType cost);
252 LabelID whatLabel(SiteID site);
253 void whatLabel(SiteID start, SiteID count, LabelID* labeling);
256 void setLabel(SiteID site, LabelID label);
263 void setLabelOrder(
bool isRandom);
264 void setLabelOrder(
const LabelID* order, LabelID size);
267 EnergyType compute_energy();
270 EnergyType giveDataEnergy();
271 EnergyType giveSmoothEnergy();
272 EnergyType giveLabelEnergy();
275 SiteID numSites()
const;
276 LabelID numLabels()
const;
329 void (
GCoptimization::*m_setupDataCostsExpansion)(SiteID,LabelID,EnergyT*,SiteID*);
330 void (
GCoptimization::*m_setupSmoothCostsExpansion)(SiteID,LabelID,EnergyT*,SiteID*);
331 void (
GCoptimization::*m_setupDataCostsSwap)(SiteID,LabelID,LabelID,EnergyT*,SiteID*);
332 void (
GCoptimization::*m_setupSmoothCostsSwap)(SiteID,LabelID,LabelID,EnergyT*,SiteID*);
336 void (*m_datacostFnDelete)(
void* f);
337 void (*m_smoothcostFnDelete)(
void* f);
341 virtual void giveNeighborInfo(SiteID site, SiteID *numSites, SiteID **neighbors, EnergyTermType **weights)=0;
342 virtual void finalizeNeighbors() = 0;
346 : m_array(theArray), m_num_labels(num_labels){}
349 const EnergyTermType*
const m_array;
350 const LabelID m_num_labels;
357 const DataCostFn m_fn;
364 const DataCostFnExtra m_fn;
370 : m_array(theArray), m_num_labels(num_labels){}
371 OLGA_INLINE EnergyTermType
compute(SiteID , SiteID , LabelID l1, LabelID l2){
return m_array[l1*m_num_labels+l2];}
373 const EnergyTermType*
const m_array;
374 const LabelID m_num_labels;
380 OLGA_INLINE EnergyTermType
compute(SiteID s1, SiteID s2, LabelID l1, LabelID l2){
return m_fn(s1,s2,l1,l2);}
382 const SmoothCostFn m_fn;
387 : m_fn(fn),m_extraData(extraData){}
388 OLGA_INLINE EnergyTermType
compute(SiteID s1, SiteID s2, LabelID l1, LabelID l2){
return m_fn(s1,s2,l1,l2,m_extraData);}
390 const SmoothCostFnExtra m_fn;
395 OLGA_INLINE EnergyTermType
compute(SiteID, SiteID, LabelID l1, LabelID l2){
return l1 != l2 ? (EnergyTermType)1 : (EnergyTermType)0;}
409 static const int cLogSitesPerBucket = 9;
410 static const int cSitesPerBucket = (1 << cLogSitesPerBucket);
411 static const size_t cDataCostPtrMask = ~(
sizeof(
SparseDataCost)-1);
412 static const ptrdiff_t cLinearSearchSize = 64/
sizeof(
SparseDataCost);
414 struct DataCostBucket {
426 EnergyTermType compute(SiteID s, LabelID l);
427 SiteID queryActiveSitesExpansion(LabelID alpha_label,
const LabelID* labeling, SiteID* activeSites);
445 OLGA_INLINE iterator end(LabelID label)
const {
return m_buckets[label*m_buckets_per_label + m_buckets_per_label-1].end; }
448 EnergyTermType search(DataCostBucket& b, SiteID s);
449 const SiteID m_num_sites;
450 const LabelID m_num_labels;
451 const int m_buckets_per_label;
452 mutable DataCostBucket* m_buckets;
455 template <
typename DataCostT> SiteID queryActiveSitesExpansion(LabelID alpha_label, SiteID* activeSites);
456 template <
typename DataCostT>
void setupDataCostsExpansion(SiteID size,LabelID alpha_label,EnergyT *e,SiteID *activeSites);
457 template <
typename DataCostT>
void setupDataCostsSwap(SiteID size,LabelID alpha_label,LabelID beta_label,EnergyT *e,SiteID *activeSites);
458 template <
typename SmoothCostT>
void setupSmoothCostsExpansion(SiteID size,LabelID alpha_label,EnergyT *e,SiteID *activeSites);
459 template <
typename SmoothCostT>
void setupSmoothCostsSwap(SiteID size,LabelID alpha_label,LabelID beta_label,EnergyT *e,SiteID *activeSites);
460 template <
typename DataCostT>
void applyNewLabeling(EnergyT *e,SiteID *activeSites,SiteID size,LabelID alpha_label);
461 template <
typename DataCostT>
void updateLabelingDataCosts();
462 template <
typename UserFunctor>
void specializeDataCostFunctor(
const UserFunctor f);
463 template <
typename UserFunctor>
void specializeSmoothCostFunctor(
const UserFunctor f);
465 EnergyType setupLabelCostsExpansion(SiteID size,LabelID alpha_label,EnergyT *e,SiteID *activeSites);
466 void updateLabelingInfo(
bool updateCounts=
true,
bool updateActive=
true,
bool updateCosts=
true);
469 void addterm1_checked(EnergyT *e,VarID i,EnergyTermType e0,EnergyTermType e1);
470 void addterm1_checked(EnergyT *e,VarID i,EnergyTermType e0,EnergyTermType e1,EnergyTermType w);
471 void addterm2_checked(EnergyT *e,VarID i,VarID j,EnergyTermType e00,EnergyTermType e01,EnergyTermType e10,EnergyTermType e11,EnergyTermType w);
474 template <
typename SmoothCostT> EnergyType giveSmoothEnergyInternal();
475 template <
typename Functor>
static void deleteFunctor(
void* f) {
delete reinterpret_cast<Functor*
>(f); }
477 static void handleError(
const char *
message);
478 static void checkInterrupt();
482 EnergyType oneExpansionIteration();
483 EnergyType oneSwapIteration();
484 void printStatus1(
const char* extraMsg=0);
485 void printStatus1(
int cycle,
bool isSwap,
gcoclock_t ticks0);
486 void printStatus2(
int alpha,
int beta,
int numVars,
gcoclock_t ticks0);
488 void permuteLabelTable();
490 template <
typename DataCostT>
bool solveSpecialCases(EnergyType& energy);
491 template <
typename DataCostT> EnergyType solveGreedy();
498 template <
typename DataCostT>
501 GreedyIter(DataCostT& dc, SiteID numSites)
502 : m_dc(dc), m_site(0), m_numSites(numSites), m_label(0), m_lbegin(0), m_lend(0)
505 OLGA_INLINE void start(
const LabelID* labels, LabelID labelCount=1)
507 m_site = labelCount ? 0 : m_numSites;
508 m_label = m_lbegin = labels;
509 m_lend = labels + labelCount;
512 OLGA_INLINE SiteID label()
const {
return *m_label; }
513 OLGA_INLINE bool done()
const {
return m_site == m_numSites; }
519 if (++m_label >= m_lend) {
525 OLGA_INLINE EnergyTermType compute()
const {
return m_dc.compute(m_site,*m_label); }
526 OLGA_INLINE SiteID feasibleSites()
const {
return m_numSites; }
531 const SiteID m_numSites;
532 const LabelID* m_label;
533 const LabelID* m_lbegin;
534 const LabelID* m_lend;
549 void setSmoothCostVH(EnergyTermType *smoothArray, EnergyTermType *vCosts, EnergyTermType *hCosts);
552 virtual void giveNeighborInfo(
SiteID site,
SiteID *numSites,
SiteID **neighbors, EnergyTermType **weights);
553 virtual void finalizeNeighbors();
554 EnergyTermType m_unityWeights[4];
561 EnergyTermType *m_neighborsWeights;
564 void computeNeighborWeights(EnergyTermType *vCosts,EnergyTermType *hCosts);
582 void setNeighbors(SiteID site1, SiteID site2,
EnergyTermType weight=1);
589 void setAllNeighbors(SiteID *numNeighbors,SiteID **neighborsIndexes,
EnergyTermType **neighborsWeights);
592 virtual void giveNeighborInfo(SiteID site, SiteID *numSites, SiteID **neighbors,
EnergyTermType **weights);
593 virtual void finalizeNeighbors();
597 typedef struct NeighborStruct{
603 bool m_needToFinishSettingNeighbors;
604 SiteID **m_neighborsIndexes;
606 bool m_needTodeleteNeighbors;
627 assert(label >= 0 && label < m_num_labels && site >= 0 && site < m_num_sites);
628 m_labeling[site] = label;
629 m_labelingInfoDirty =
true;
634 assert(site >= 0 && site < m_num_sites);
635 return m_labeling[site];
640 #pragma warning(pop) // no warnings from includes 642 #pragma GCC diagnostic pop LabelCostIter * next
Definition: GCoptimization.h:297
Definition: GCoptimization.h:228
int m_stepsThisCycle
Definition: GCoptimization.h:306
LabelCost * next
Definition: GCoptimization.h:290
int LabelID
Definition: GCoptimization.h:184
int EnergyTermType
Definition: GCoptimization.h:180
~LabelCost()
Definition: GCoptimization.h:286
VarID aux
Definition: GCoptimization.h:289
OLGA_INLINE EnergyTermType compute(SiteID s, LabelID l)
Definition: GCoptimization.h:355
LabelID * m_labeling
Definition: GCoptimization.h:302
EnergyTermType * m_smoothcostIndividual
Definition: GCoptimization.h:310
const char * message
Definition: GCoptimization.h:129
LabelID whatLabel(SiteID site)
Definition: GCoptimization.h:632
OLGA_INLINE bool operator!=(const iterator &b) const
Definition: GCoptimization.h:436
int m_weightedGraph
Definition: GCoptimization.h:555
LabelCost * node
Definition: GCoptimization.h:296
Definition: GCoptimization.h:295
VarID SiteID
Definition: GCoptimization.h:185
Definition: GCoptimization.h:285
EnergyT::Var VarID
Definition: GCoptimization.h:183
OLGA_INLINE EnergyTermType compute(SiteID s1, SiteID s2, LabelID l1, LabelID l2)
Definition: GCoptimization.h:380
DataCostFnFromFunction(DataCostFn fn)
Definition: GCoptimization.h:354
OLGA_INLINE iterator end(LabelID label) const
Definition: GCoptimization.h:445
Definition: GCoptimization.h:168
SiteID m_num_sites
Definition: GCoptimization.h:301
static void deleteFunctor(void *f)
Definition: GCoptimization.h:475
SiteID * m_lookupSiteVar
Definition: GCoptimization.h:303
Definition: GCoptimization.h:368
OLGA_INLINE iterator()
Definition: GCoptimization.h:431
bool active
Definition: GCoptimization.h:288
void Report()
Definition: GCoptimization.cpp:95
OLGA_INLINE bool operator==(const iterator &b) const
Definition: GCoptimization.h:435
Definition: GCoptimization.h:394
EnergyTermType * m_labelingDataCosts
Definition: GCoptimization.h:311
Definition: GCoptimization.h:377
void setLabel(SiteID site, LabelID label)
Definition: GCoptimization.h:625
LabelID * m_labelTable
Definition: GCoptimization.h:305
LabelID numLabels
Definition: GCoptimization.h:291
OLGA_INLINE SiteID site() const
Definition: GCoptimization.h:433
OLGA_INLINE EnergyTermType compute(SiteID, SiteID, LabelID l1, LabelID l2)
Definition: GCoptimization.h:395
void * m_smoothcostFn
Definition: GCoptimization.h:321
OLGA_INLINE iterator & operator++()
Definition: GCoptimization.h:432
Definition: GCoptimization.h:241
OLGA_INLINE EnergyTermType compute(SiteID, SiteID, LabelID l1, LabelID l2)
Definition: GCoptimization.h:371
SiteID m_numNeighborsTotal
Definition: GCoptimization.h:325
Definition: GCoptimization.h:353
gcoclock_t gcoclock()
Definition: GCoptimization.cpp:54
EnergyTermType cost
Definition: GCoptimization.h:287
#define OLGA_INLINE
Definition: GCoptimization.h:146
SmoothCostFnFromArray(EnergyTermType *theArray, LabelID num_labels)
Definition: GCoptimization.h:369
SiteID * m_numNeighbors
Definition: GCoptimization.h:324
EnergyType m_beforeExpansionEnergy
Definition: GCoptimization.h:322
Definition: GCoptimization.h:403
bool m_labelingInfoDirty
Definition: GCoptimization.h:317
Definition: GCoptimization.h:569
GraphT::node_id Var
Definition: energy.h:83
int m_labelcostCount
Definition: GCoptimization.h:316
level
Definition: DependencyCollector.py:25
Energy< EnergyTermType, EnergyTermType, EnergyType > EnergyT
Definition: GCoptimization.h:182
Definition: GCoptimization.h:429
long long EnergyType
Definition: GCoptimization.h:178
OLGA_INLINE EnergyTermType cost() const
Definition: GCoptimization.h:434
SiteID * m_activeLabelCounts
Definition: GCoptimization.h:313
int m_verbosity
Definition: GCoptimization.h:318
int m_stepsThisCycleTotal
Definition: GCoptimization.h:307
LabelCost * m_labelcostsAll
Definition: GCoptimization.h:314
LabelID m_num_labels
Definition: GCoptimization.h:300
Definition: GCoptimization.h:543
LabelID * labels
Definition: GCoptimization.h:292
gcoclock_t GCO_CLOCKS_PER_SEC
Definition: GCoptimization.h:141
Definition: GCoptimization.h:127
SiteID numSites() const
Definition: GCoptimization.h:615
EnergyTermType * m_datacostIndividual
Definition: GCoptimization.h:309
int m_random_label_order
Definition: GCoptimization.h:308
DataCostFnFromArray(EnergyTermType *theArray, LabelID num_labels)
Definition: GCoptimization.h:345
LabelCostIter ** m_labelcostsByLabel
Definition: GCoptimization.h:315
Definition: GCoptimization.h:344
GCException(const char *m)
Definition: GCoptimization.h:130
SiteID site
Definition: GCoptimization.h:229
OLGA_INLINE iterator begin(LabelID label) const
Definition: GCoptimization.h:444
OLGA_INLINE EnergyTermType compute(SiteID s, LabelID l)
Definition: GCoptimization.h:347
void * m_datacostFn
Definition: GCoptimization.h:320
EnergyTermType cost
Definition: GCoptimization.h:230
void setVerbosity(int level)
Definition: GCoptimization.h:282
Definition: GCoptimization.h:223
Definition: LinkedBlockList.h:21
LabelID numLabels() const
Definition: GCoptimization.h:620
SiteID * m_labelCounts
Definition: GCoptimization.h:312
SmoothCostFnFromFunction(SmoothCostFn fn)
Definition: GCoptimization.h:378
clock_t gcoclock_t
Definition: GCoptimization.h:138