Skip to content

Commit

Permalink
fixed encoder and fixed ranging problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ToKiNoBug committed Jul 28, 2022
1 parent e68d01c commit 3f6238c
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 23 deletions.
4 changes: 2 additions & 2 deletions HeuristicFlow/src/AOS/InternalHeaderCheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ This file is part of HeuristicFlow.
*/

#ifndef HEU_AOS_
//#error \
"Please include HeuristicFlow/AOS instead of including headers inside the src directory directly."
//#error "Please include HeuristicFlow/AOS instead of including headers inside the src directory
// directly."
#endif // #ifndef HEU_AOS_
7 changes: 3 additions & 4 deletions HeuristicFlow/src/EAGlobal/BoxConstraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class BoxBase {
} else if constexpr (std::is_integral_v<Scalar_t>) {
at(*v, idx) = randIdx<Scalar_t>(static_cast<const Derived*>(this)->min(idx),
1 + static_cast<const Derived*>(this)->max(idx));
// applyConstraint(v);
#warning goes out of range, don't know how to fix.
} else {
at(*v, idx) = Scalar_t(randD(static_cast<const Derived*>(this)->min(idx),
static_cast<const Derived*>(this)->max(idx)));
Expand Down Expand Up @@ -488,8 +486,9 @@ class FixedDiscreteBox17
public std::conditional_t<array_traits<Var>::isVector, empytStruct0,
MatBoxBase<FixedDiscreteBox17<Var, _minCode, _maxCode>, Var>>,
public internal::SquareBoxSizeBody<Var> {
public:
static constexpr double _minCT = ::heu::decode(_minCode);
static constexpr double _maxCT = ::heu::decode(_minCode);
static constexpr double _maxCT = ::heu::decode(_maxCode);
static_assert(_minCT < _maxCT);
static constexpr BoxShape Shape = BoxShape::SQUARE_BOX;
using Var_t = Var;
Expand Down Expand Up @@ -657,7 +656,7 @@ class FixedContinousBox17 : public FixedDiscreteBox17<Var, _minCode, _maxCode> {
using Var_t = Var;

static constexpr double _deltaCT = decode(_deltaCode);
static_assert(_deltaCode > 0);
static_assert(_deltaCT > 0);

inline constexpr Scalar_t delta(const int = 0) const noexcept { return _deltaCT; }
};
Expand Down
18 changes: 8 additions & 10 deletions HeuristicFlow/src/Global/ConvertDoubleAndBinCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,11 @@ constexpr uint64_t exponentCode(double fl) { return uint64_t(exponentVal(fl)) <<
constexpr double absValWithExponentCode0(double fl) {
fl = absVal(fl);

if (exponentVal(fl) != 0) { // erase the 1+ part
int N = int(exponentVal(fl)) - 1023;

fl -= expOf2AtCompileTime(N);

while (exponentVal(fl) != 0) {
fl /= 2;
}
if (exponentVal(fl) == 0) {
return fl;
}

return fl;
return (fl * expOf2AtCompileTime(1023 - exponentVal(fl)) - 1) * expOf2AtCompileTime(-1022);
}

constexpr uint64_t mantissaCode(double fl) {
Expand Down Expand Up @@ -218,7 +212,11 @@ constexpr uint64_t mantissaCode(double fl) {
* \return constexpr binCode64 The binary code.
*/
constexpr binCode64 encode(const double d) {
if (std::isnan(d)) {
if (

!(d >= 0 || d <= 0) // if a number is neither negative nor non-negative, it is nan

) {
return binCode64((0ULL) | (0b11111111111ULL << 52) | (1ULL << 51));
}

Expand Down
2 changes: 1 addition & 1 deletion HeuristicFlow/src/Global/Randoms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ inline int_t randIdx(int_t size) noexcept {
template <typename int_t>
inline int_t randIdx(int_t min, int_t max_plus_1) noexcept {
static_assert(std::is_integral<int_t>::value, "int_t must be integer");
return int_t((max_plus_1 - min) * randF() + min);
return int_t((max_plus_1 - min) * randF()) + min;
}

inline double normD() noexcept {
Expand Down
24 changes: 18 additions & 6 deletions HeuristicFlow/src/Global/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,19 @@ template <class T>
struct getStdVectorOrArraySizeCT {
private:
using scalar_t = typename toElement<T>::type;
static constexpr bool isSizeFixed = !std::is_same<std::vector<scalar_t>, T>::value;

struct isNotStdArray_t {};

template <class U>
static decltype(U().resize(10), isNotStdArray_t()) fun(const U&) {
return isNotStdArray_t();
}

static int fun(...) { return 0; }

private:
static constexpr bool isSizeFixed = !std::is_same_v<isNotStdArray_t, decltype(fun(T()))>;
//! std::is_same<std::vector<scalar_t>, T>::value;

public:
static constexpr int value = (isSizeFixed) ? (stdArraySizeCT<T>::value) : (Eigen::Dynamic);
Expand All @@ -219,15 +231,15 @@ struct getStdVectorOrArraySizeCT {
};

template <class T, bool isTEigenClass>
struct getSizeCTOfAnyVector_v {
struct __impl_getSizeOfAnyVector {
// here implements when isTEigenClass==true
static constexpr int value = getEigenClassSizeCT<T>::value;
static constexpr int rowsCT = getEigenClassSizeCT<T>::rowsCT;
static constexpr int colsCT = getEigenClassSizeCT<T>::colsCT;
};

template <class T>
struct getSizeCTOfAnyVector_v<T, false> {
struct __impl_getSizeOfAnyVector<T, false> {
static constexpr int value = getStdVectorOrArraySizeCT<T>::value;
static constexpr int rowsCT = getStdVectorOrArraySizeCT<T>::rowsCT;
static constexpr int colsCT = getStdVectorOrArraySizeCT<T>::colsCT;
Expand All @@ -237,9 +249,9 @@ struct getSizeCTOfAnyVector_v<T, false> {
template <class T>
struct getSizeCTOfAnyVector {
static constexpr bool isTEigenClass = isEigenClass<T>::value;
static constexpr int value = getSizeCTOfAnyVector_v<T, isTEigenClass>::value;
static constexpr int rowsCT = getSizeCTOfAnyVector_v<T, isTEigenClass>::rowsCT;
static constexpr int colsCT = getSizeCTOfAnyVector_v<T, isTEigenClass>::colsCT;
static constexpr int value = __impl_getSizeOfAnyVector<T, isTEigenClass>::value;
static constexpr int rowsCT = __impl_getSizeOfAnyVector<T, isTEigenClass>::rowsCT;
static constexpr int colsCT = __impl_getSizeOfAnyVector<T, isTEigenClass>::colsCT;
};
} // namespace

Expand Down
23 changes: 23 additions & 0 deletions test/Boxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This file is part of HeuristicFlow.
#include <HeuristicFlow/EAGlobal>

#include <iostream>
#include "Eigen/src/Core/Array.h"
using namespace Eigen;
using namespace std;

Expand Down Expand Up @@ -172,6 +173,28 @@ void test_Box() {
box5.initialize(&mat5);

cout << "initialized by box5 : \n" << mat5 << endl;

cout << "\n\n\n\n\nTesting box6 : Fixed BoxXXf (in range [-10,5], delta = 0.5)" << endl;

heu::FixedContinousBox17<Eigen::ArrayXXd, heu::encode(-10), heu::encode(5), heu::encode(0.5)>
box6;
box6.setDimensions(3, 4);

Eigen::ArrayXXd mat6;

cout << "box6.min() = " << box6.min() << endl;
cout << "box6.max() = " << box6.max() << endl;
cout << "box6.delta() = " << box6.delta() << endl;

cout << "size of box6 = " << sizeof(box6) << endl;

box6.initialize(&mat6);

cout << "initialized : \n" << mat6 << endl;

box6.applyDelta(&mat6);

cout << "after delta : \n" << mat6 << endl;
}

int main() {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_executable(AOS_Rastrigin AOS_Rastrigin.cpp)
add_executable(testFunctions testFunctions.cpp)
add_executable(MinMaxCompileTime MinMaxCompileTime.cpp)
add_executable(SimpleMatrix simpleMatrix.cpp)
add_executable(TestEncoder testEncoder.cpp)

find_package(OpenMP)

Expand Down
62 changes: 62 additions & 0 deletions test/testEncoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <HeuristicFlow/Global>

using namespace std;
using namespace heu;

void printBin(const uint64_t bin, bool noSpace = false) {
char str[67] = "";

uint64_t mask = 1ULL << 63;

for (int idx = 0; idx < 64; idx++) {
str[idx] = (mask & bin) == 0 ? '0' : '1';
mask = mask >> 1;
}

for (int idx = 0; idx < 64; idx++) {
printf("%c", str[idx]);
if (!noSpace) {
if (idx == 0 || idx == 11) {
printf("%c", ' ');
}
}
}

printf("\n");
}

inline void printBin(const double fl, bool noSpace = false) {
printBin(reinterpret_cast<const uint64_t &>(fl), noSpace);
}

int main() {
printf("%s\n", "Correct val of 1.52e-320 : ");
printBin(1.52e-320);

printf("%s\n", "computed val of 1.52e-320 : ");
printBin(encode(1.52e-320));

printf("%s\n", "Correct val of 3.14e-100 : ");
printBin(3.14e-100);

printf("%s\n", "computed code of of 3.14e-100 : ");
printBin(encode(3.14e-100));

printf("%s\n", "Correct val of 1.507 : ");
printBin(1.507);

printf("%s\n", "computed code of of 1.507 : ");
printBin(encode(1.507));

printf("%s\n", "Correct val of 3.14 : ");
printBin(3.14);

printf("%s\n", "computed code of of 3.14 : ");
printBin(encode(3.14));

printf("%s\n", "Correct val of 3.14e100 : ");
printBin(3.14e100);

printf("%s\n", "computed code of of 3.14e100 : ");
printBin(encode(3.14e100));
}

0 comments on commit 3f6238c

Please sign in to comment.