OptimalBase.h 2.39 KB
Newer Older
Masahiko Sakai's avatar
Masahiko Sakai committed
1
/**************************************************************************[OptimalBase.h]
Masahiko Sakai's avatar
Masahiko Sakai committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/

#ifndef OptimalBase_h
#define OptimalBase_h

#include "PbSolver.h"
Masahiko Sakai's avatar
Masahiko Sakai committed
24
#include "Global.h"
Masahiko Sakai's avatar
Masahiko Sakai committed
25 26 27

//=================================================================================================

Masahiko Sakai's avatar
Masahiko Sakai committed
28
enum OptBaseT  { obt_digits, obt_comb, obt_kind };
Masahiko Sakai's avatar
Masahiko Sakai committed
29

Masahiko Sakai's avatar
Masahiko Sakai committed
30 31 32 33 34 35 36 37 38 39 40 41
// returns the cardinarity of the given sorted sequence as a set.
template <class T>
int CountKind_Sort(const vec<T>& seq)
{
  assert(seq.size()>0);
  int i,c=1;
  for(i=0;i<(seq.size()-1);i++){
    if(seq[i]!=seq[i+1])
      c++;
  }
  return(c);
}
Masahiko Sakai's avatar
Masahiko Sakai committed
42

Masahiko Sakai's avatar
Masahiko Sakai committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
template <class T>
int count_weighted_sort(const vec<T>& seq)  // assuming seq is sorted
{ 
  assert(seq.size() > 0);
  double sum = 0;
  int num_succ_dig = 1;
  for(int i = 1; i < seq.size(); i++) {
    if(seq[i-1] == seq[i]) 
      num_succ_dig++;
    else {
      sum += 1.0 / (double)num_succ_dig;
      num_succ_dig = 1;
    }  
  }
  sum += 1.0 / (double)num_succ_dig;
  return (int)(sum*10);
}

//void optimizeBase(vec<Int>& seq, int carry_ins, vec<Int>& rhs, int cost, vec<int>& base, int& cost_bestfound, vec<int>& base_bestfound, bool carry_important);

void optimizeBase(vec<Int>& seq, vec<Int>& rhs, int& cost_bestfound, vec<int>& base_bestfound, OptBaseT obt);
Masahiko Sakai's avatar
Masahiko Sakai committed
64 65

#endif