├── .gitignore ├── makefile ├── src ├── prun.h ├── sym.h ├── move.h ├── solve.h ├── coord.h ├── cubie.h ├── face.cpp ├── face.h ├── cubie.cpp ├── sym.cpp ├── test.cpp ├── coord.cpp ├── main.cpp ├── move.cpp ├── solve.cpp └── prun.cpp ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Project specific 35 | twophase 36 | twophase*.tbl 37 | .depend 38 | .idea 39 | CMakeLists.txt 40 | cmake-build-debug 41 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | C=gcc 2 | CXX=g++ 3 | RM=rm -f 4 | CPPFLAGS=-std=c++11 -lpthread -O3 5 | LDFLAGS= 6 | LDLIBS=-lpthread 7 | 8 | SRCS=$(patsubst %,src/%,main.cpp coord.cpp cubie.cpp face.cpp move.cpp prun.cpp solve.cpp sym.cpp) 9 | OBJS=$(subst .cpp,.o,$(SRCS)) 10 | 11 | all: tool 12 | 13 | tool: $(OBJS) 14 | $(CXX) $(LDFLAGS) -o twophase $(OBJS) $(LDLIBS) 15 | 16 | depend: .depend 17 | 18 | .depend: $(SRCS) 19 | $(RM) ./.depend 20 | $(CXX) $(CPPFLAGS) -MM $^>>./.depend; 21 | 22 | clean: 23 | $(RM) $(OBJS) 24 | 25 | distclean: clean 26 | $(RM) *~ .depend 27 | 28 | include .depend 29 | -------------------------------------------------------------------------------- /src/prun.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Pruning table generation and lookup. 3 | */ 4 | 5 | #ifndef __PRUN__ 6 | #define __PRUN__ 7 | 8 | #include 9 | #include "coord.h" 10 | #include "sym.h" 11 | 12 | namespace prun { 13 | 14 | const int N_FS1TWIST = sym::N_FSLICE1 * coord::N_TWIST; 15 | const int N_CORNUD2 = sym::N_CORNERS * coord::N_UDEDGES2; 16 | const int N_CSLICE2 = coord::N_CORNERS * coord::N_SLICE2; 17 | 18 | #ifdef AX 19 | using prun1 = uint64_t; 20 | #else 21 | using prun1 = uint32_t; 22 | #endif 23 | 24 | extern prun1 *phase1; 25 | extern uint8_t *phase2; 26 | extern uint8_t *precheck; 27 | 28 | int get_phase1(int flip, int slice, int twist, int togo, move::mask& next); 29 | int get_phase2(int corners, int udedges); 30 | int get_precheck(int corners, int slice); 31 | 32 | bool init(bool file = true); 33 | 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/sym.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Symmetry definition and reduction/conjugation tables. 3 | */ 4 | 5 | #ifndef __SYM__ 6 | #define __SYM__ 7 | 8 | #include "coord.h" 9 | #include "cubie.h" 10 | #include "move.h" 11 | 12 | namespace sym { 13 | 14 | const int COUNT = 48; 15 | 16 | #ifdef F5 17 | const int COUNT_SUB = 4; // number of symmetries used for reduction 18 | const int N_FSLICE1 = 255664; 19 | const int N_CORNERS = 10368; 20 | const int ROT = 36; // 90 degree rotation around FB-axis 21 | #else 22 | const int COUNT_SUB = 16; 23 | const int N_FSLICE1 = 64430; 24 | const int N_CORNERS = 2768; 25 | const int ROT = 16; // 120 degree rotation around axis through URF and DLB corner 26 | #endif 27 | 28 | extern cubie::cube cubes[COUNT]; 29 | extern int inv[COUNT]; 30 | extern int effect[COUNT][3]; 31 | 32 | extern int conj_move[move::COUNT][COUNT]; 33 | extern uint16_t conj_twist[coord::N_TWIST][COUNT_SUB]; 34 | extern uint16_t conj_udedges2[coord::N_UDEDGES2][COUNT_SUB]; 35 | 36 | extern uint32_t fslice1_sym[coord::N_FSLICE1]; 37 | extern uint32_t corners_sym[coord::N_CORNERS]; 38 | extern uint32_t fslice1_raw[N_FSLICE1]; 39 | extern uint16_t corners_raw[N_CORNERS]; 40 | extern uint16_t fslice1_selfs[N_FSLICE1]; 41 | extern uint16_t corners_selfs[N_CORNERS]; 42 | 43 | inline bool eff_inv(int eff) { return eff & 1; } 44 | inline bool eff_flip(int eff) { return eff & 2; } 45 | inline int eff_shift(int eff) { return eff >> 2; } 46 | inline int coord_c(int coord) { return coord / COUNT_SUB; } 47 | inline int coord_s(int coord) { return coord % COUNT_SUB; } 48 | 49 | void init(); 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/move.h: -------------------------------------------------------------------------------- 1 | /** 2 | * All kinds of moveset definitions and setup 3 | */ 4 | 5 | #ifndef __MOVE__ 6 | #define __MOVE__ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "cubie.h" 13 | 14 | namespace move { 15 | 16 | using mask = uint64_t; 17 | 18 | #ifdef QT 19 | #ifdef AX 20 | const int COUNT = 30; 21 | const int COUNT1 = 24; 22 | const int split[] = { 23 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 24 | 8, 10, 12, 16, 18, 20 25 | }; // split extra half-turns into quarter-turn (for cleaner phase 2 implementation) 26 | #else 27 | const int COUNT = 16; 28 | const int COUNT1 = 12; 29 | const int split[] = { 30 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 31 | 4, 6, 8, 10 32 | }; 33 | #endif 34 | #else 35 | #ifdef AX 36 | const int COUNT = 45; 37 | const int COUNT1 = 45; 38 | #else 39 | const int COUNT = 18; 40 | const int COUNT1 = 18; 41 | #endif 42 | #endif 43 | 44 | extern std::string names[COUNT]; 45 | extern cubie::cube cubes[COUNT]; 46 | extern int inv[COUNT]; 47 | 48 | extern mask next[COUNT]; // successor moves that should be explored 49 | extern mask next_p1p2[COUNT]; // `next` for phase1 to phase 2 transition 50 | extern mask qt_skip[COUNT]; // to avoid ever trying M^3 = M' in QT mode 51 | 52 | extern mask p1mask; // phase 1 moves 53 | extern mask p2mask; // phase 2 moves 54 | 55 | inline mask bit(int m) { 56 | return mask(1) << m; 57 | } 58 | inline bool in(int m, mask mm) { 59 | return mm & bit(m); 60 | } 61 | 62 | // Convert solution to AXHT; especially useful when solving in AXQT 63 | std::string compress(const std::vector& mseq); 64 | 65 | /* Compute solution lengths in different metrics */ 66 | int len_ht(const std::vector& mseq); 67 | int len_axht(const std::vector& mseq); 68 | int len_qt(const std::vector& mseq); 69 | int len_axqt(const std::vector& mseq); 70 | 71 | void init(); 72 | 73 | } 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/solve.h: -------------------------------------------------------------------------------- 1 | #ifndef __SOLVE__ 2 | #define __SOLVE__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | # include "move.h" 10 | 11 | namespace solve { 12 | 13 | using searchres = std::pair, int>; // moves + search direction 14 | inline bool cmp(const searchres& s1, const searchres& s2) { return s1.first.size() < s2.first.size(); } 15 | 16 | // Container with coords of a starting position 17 | struct coordc { 18 | int flip; 19 | int slice; 20 | int twist; 21 | int uedges; 22 | int dedges; 23 | int corners; 24 | }; 25 | 26 | // Number of search directions 27 | #ifdef F5 28 | const int N_DIRS = 4; 29 | #else 30 | const int N_DIRS = 6; 31 | #endif 32 | 33 | class Engine { 34 | 35 | int n_threads; // number of search threads 36 | int n_splits; // number of sub-searches every search is split into 37 | int n_sols; // number of solutions to find 38 | int max_len; // find solutions with at most this length; -1 means simply search for the full `tlimit` 39 | int tlim; // search for this amount of milliseconds 40 | 41 | coordc dirs[N_DIRS]; // search directions 42 | move::mask masks[move::COUNT1]; // split masks 43 | int depths[N_DIRS]; // current search depths per direction 44 | int splits[N_DIRS]; // current search splits per direction 45 | 46 | bool done; // indicate that we are done 47 | int lenlim; // only look for solution that are strictly shorter than this 48 | std::mutex job_mtx; // thread-safety for selection of the next search task 49 | std::mutex sol_mtx; // thread-safety for reporting a solution 50 | std::priority_queue, decltype(&cmp)> sols {cmp}; // already found solutions 51 | std::vector threads; // search threads 52 | 53 | // Tools for implementing a required timeout 54 | std::mutex tout_mtx; 55 | std::condition_variable tout_cvar; 56 | 57 | public: 58 | Engine( 59 | int n_threads, int tlim, 60 | int n_sols = 1, int max_len = -1, int n_splits = 1 61 | ); 62 | void prepare(); // setup all threads 63 | void solve(const cubie::cube& c, std::vector>& res); // actual solve 64 | void finish(); // wait for all threads to shutdown (mostly for clean program exit) 65 | void report_sol(searchres& sol); // report a solution; never call this from the outside 66 | 67 | void thread(); // search thread 68 | 69 | }; 70 | 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/coord.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Coord definitions, utilities and move tables. 3 | */ 4 | 5 | #ifndef __COORD__ 6 | #define __COORD__ 7 | 8 | #include "cubie.h" 9 | #include "move.h" 10 | 11 | namespace coord { 12 | 13 | const int N_FLIP = 2048; // 2^(12 - 1) 14 | const int N_TWIST = 2187; // 3^(8 - 1) 15 | const int N_SLICE1 = 495; // binom(12, 4) 16 | const int N_FSLICE1 = 1013760; // N_FLIP * N_SLICE 17 | 18 | const int N_SLICE = 11880; // 12! / 8! 19 | const int N_UEDGES = 11880; // 12! / 8! 20 | const int N_DEDGES = 11880; // 12! / 8! 21 | 22 | const int N_SLICE2 = 24; // 4! 23 | const int N_UDEDGES2 = 40320; // 8! 24 | const int N_CORNERS = 40320; // 8! 25 | 26 | const int SLICE1_SOLVED = 494; // SLICE1 is not 0 at the end of phase 1 27 | 28 | extern uint16_t move_flip[N_FLIP][move::COUNT]; 29 | extern uint16_t move_twist[N_TWIST][move::COUNT]; 30 | extern uint16_t move_edges4[N_SLICE][move::COUNT]; 31 | extern uint16_t move_corners[N_CORNERS][move::COUNT]; 32 | extern uint16_t move_udedges2[N_UDEDGES2][move::COUNT]; // primarily for faster phase 2 table generation 33 | 34 | int get_flip(const cubie::cube& c); 35 | int get_twist(const cubie::cube& c); 36 | int get_slice(const cubie::cube& c); 37 | int get_uedges(const cubie::cube& c); 38 | int get_dedges(const cubie::cube& c); 39 | int get_corners(const cubie::cube& c); 40 | 41 | void set_flip(cubie::cube& c, int flip); 42 | void set_twist(cubie::cube& c, int twist); 43 | void set_slice(cubie::cube& c, int slice); 44 | void set_uedges(cubie::cube& c, int uedges); 45 | void set_dedges(cubie::cube& c, int dedges); 46 | void set_corners(cubie::cube& c, int corners); 47 | 48 | int get_slice1(const cubie::cube& c); // faster table generation 49 | void set_slice1(cubie::cube& c, int slice1); 50 | int get_udedges2(const cubie::cube& c); 51 | void set_udedges2(cubie::cube& c, int udedges2); 52 | inline int merge_udedges2(int uedges, int dedges) { return 24 * uedges + (dedges % 24); }; 53 | 54 | inline int slice_to_slice1(int slice) { return slice / 24; } 55 | inline int slice1_to_slice(int slice1) { return 24 * slice1; } 56 | inline int slice_to_slice2(int slice) { return slice - N_SLICE2 * SLICE1_SOLVED; } 57 | inline int slice2_to_slice(int slice2) { return slice2 + N_SLICE2 * SLICE1_SOLVED; } 58 | inline int fslice1(int flip, int slice1) { return N_FLIP * slice1 + flip; } 59 | inline int fslice1_to_flip(int fslice1) { return fslice1 % N_FLIP; } 60 | inline int fslice1_to_slice1(int fslice1) { return fslice1 / N_FLIP; } 61 | 62 | void init(); 63 | 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /src/cubie.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Cubie definitions, cubie-cube representation + methods for manipulating it 3 | */ 4 | 5 | #ifndef __CUBIE__ 6 | #define __CUBIE__ 7 | 8 | #include 9 | 10 | namespace cubie { 11 | 12 | /* All cubie definitions are plain integers for making uniform handling much easier */ 13 | 14 | namespace corner { // definition of corner cubies 15 | const int COUNT = 8; 16 | 17 | const int URF = 0; 18 | const int UFL = 1; 19 | const int ULB = 2; 20 | const int UBR = 3; 21 | const int DFR = 4; 22 | const int DLF = 5; 23 | const int DBL = 6; 24 | const int DRB = 7; 25 | 26 | const std::string NAMES[] = { 27 | "URF", "UFL", "ULB", "UBR", "DFR", "DLF", "DBL", "DRB" 28 | }; 29 | } 30 | using namespace corner; 31 | 32 | namespace edge { // definition of edge cubies 33 | const int COUNT = 12; 34 | 35 | const int UR = 0; 36 | const int UF = 1; 37 | const int UL = 2; 38 | const int UB = 3; 39 | const int DR = 4; 40 | const int DF = 5; 41 | const int DL = 6; 42 | const int DB = 7; 43 | // SLICE-edges last s.t. UDEDGES2 is easier to handle 44 | const int FR = 8; 45 | const int FL = 9; 46 | const int BL = 10; 47 | const int BR = 11; 48 | 49 | const std::string NAMES[] = { 50 | "UR", "UF", "UL", "UB", "DR", "DF", "DL", "DB", "FR", "FL", "BL", "BR" 51 | }; 52 | } 53 | using namespace edge; 54 | 55 | struct cube { 56 | int cperm[corner::COUNT]; // corner cubie permutation 57 | int eperm[edge::COUNT]; // edge cubie permutation 58 | int cori[corner::COUNT]; // corner cubie orientation; 0 if U/D-facelet on U/D-face; 1 clockwise rot; 2 c-clock 59 | int eori[edge::COUNT]; // edge cubie orientation; 0 if U/D-facelet on U/D-face or same for F/B for slice edges 60 | }; 61 | 62 | const cube SOLVED_CUBE = { 63 | {URF, UFL, ULB, UBR, DFR, DLF, DBL, DRB}, 64 | {UR, UF, UL, UB, DR, DF, DL, DB, FR, FL, BL, BR}, 65 | {}, {} 66 | }; // cubie-cube in solved state 67 | 68 | /* Explicitly pass result cube to avoid unnecessary copying during table generation */ 69 | 70 | namespace corner { 71 | void mul(const cube& c1, const cube& c2, cube& into); // multiply only corner cubies 72 | } 73 | namespace edge { 74 | void mul(const cube& c1, const cube& c2, cube& into); // multiply only edge cubies 75 | } 76 | 77 | void mul(const cube& c1, const cube& c2, cube& into); // fully multiply two cubes 78 | void inv(const cube& c, cube& into); // compute the inverse cube 79 | void shuffle(cube& c); // generate a uniformly random cube 80 | int check(const cube& c); // check a cube for being solvable 81 | 82 | bool operator==(const cube& c1, const cube& c2); 83 | bool operator!=(const cube& c1, const cube& c2); 84 | 85 | } 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /src/face.cpp: -------------------------------------------------------------------------------- 1 | #include "face.h" 2 | 3 | #include 4 | #include 5 | 6 | namespace face { 7 | 8 | // True modulo function that also works properly for negative numbers 9 | int mod(int a, int m) { 10 | return a > 0 ? a % m : (a % m + m) % m; 11 | } 12 | 13 | // Converts a cubelet and its orientation into a single unique code-number 14 | int encode(const std::string& cubelet, int ori) { 15 | int res = 0; 16 | for (int i = 0; i < cubelet.size(); i++) 17 | res = color::COUNT * res + color::FROM_NAME.at(cubelet[mod(i - ori, cubelet.size())]); 18 | return res; 19 | } 20 | 21 | // Map code to cubie ID and orientation 22 | std::unordered_map> corners; 23 | std::unordered_map> edges; 24 | 25 | void init() { 26 | for (int corner = 0; corner < cubie::corner::COUNT; corner++) { 27 | for (int ori = 0; ori < 3; ori++) 28 | corners[encode(cubie::corner::NAMES[corner], ori)] = std::make_pair(corner, ori); 29 | } 30 | for (int edge = 0; edge < cubie::edge::COUNT; edge++) { 31 | for (int ori = 0; ori < 2; ori++) 32 | edges[encode(cubie::edge::NAMES[edge], ori)] = std::make_pair(edge, ori); 33 | } 34 | } 35 | 36 | int to_cubie(const std::string& s, cubie::cube& c) { 37 | for (int i = 0; i < N_FACELETS; i++) { 38 | if (color::FROM_NAME.find(s[i]) == color::FROM_NAME.end()) 39 | return 1; // invalid color 40 | if ((i - 4) % 9 == 0 && color::FROM_NAME.at(s[i]) != i / 9) 41 | return 2; // invalid center facelet (they are always fixed) 42 | } 43 | 44 | for (int corner = 0; corner < cubie::corner::COUNT; corner++) { 45 | char cornlet[3]; 46 | for (int i = 0; i < 3; i++) 47 | cornlet[i] = s[CORNLETS[corner][i]]; 48 | auto tmp = corners.find(encode(std::string(cornlet, 3), 0)); 49 | if (tmp == corners.end()) 50 | return 3; // invalid corner cubie 51 | c.cperm[corner] = tmp->second.first; 52 | c.cori[corner] = tmp->second.second; 53 | } 54 | 55 | for (int edge = 0; edge < cubie::edge::COUNT; edge++) { 56 | char edgelet[2]; 57 | for (int i = 0; i < 2; i++) 58 | edgelet[i] = s[EDGELETS[edge][i]]; 59 | auto tmp = edges.find(encode(std::string(edgelet, 2), 0)); 60 | if (tmp == edges.end()) 61 | return 4; // invalid edge cubie 62 | c.eperm[edge] = tmp->second.first; 63 | c.eori[edge] = tmp->second.second; 64 | } 65 | 66 | return 0; 67 | } 68 | 69 | // Assumes the given cube to be valid 70 | std::string from_cubie(const cubie::cube& c) { 71 | char s[N_FACELETS]; 72 | 73 | for (int color = 0; color < color::COUNT; color++) 74 | s[9 * color + 4] = color::NAMES[color]; 75 | for (int corner = 0; corner < cubie::corner::COUNT; corner++) { 76 | for (int i = 0; i < 3; i++) 77 | // Corner twist is defined clockwise 78 | s[CORNLETS[corner][i]] = cubie::corner::NAMES[c.cperm[corner]][mod(i - c.cori[corner], 3)]; 79 | } 80 | for (int edge = 0; edge < cubie::edge::COUNT; edge++) { 81 | for (int i = 0; i < 2; i++) 82 | s[EDGELETS[edge][i]] = cubie::edge::NAMES[c.eperm[edge]][mod(i - c.eori[edge], 2)]; 83 | } 84 | 85 | return std::string(s, N_FACELETS); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/face.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Since `cubie::cube`s (especially the orientation part) are quite tricky to deal with, we use a more convenient 3 | * representation to interface with the outside world, the face-cube. Having defined an ordering over all 54 stickers 4 | * on the physical cube, a list of the colors for each sticker (in terms of the faces U, R, F, D, L and B not the 5 | * actual cube colors) uniquely specifies any cube-state. 6 | * 7 | * The facelet positions are defined as shown in the folded-up Rubik's cube depicted below. 8 | * 9 | * +--+-----+ 10 | * |U1|U2|U3| 11 | * |--+--+--| 12 | * |U4|U5|U6| 13 | * |--+--+--| 14 | * |U7|U8|U9| 15 | * +--+--+--+--+--+--+--+--+--+--+--+--+ 16 | * |L1|L2|L3|F1|F2|F3|R1|R2|R3|B1|B2|B3| 17 | * |--+--+--|--+--+--|--+--+--|--+--+--| 18 | * |L4|L5|L6|F4|F5|F6|R4|R5|R6|B4|B5|B6| 19 | * |--+--+--|--+--+--|--+--+--|--+--+--| 20 | * |L7|L8|L9|F7|F8|F9|R7|R8|R9|B7|B8|B9| 21 | * +--+--+--+--+--+--+--+--+--+--+--+--+ 22 | * |D1|D2|D3| 23 | * |--+--+--| 24 | * |D4|D5|D6| 25 | * |--+--+--| 26 | * |D7|D8|D9| 27 | * +--+--+--+ 28 | * 29 | * A facelet string simply lists the colors of every facelet position with the faces being in order U, R, F, D, L, B 30 | * and the facelets within a face sorted by their index, i.e. U1U2U3U4U5U6U7U8U9R1R2... where U1, U2, ... are the 31 | * colors of the corresponding facelets. 32 | * 33 | * Note that facelet X5 (i.e. the center sticker of face X) must always be of color X. It also does not matter which 34 | * of the actual cube colors (like red, orange, etc.) is assigned to which face, the assignment must only be consistent 35 | * with respect to the neighborhood relations, i.e. for example if white is considered as the F-face, then yellow must 36 | * be B (as it is always on the opposite side on a physical cube). 37 | */ 38 | 39 | #ifndef __FACE__ 40 | #define __FACE__ 41 | 42 | #include 43 | #include 44 | #include "cubie.h" 45 | 46 | namespace face { 47 | 48 | const int N_FACELETS = 54; // number of facelets (stickers) = 9 * 6 49 | 50 | namespace color { 51 | const int COUNT = 6; // number of colors/faces of a cube 52 | 53 | /* Color/Face ordering */ 54 | const int U = 0; 55 | const int R = 1; 56 | const int F = 2; 57 | const int D = 3; 58 | const int L = 4; 59 | const int B = 5; 60 | 61 | const char NAMES[] = {'U', 'R', 'F', 'D', 'L', 'B'}; 62 | 63 | // Maps color character to corresponding color ID 64 | const std::unordered_map FROM_NAME = { 65 | {'U', U}, {'R', R}, {'F', F}, {'D', D}, {'L', L}, {'B', B} 66 | }; 67 | } 68 | 69 | /* Map corner/edge IDs to corresponding facelet positions */ 70 | const int CORNLETS[][3] = { 71 | {8, 9, 20}, {6, 18, 38}, {0, 36, 47}, {2, 45, 11}, 72 | {29, 26, 15}, {27, 44, 24}, {33, 53, 42}, {35, 17, 51} 73 | }; 74 | const int EDGELETS[][2] = { 75 | {5, 10}, {7, 19}, {3, 37}, {1, 46}, {32, 16}, {28, 25}, 76 | {30, 43}, {34, 52}, {23, 12}, {21, 41}, {50, 39}, {48, 14} 77 | }; 78 | 79 | /* Routines for converting a facelet-string to a cubie-cube and vice-versa */ 80 | int to_cubie(const std::string& s, cubie::cube &c); 81 | std::string from_cubie(const cubie::cube &c); 82 | 83 | // Initializes the face-level; to be called before accessing anything from this file 84 | void init(); 85 | 86 | } 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /src/cubie.cpp: -------------------------------------------------------------------------------- 1 | #include "cubie.h" 2 | 3 | #include 4 | #include 5 | #include "coord.h" 6 | 7 | namespace cubie { 8 | 9 | /* Faster than tricky if-else sequences for handling mirrored states */ 10 | int mul_coris[][6] = { 11 | {0, 1, 2, 3, 4, 5}, 12 | {1, 2, 0, 4, 5, 3}, 13 | {2, 0, 1, 5, 3, 4}, 14 | {3, 5, 4, 0, 2, 1}, 15 | {4, 3, 5, 1, 0, 2}, 16 | {5, 4, 3, 2, 1, 0} 17 | }; 18 | int inv_cori[] = { 19 | 0, 2, 1, 3, 4, 5 20 | }; 21 | 22 | std::random_device device; 23 | std::mt19937 gen(device()); 24 | 25 | void corner::mul(const cubie::cube& c1, const cubie::cube& c2, cubie::cube& into) { 26 | for (int i = 0; i < corner::COUNT; i++) { 27 | into.cperm[i] = c1.cperm[c2.cperm[i]]; 28 | into.cori[i] = mul_coris[c1.cori[c2.cperm[i]]][c2.cori[i]]; 29 | } 30 | } 31 | 32 | void edge::mul(const cubie::cube& c1, const cubie::cube& c2, cubie::cube& into) { 33 | for (int i = 0; i < edge::COUNT; i++) { 34 | into.eperm[i] = c1.eperm[c2.eperm[i]]; 35 | into.eori[i] = (c1.eori[c2.eperm[i]] + c2.eori[i]) & 1; 36 | } 37 | } 38 | 39 | // Permutation partiy = #inversions % 2 40 | bool parity(const int perm[], int len) { 41 | int par = 0; 42 | for (int i = 0; i < len; i++) { 43 | for (int j = 0; j < i; j++) { 44 | if (perm[j] > perm[i]) 45 | par++; 46 | } 47 | } 48 | return par & 1; 49 | } 50 | 51 | void mul(const cubie::cube& c1, const cubie::cube& c2, cubie::cube& into) { 52 | corner::mul(c1, c2, into); 53 | edge::mul(c1, c2, into); 54 | } 55 | 56 | void inv(const cube& c, cube& into) { 57 | for (int corner = 0; corner < corner::COUNT; corner++) 58 | into.cperm[c.cperm[corner]] = corner; // inv[a[i]] = i 59 | for (int edge = 0; edge < edge::COUNT; edge++) 60 | into.eperm[c.eperm[edge]] = edge; 61 | for (int i = 0; i < corner::COUNT; i++) 62 | into.cori[i] = inv_cori[c.cori[into.cperm[i]]]; 63 | for (int i = 0; i < edge::COUNT; i++) 64 | into.eori[i] = c.eori[into.eperm[i]]; 65 | } 66 | 67 | int check(const cube& c) { 68 | bool corners[corner::COUNT] = {}; 69 | int cori_sum = 0; 70 | 71 | for (int i = 0; i < corner::COUNT; i++) { 72 | if (c.cperm[i] < 0 || c.cperm[i] >= corner::COUNT) 73 | return 1; // invalid corner cubie 74 | corners[c.cperm[i]] = true; 75 | if (c.cori[i] < 0 || c.cori[i] >= 3) 76 | return 2; // invalid corner orientation 77 | cori_sum += c.cori[i]; 78 | } 79 | if (cori_sum % 3 != 0) 80 | return 3; // invalid twist parity 81 | for (bool corner : corners) { 82 | if (!corner) 83 | return 4; // missing corner 84 | } 85 | 86 | bool edges[edge::COUNT] = {}; 87 | int eori_sum = 0; 88 | 89 | for (int i = 0; i < edge::COUNT; i++) { 90 | if (c.eperm[i] < 0 || c.eperm[i] >= edge::COUNT) 91 | return 5; // invalid edge cubie 92 | edges[c.eperm[i]] = true; 93 | if (c.eori[i] < 0 || c.eori[i] >= 2) 94 | return 6; // invalid edge orientation 95 | eori_sum += c.eori[i]; 96 | } 97 | if ((eori_sum & 1) != 0) 98 | return 7; // invalid flip parity 99 | for (bool edge : edges) { 100 | if (!edge) 101 | return 8; // missing edge 102 | } 103 | 104 | if (parity(c.cperm, corner::COUNT) != parity(c.eperm, edge::COUNT)) 105 | return 9; // corner and edge permutation parity mismatch 106 | return 0; 107 | } 108 | 109 | void shuffle(cube& c) { 110 | for (int i = 0; i < corner::COUNT; i++) 111 | c.cperm[i] = i; 112 | for (int i = 0; i < edge::COUNT; i++) 113 | c.eperm[i] = i; 114 | 115 | coord::set_corners(c, std::uniform_int_distribution(0, coord::N_CORNERS)(gen)); 116 | std::shuffle(c.eperm, c.eperm + edge::COUNT, gen); // no coordinate for all edges 117 | if (parity(c.cperm, corner::COUNT) != parity(c.eperm, edge::COUNT)) 118 | std::swap(c.cperm[corner::COUNT - 2], c.cperm[corner::COUNT - 1]); // flip parity 119 | 120 | coord::set_twist(c, std::uniform_int_distribution(0, coord::N_TWIST - 1)(gen)); 121 | coord::set_flip(c, std::uniform_int_distribution(0, coord::N_FLIP - 1)(gen)); 122 | } 123 | 124 | // We could maybe make this faster, but it is not performance critical anyways 125 | bool operator==(const cube& c1, const cube& c2) { 126 | return 127 | std::equal(c1.cperm, c1.cperm + corner::COUNT, c2.cperm) && 128 | std::equal(c1.eperm, c1.eperm + edge::COUNT, c2.eperm) && 129 | std::equal(c1.cori, c1.cori + corner::COUNT, c2.cori) && 130 | std::equal(c1.eori, c1.eori + edge::COUNT, c2.eori) 131 | ; 132 | } 133 | 134 | bool operator!=(const cube& c1, const cube& c2) { 135 | return !(c1 == c2); 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /src/sym.cpp: -------------------------------------------------------------------------------- 1 | #include "sym.h" 2 | 3 | namespace sym { 4 | 5 | using namespace cubie::corner; 6 | using namespace cubie::edge; 7 | 8 | const uint32_t EMPTY = ~uint32_t(0); 9 | 10 | cubie::cube cubes[COUNT]; 11 | int inv[COUNT]; 12 | int effect[COUNT][3]; 13 | 14 | int conj_move[move::COUNT][COUNT]; 15 | uint16_t conj_twist[coord::N_TWIST][COUNT_SUB]; 16 | uint16_t conj_udedges2[coord::N_UDEDGES2][COUNT_SUB]; 17 | 18 | uint32_t fslice1_sym[coord::N_FSLICE1]; 19 | uint32_t corners_sym[coord::N_CORNERS]; 20 | uint32_t fslice1_raw[N_FSLICE1]; 21 | uint16_t corners_raw[N_CORNERS]; 22 | uint16_t fslice1_selfs[N_FSLICE1]; 23 | uint16_t corners_selfs[N_CORNERS]; 24 | 25 | void init_base() { 26 | cubie::cube c = cubie::SOLVED_CUBE; 27 | cubie::cube tmp; 28 | 29 | cubie::cube lr2 = { 30 | {UFL, URF, UBR, ULB, DLF, DFR, DRB, DBL}, 31 | {UL, UF, UR, UB, DL, DF, DR, DB, FL, FR, BR, BL}, 32 | {3, 3, 3, 3, 3, 3, 3, 3}, {} // special mirror ori 33 | }; 34 | cubie::cube u4 = { 35 | {UBR, URF, UFL, ULB, DRB, DFR, DLF, DBL}, 36 | {UB, UR, UF, UL, DB, DR, DF, DL, BR, FR, FL, BL}, 37 | {}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1} 38 | }; 39 | cubie::cube f2 = { 40 | {DLF, DFR, DRB, DBL, UFL, URF, UBR, ULB}, 41 | {DL, DF, DR, DB, UL, UF, UR, UB, FL, FR, BR, BL}, 42 | {}, {} 43 | }; 44 | cubie::cube urf3 = { 45 | {URF, DFR, DLF, UFL, UBR, DRB, DBL, ULB}, 46 | {UF, FR, DF, FL, UB, BR, DB, BL, UR, DR, DL, UL}, 47 | {1, 2, 1, 2, 2, 1, 2, 1}, 48 | {1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1} 49 | }; 50 | 51 | // First 4 symmetries are the ones used in F5 mode 52 | for (int i = 0; i < COUNT; i++) { 53 | cubes[i] = c; 54 | 55 | cubie::mul(c, lr2, tmp); 56 | std::swap(tmp, c); 57 | 58 | if (i % 2 == 1) { 59 | cubie::mul(c, f2, tmp); 60 | std::swap(tmp, c); 61 | } 62 | if (i % 4 == 3) { 63 | cubie::mul(c, u4, tmp); 64 | std::swap(tmp, c); 65 | } 66 | if (i % 16 == 15) { 67 | cubie::mul(c, urf3, tmp); 68 | std::swap(tmp, c); 69 | } 70 | } 71 | 72 | /* Maybe not the most efficient, but overall time spent here completely negligible. */ 73 | 74 | for (int i = 0; i < COUNT; i++) { 75 | for (int j = 0; j < COUNT; j++) { 76 | cubie::mul(cubes[i], cubes[j], c); 77 | if (c == cubie::SOLVED_CUBE) { 78 | inv[i] = j; 79 | break; 80 | } 81 | } 82 | } 83 | 84 | for (int m = 0; m < move::COUNT; m++) { 85 | for (int s = 0; s < COUNT; s++) { 86 | cubie::mul(cubes[s], move::cubes[m], tmp); 87 | cubie::mul(tmp, cubes[inv[s]], c); 88 | for (int conj = 0; conj < move::COUNT; conj++) { 89 | if (c == move::cubes[conj]) { 90 | conj_move[m][s] = conj; 91 | break; 92 | } 93 | } 94 | } 95 | } 96 | 97 | /* Figure this out right here instead of defining even more "weird" constants */ 98 | int per_axis = move::COUNT1 / 3; 99 | int per_face = 3; 100 | #ifdef QT 101 | per_face -= 1; 102 | #endif 103 | for (int s = 0; s < COUNT; s++) { 104 | for (int ax = 0; ax < 3; ax++) { 105 | effect[s][ax] = (conj_move[per_axis * ax][inv[s]] / per_axis) << 2; // shift 106 | effect[s][ax] |= (conj_move[per_axis * ax][inv[s]] % per_axis >= per_face) << 1; // flip 107 | effect[s][ax] |= (conj_move[per_axis * ax][inv[s]] % per_face != 0); // inv 108 | } 109 | } 110 | } 111 | 112 | void init_conjcoord( 113 | uint16_t conj_coord[][COUNT_SUB], 114 | int n_coords, 115 | int (*get_coord)(const cubie::cube&), 116 | void (*set_coord)(cubie::cube&, int), 117 | void (*mul)(const cubie::cube&, const cubie::cube&, cubie::cube&) 118 | ) { 119 | cubie::cube c1 = cubie::SOLVED_CUBE; // make sure all multiplications will work 120 | cubie::cube c2; 121 | cubie::cube tmp; 122 | 123 | for (int coord = 0; coord < n_coords; coord++) { 124 | set_coord(c1, coord); 125 | conj_coord[coord][0] = coord; // sym 0 is identity 126 | for (int s = 1; s < COUNT_SUB; s++) { 127 | mul(cubes[s], c1, tmp); 128 | mul(tmp, cubes[inv[s]], c2); 129 | conj_coord[coord][s] = get_coord(c2); 130 | } 131 | } 132 | } 133 | 134 | void init_fslice1() { 135 | std::fill(fslice1_sym, fslice1_sym + coord::N_FSLICE1, EMPTY); 136 | 137 | cubie::cube c1 = cubie::SOLVED_CUBE; 138 | cubie::cube c2; 139 | cubie::cube tmp; 140 | int cls = 0; 141 | 142 | for (int slice1 = 0; slice1 < coord::N_SLICE1; slice1++) { 143 | coord::set_slice1(c1, slice1); // SLICE is slightly more expensive to set 144 | for (int flip = 0; flip < coord::N_FLIP; flip++) { 145 | coord::set_flip(c1, flip); 146 | int fslice1 = coord::fslice1(flip, slice1); 147 | 148 | if (fslice1_sym[fslice1] != EMPTY) 149 | continue; 150 | fslice1_sym[fslice1] = COUNT_SUB * cls; 151 | fslice1_raw[cls] = fslice1; 152 | fslice1_selfs[cls] = 1; // symmetry 0 is identity and always a self-sym 153 | 154 | for (int s = 1; s < COUNT_SUB; s++) { 155 | cubie::edge::mul(cubes[inv[s]], c1, tmp); 156 | cubie::edge::mul(tmp, cubes[s], c2); 157 | int fslice11 = coord::fslice1(coord::get_flip(c2), coord::get_slice1(c2)); 158 | if (fslice1_sym[fslice11] == EMPTY) 159 | fslice1_sym[fslice11] = COUNT_SUB * cls + s; 160 | else if (fslice11 == fslice1) // collect self-symmetries 161 | fslice1_selfs[cls] |= 1 << s; 162 | } 163 | cls++; 164 | } 165 | } 166 | } 167 | 168 | void init_corners() { 169 | std::fill(corners_sym, corners_sym + coord::N_CORNERS, EMPTY); 170 | 171 | cubie::cube c1 = cubie::SOLVED_CUBE; 172 | cubie::cube c2; 173 | cubie::cube tmp; 174 | int cls = 0; 175 | 176 | for (int corners = 0; corners < coord::N_CORNERS; corners++) { 177 | coord::set_corners(c1, corners); 178 | 179 | if (corners_sym[corners] != EMPTY) 180 | continue; 181 | corners_sym[corners] = COUNT_SUB * cls; 182 | corners_raw[cls] = corners; 183 | corners_selfs[cls] = 1; 184 | 185 | for (int s = 1; s < COUNT_SUB; s++) { 186 | cubie::corner::mul(cubes[inv[s]], c1, tmp); 187 | cubie::corner::mul(tmp, cubes[s], c2); 188 | int corners1 = coord::get_corners(c2); 189 | if (corners_sym[corners1] == EMPTY) 190 | corners_sym[corners1] = COUNT_SUB * cls + s; 191 | else if (corners1 == corners) 192 | corners_selfs[cls] |= 1 << s; 193 | } 194 | cls++; 195 | } 196 | } 197 | 198 | void init() { 199 | init_base(); 200 | init_conjcoord(conj_twist, coord::N_TWIST, coord::get_twist, coord::set_twist, cubie::corner::mul); 201 | init_conjcoord(conj_udedges2, coord::N_UDEDGES2, coord::get_udedges2, coord::set_udedges2, cubie::edge::mul); 202 | init_fslice1(); 203 | init_corners(); 204 | } 205 | 206 | } 207 | -------------------------------------------------------------------------------- /src/test.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tests & sanity checks for individual parts of the solver; very useful during development. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "coord.h" 11 | #include "cubie.h" 12 | #include "move.h" 13 | #include "prun.h" 14 | #include "sym.h" 15 | 16 | inline void ok() { std::cout << "Ok." << std::endl; } 17 | inline void error() { std::cout << "Error." << std::endl; } 18 | 19 | void test_cubie() { 20 | std::cout << "Testing cubie level ..." << std::endl; 21 | cubie::cube c = cubie::SOLVED_CUBE; 22 | 23 | cubie::cube tmp1, tmp2; 24 | cubie::inv(c, tmp1); 25 | if (c != tmp1) 26 | error(); 27 | cubie::mul(c, tmp1, tmp2); 28 | if (c != tmp2) 29 | error(); 30 | 31 | cubie::shuffle(c); 32 | cubie::inv(c, tmp1); 33 | cubie::mul(c, tmp1, tmp2); 34 | if (tmp2 != cubie::SOLVED_CUBE) 35 | error(); 36 | 37 | for (int i = 0; i < 100; i++) { 38 | cubie::shuffle(c); 39 | if (cubie::check(c) != 0) 40 | error(); 41 | } 42 | cubie::shuffle(c); 43 | std::swap(c.cperm[0], c.cperm[1]); 44 | if (check(c) == 0) 45 | error(); 46 | 47 | ok(); 48 | } 49 | 50 | void test_getset(int (*get_coord)(const cubie::cube&), void (*set_coord)(cubie::cube&, int), int count) { 51 | cubie::cube c; 52 | for (int i = 0; i < count; i++) { 53 | set_coord(c, i); 54 | if (get_coord(c) != i) 55 | error(); 56 | } 57 | ok(); 58 | } 59 | 60 | void test_movecoord(uint16_t move_coord[][move::COUNT], int n_coord, move::mask moves = move::p1mask | move::p2mask) { 61 | for (int coord = 0; coord < n_coord; coord++) { 62 | for (; moves; moves &= moves - 1) { 63 | int m = ffsll(moves) - 1; 64 | if (move_coord[move_coord[coord][m]][move::inv[m]] != coord) 65 | error(); 66 | if (move_coord[move_coord[coord][move::inv[m]]][m] != coord) 67 | error(); 68 | } 69 | } 70 | ok(); 71 | } 72 | 73 | void test_coord() { 74 | std::cout << "Testing coord level ..." << std::endl; 75 | test_getset(coord::get_flip, coord::set_flip, coord::N_FLIP); 76 | test_getset(coord::get_twist, coord::set_twist, coord::N_TWIST); 77 | test_getset(coord::get_slice, coord::set_slice, coord::N_SLICE); 78 | test_getset(coord::get_uedges, coord::set_uedges, coord::N_UEDGES); 79 | test_getset(coord::get_dedges, coord::set_dedges, coord::N_DEDGES); 80 | test_getset(coord::get_corners, coord::set_corners, coord::N_CORNERS); 81 | 82 | test_getset(coord::get_slice1, coord::set_slice1, coord::N_SLICE1); 83 | test_getset(coord::get_udedges2, coord::set_udedges2, coord::N_UDEDGES2); 84 | 85 | test_movecoord(coord::move_flip, coord::N_FLIP); 86 | test_movecoord(coord::move_twist, coord::N_TWIST); 87 | test_movecoord(coord::move_edges4, coord::N_SLICE); 88 | test_movecoord(coord::move_corners, coord::N_CORNERS); 89 | test_movecoord(coord::move_udedges2, coord::N_UDEDGES2, move::p2mask); 90 | } 91 | 92 | void test_move() { 93 | std::cout << "Testing move level ..." << std::endl; 94 | 95 | cubie::cube c; 96 | for (int m = 0; m < move::COUNT; m++) { 97 | if (move::inv[move::inv[m]] != m) 98 | error(); 99 | cubie::mul(move::cubes[m], move::cubes[move::inv[m]], c); 100 | if (c != cubie::SOLVED_CUBE) 101 | error(); 102 | cubie::mul(move::cubes[move::inv[m]], move::cubes[m], c); 103 | if (c != cubie::SOLVED_CUBE) 104 | error(); 105 | } 106 | ok(); 107 | 108 | std::cout << "Phase 1: "; 109 | for (int m = 0; m < move::COUNT; m++) { 110 | if (move::in(m, move::p1mask)) 111 | std::cout << move::names[m] << " "; 112 | } 113 | std::cout << std::endl; 114 | std::cout << "Phase 2: "; 115 | for (int m = 0; m < move::COUNT; m++) { 116 | if (move::in(m, move::p2mask)) 117 | std::cout << move::names[m] << " "; 118 | } 119 | std::cout << std::endl; 120 | 121 | std::cout << "Forbidden:" << std::endl; 122 | for (int m = 0; m < move::COUNT; m++) { 123 | std::cout << move::names[m] << ": "; 124 | for (int m1 = 0; m1 < move::COUNT; m1++) { 125 | if (!move::in(m1, move::next[m])) 126 | std::cout << move::names[m1] << " "; 127 | } 128 | if (move::qt_skip[m] != 0) { 129 | std::cout << "| "; 130 | for (int m1 = 0; m1 < move::COUNT; m1++) { 131 | if (move::in(m1, move::qt_skip[m])) 132 | std::cout << move::names[m1] << " "; 133 | } 134 | } 135 | std::cout << std::endl; 136 | } 137 | 138 | } 139 | 140 | void test_conj(uint16_t conj_coord[][sym::COUNT_SUB], int n_coord) { 141 | for (int coord = 0; coord < n_coord; coord++) { 142 | for (int s = 0; s < sym::COUNT_SUB; s++) { 143 | if (conj_coord[conj_coord[coord][s]][sym::inv[s]] != coord) 144 | error(); 145 | if (conj_coord[conj_coord[coord][sym::inv[s]]][s] != coord) 146 | error(); 147 | } 148 | } 149 | ok(); 150 | } 151 | 152 | void test_sym() { 153 | std::cout << "Testing sym level ..." << std::endl; 154 | test_conj(sym::conj_twist, coord::N_TWIST); 155 | test_conj(sym::conj_udedges2, coord::N_UDEDGES2); 156 | } 157 | 158 | void test_prun() { 159 | std::cout << "Testing pruning ..." << std::endl; 160 | 161 | srand(0); 162 | int n_moves = std::bitset<64>(move::p1mask).count(); // make sure not to consider B-moves in F5-mode 163 | 164 | for (int i = 0; i < 1000; i++) { 165 | int flip = rand() % coord::N_FLIP; 166 | int slice = rand() % coord::N_SLICE; 167 | int twist = rand() % coord::N_TWIST; 168 | 169 | move::mask next; 170 | move::mask next1; 171 | move::mask tmp; 172 | int togo = prun::get_phase1(flip, slice, twist, 100, tmp); 173 | 174 | int dist = prun::get_phase1(flip, slice, twist, togo, next); 175 | next &= move::p1mask; 176 | 177 | next1 = 0; 178 | for (int m = 0; m < n_moves; m++) { 179 | int flip1 = coord::move_flip[flip][m]; 180 | int slice1 = coord::move_edges4[slice][m]; 181 | int twist1 = coord::move_twist[twist][m]; 182 | if (prun::get_phase1(flip1, slice1, twist1, 100, tmp) < dist) 183 | next1 |= move::bit(m); 184 | } 185 | if (next1 != next) 186 | error(); 187 | 188 | dist = prun::get_phase1(flip, slice, twist, togo + 1, next); 189 | next &= move::p1mask; 190 | 191 | next1 = 0; 192 | for (int m = 0; m < n_moves; m++) { 193 | int flip1 = coord::move_flip[flip][m]; 194 | int slice1 = coord::move_edges4[slice][m]; 195 | int twist1 = coord::move_twist[twist][m]; 196 | if (prun::get_phase1(flip1, slice1, twist1, 100, tmp) <= dist) 197 | next1 |= move::bit(m); 198 | } 199 | if (next1 != next) 200 | error(); 201 | } 202 | 203 | ok(); 204 | } 205 | 206 | bool check(const cubie::cube &c, const std::vector& sol) { 207 | cubie::cube c1; 208 | cubie::cube c2; 209 | 210 | c1 = c; 211 | for (int m : sol) { 212 | cubie::mul(c1, move::cubes[m], c2); 213 | std::swap(c1, c2); 214 | } 215 | 216 | return c1 == cubie::SOLVED_CUBE; 217 | } 218 | 219 | int main() { 220 | auto tick = std::chrono::high_resolution_clock::now(); 221 | move::init(); 222 | coord::init(); 223 | sym::init(); 224 | prun::init(); 225 | std::cout << std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - tick).count() / 1000. << "ms" << std::endl; 226 | 227 | test_cubie(); 228 | test_coord(); 229 | test_move(); 230 | test_sym(); 231 | test_prun(); 232 | 233 | return 0; 234 | } 235 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rob-twophase v2.0 2 | 3 | This is an extremely efficient Rubik's Cube solving algorithm designed particularly for the use by high-speed robots. At its core, it is a highly optimized C++ version of Herbert Kociemba's two-phase algorithm that combines many of the best tricks from the excellent implementations [`RubiksCube-TwophaseSolver`](https://github.com/hkociemba/RubiksCube-TwophaseSolver), [`min2phase`](https://github.com/cs0x7f/min2phase) and [`cube20src`](https://github.com/rokicki/cube20src) with some further improvements of my own. Additionally, it includes several features that (to the best of my knowledge) cannot be found elsewhere at the moment. First and foremost, `rob-twophase` is able to directly consider the mechanics of axial robots (i.e. that they can move opposite faces in parallel or that a 180-degree turn takes about twice as long as a 90-degree one) yielding 15-20% faster to execute solutions on average. Secondly, it supports single-solve multi-threading with an arbitrary number of threads yielding 10+ times speed-ups even on moderate hardware. Finally, it can search for multiple solutions at once allowing post-selection to consider additional execution parameters (like for instance turn transitions). If you are planning to challenge the official Guinness World Record for the fastest robot to solve a Rubik's Cube, `rob-twophase` is most likely the solver you will want to use. 4 | 5 | **New in Version 2.0:** 6 | 7 | * Considerably faster solving performance by less table decomposition and proper elimination of redundant maneuvers in QT-modes. 8 | * Significantly faster initial table generation (also through better coordinates). 9 | * Much better utilization of high thread-counts via the `-s` parameter. 10 | * Return more than one solution with `-n`. 11 | * Automatically compress QT-mode solutions back to HT using the `-c` option. 12 | * Cleaner code by major refactoring and elimination of questionable "optimizations". 13 | 14 | ## Usage 15 | 16 | The easiest way to use `rob-twophase` is to use the small interactive CMD-utility that it compiles to via `make`. Interfacing with this tool via pipes to STDIN/STDOUT should be more than sufficient for most applications (this is also what I do for my own robots). If you want to interface with it directly in C++ best have a look at `src/main.cpp` to see how to use the internal solver engine. The solving mode needs to be selected during compile time via compiler-flags (both for efficiency but also simplicity reasons). Simply add them to `CPPFLAGS` if you are using the provided `makefile`. `-DQT` solves in the quarter-turn metric (only 90-degree moves), `-DAX` in the axial metric (opposite faces can be manipulated at the same time) and `-DF5` uses only 5 faces (never turning the B-face). All three of those flags can be combined arbitrarily. 17 | 18 | The CMD-program provides the following options: 19 | 20 | * `-c` (default OFF): Compress solutions to AXHT. This is especially useful when solving in AXQT as properly merging move sequences like `D (U D)` is not entirely trivial without having all the proper move definitions at the ready. 21 | 22 | * `-l` (default -1): Maximum solution length. The search will stop once a solution of at most this length is found. With `-1` the solver will simply search for the full time-limit and eventually return the best solution found. 23 | 24 | * `-m` (default 10): Time-limit in milliseconds. 25 | 26 | * `-n` (default 1): Number of solutions to return, i.e. it will return the best `-n` solutions found. 27 | 28 | * `-s` (default 1): Number of splits for every IDA-search task. This is an advanced parallelization parameter most relevant for high thread-counts. As a very rough guide, choose it so that `-t / -s` is close to 6 (or close to 4 when using `-DF5`). 29 | 30 | * `-t` (default 1): Number of threads. Best set this as the number of processor threads you have (typically number of cores times two), i.e. use hyper-threading. 31 | 32 | * `-w` (default 0): Number of random warmup solves to perform on start-up to optimally prepare the cache for the robot solves that matter. 33 | 34 | When first starting `rob-twophase`, it will generate fairly big tables which may take several seconds to minutes (see section below). Those are then persisted in files to make further start-ups very quick. After starting it can solve cubes by typing `solve FACECUBE` (see [`src/face.h`](https://github.com/efrantar/rob-twophase/blob/master/src/face.h) for a detailed documentation of Kociemba's face-cube representation), generate scrambles with `scramble` or run benchmarks with `bench`. Note that the program is already designed to be directly used by robots (for example via pipe communication) and thereby of course also does things such as always preloading all threads to ensure maximum solving speed. 35 | 36 | ## Performance 37 | 38 | All benchmarks were run on a stock AMD Ryzen 5 3600 (6 cores, 12 threads) processor (hence `-t 12 -s 2`) combined with standard clocked DDR4 memory and use exactly the same set of 10000 uniformly random cubes (file `bench.cubes`). 39 | 40 | The first table gives for each solving mode (indicated by the compiler flags) the average solution length (number of moves) when running the solver with a timelimit of 10ms (`-m 10`) per cube in the various metrics (half-turn HT, quarter-turn QT, axial half-turn AXHT and axial quarter-turn AXQT). The number in bold is the length in the metric that is being solved in (i.e. the number that is relevant), the other values are just given to illustrate the gains from directly solving in the appropriate metric. 41 | 42 | | `-DQT` | `-DAX` | `-DF5` | HT | QT | AXHT | AXQT | Setup Time | Table Size | 43 | | :----: | :----: | :----: | :-: | :-: | :--: | :--: | :--------: | :--------: | 44 | | - | - | - | **18.56** | *26.46* | *17.00* | *24.60* | 38s | 676MB | 45 | | YES | - | - | *19.84* | **24.22** | *17.97* | *22.21* | 26s | 676MB | 46 | | - | YES | - | *21.83* | *31.05* | **14.71** | *22.95* | 98s | 1.2GB | 47 | | YES | YES | - | *22.76* | *27.03* | *16.40* | **19.91** | 57s | 1.2GB | 48 | | - | - | YES | **20.61** | *29.51* | *18.90* | *27.50* | 125s | 2.7GB | 49 | | YES | - | YES | *22.07* | **26.98** | *19.99* | *24.78* | 85s | 2.7GB | 50 | | - | YES | YES | *23.65* | *33.34* | **16.85** | *25.50* | 287s | 4.9GB | 51 | | YES | YES | YES | *25.08* | *29.94* | *18.38* | **22.49** | 162s | 4.9GB | 52 | 53 | Finally, a speed comparison with Tomas Rokicki's [`cube20src`](https://github.com/rokicki/cube20src) solver which was used to prove that God's number is 20 (and is probably the next fastest available solver). `rob-twophase` learned many great tricks from this extremely optimized implementation. Furthermore, `cube20src` clearly remains the best choice for batch-solving a very large number of cubes. However, it does (at least as of right now) not support robot metrics or single-solve multi-threading. In general, `rob-twophase` is slightly faster in single-threaded mode (apart from shorter QT searches it seems) but dramatically faster when multi-threading. The table below gives the average solving time for different move-bounds and metrics (using again `bench.cubes`). 54 | 55 | | Metric | Max #Moves | `rob-twophase` w. `-t 1` | `rob-twophase` w. `-t 12` | `cube20src` | 56 | | :----: | :--------: | :----------------------: | :-----------------------: | :---------: | 57 | | HT | 20 | 00.48ms | **00.02ms** | 00.55ms | 58 | | HT | 19 | 41.51ms | **04.37ms** | 53.04ms | 59 | | QT | 26 | 11.20ms | **00.96ms** | 10.26ms | 60 | | QT | 25 | 71.43ms | **07.13ms** | 77.94ms | 61 | -------------------------------------------------------------------------------- /src/coord.cpp: -------------------------------------------------------------------------------- 1 | #include "coord.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "cubie.h" 8 | 9 | namespace coord { 10 | 11 | const int N_C12K4 = 495; // binom(12, 4) 12 | const int N_PERM4 = 24; // 4! 13 | 14 | uint16_t move_flip[N_FLIP][move::COUNT]; 15 | uint16_t move_twist[N_TWIST][move::COUNT]; 16 | uint16_t move_edges4[N_SLICE][move::COUNT]; 17 | uint16_t move_corners[N_CORNERS][move::COUNT]; 18 | uint16_t move_udedges2[N_UDEDGES2][move::COUNT]; 19 | 20 | /* Used for en-/decoding pos-perm coords */ 21 | uint8_t enc_perm[1 << (4 * 2)]; // encode 4-elem perm as 8 bits 22 | uint8_t dec_perm[N_PERM4]; 23 | uint16_t enc_comb[1 << 12]; // encode 4-elem comb as 12-bit mask with exactly 4 bits on 24 | uint16_t dec_comb[N_C12K4]; 25 | 26 | int binarize_perm(int perm[]) { 27 | int bin = 0; 28 | for (int i = 3; i >= 0; i--) 29 | bin = (bin << 2) | perm[i]; 30 | return bin; 31 | } 32 | 33 | void init_encdec() { 34 | int perm[] = {0, 1, 2, 3}; 35 | for (int i = 0; i < N_PERM4; i++) { 36 | int bin = binarize_perm(perm); 37 | enc_perm[bin] = i; 38 | dec_perm[i] = bin; 39 | std::next_permutation(perm, perm + 4); 40 | } 41 | 42 | int i = 0; 43 | for (int comb = 0; comb < (1 << cubie::edge::COUNT); comb++) { 44 | if (std::bitset(comb).count() == 4) { 45 | enc_comb[comb] = i; 46 | dec_comb[i] = comb; 47 | i++; 48 | } 49 | } 50 | } 51 | 52 | int get_ori(const int oris[], int len, int n_oris) { 53 | int val = 0; 54 | for (int i = 0; i < len - 1; i++) // last ori can be reconstructed by parity 55 | val = n_oris * val + oris[i]; 56 | return val; 57 | } 58 | 59 | void set_ori(int val, int oris[], int len, int n_oris) { 60 | int par = 0; 61 | for (int i = len - 2; i >= 0; i--) { 62 | oris[i] = val % n_oris; 63 | par += oris[i]; 64 | val /= n_oris; 65 | } 66 | // Ori parity must always be 0 67 | oris[len - 1] = (n_oris - par % n_oris) % n_oris; 68 | } 69 | 70 | // `mask` indicates which 4 edges to compute the coordinate for 71 | int get_combperm(const int cubies[], int len, int mask) { 72 | int min_cubie = ffs(mask) - 1; 73 | 74 | int comb = 0; 75 | int perm = 0; 76 | 77 | for (int i = len - 1; i >= 0; i--) { 78 | if (mask & (1 << cubies[i])) { 79 | comb |= 1 << i; 80 | perm = (perm << 2) | (cubies[i] - min_cubie); 81 | } 82 | } 83 | 84 | return N_PERM4 * enc_comb[comb] + enc_perm[perm]; 85 | } 86 | 87 | void set_combperm(int comb, int perm, int cubies[], int len, int min_cubie) { 88 | comb = dec_comb[comb]; 89 | perm = dec_perm[perm]; 90 | 91 | int cubie = 0; 92 | for (int i = 0; i < len; i++) { 93 | if (cubie == min_cubie) 94 | cubie += 4; 95 | if (comb & (1 << i)) { 96 | cubies[i] = (perm & 0x3) + min_cubie; 97 | perm >>= 2; 98 | } else 99 | cubies[i] = cubie++; 100 | } 101 | } 102 | 103 | /* Faster than using `*_comperm()` twice */ 104 | 105 | int get_perm8(const int cubies[]) { 106 | int comb1 = 0; 107 | int perm1 = 0; 108 | int perm2 = 0; 109 | 110 | for (int i = 7; i >= 0; i--) { 111 | if (cubies[i] < 4) { 112 | comb1 |= 1 << i; 113 | perm1 = (perm1 << 2) | cubies[i]; 114 | } else 115 | perm2 = (perm2 << 2) | (cubies[i] - 4); 116 | } 117 | 118 | comb1 = enc_comb[comb1]; 119 | perm1 = enc_perm[perm1]; 120 | perm2 = enc_perm[perm2]; 121 | return N_PERM4 * (N_PERM4 * comb1 + perm1) + perm2; 122 | } 123 | 124 | void set_perm8(int perm8, int cubies[]) { 125 | int perm2 = dec_perm[perm8 % N_PERM4]; 126 | int comb1 = dec_comb[(perm8 / N_PERM4) / N_PERM4]; 127 | int perm1 = dec_perm[(perm8 / N_PERM4) % N_PERM4]; 128 | 129 | for (int i = 0; i < 8; i++) { 130 | if (comb1 & (1 << i)) { 131 | cubies[i] = perm1 & 0x3; 132 | perm1 >>= 2; 133 | } else { 134 | cubies[i] = (perm2 & 0x3) + 4; 135 | perm2 >>= 2; 136 | } 137 | } 138 | } 139 | 140 | int get_twist(const cubie::cube& c) { 141 | return get_ori(c.cori, cubie::corner::COUNT, 3); 142 | } 143 | 144 | void set_twist(cubie::cube& c, int twist) { 145 | set_ori(twist, c.cori, cubie::corner::COUNT, 3); 146 | } 147 | 148 | int get_flip(const cubie::cube& c) { 149 | return get_ori(c.eori, cubie::edge::COUNT, 2); 150 | } 151 | 152 | void set_flip(cubie::cube& c, int flip) { 153 | set_ori(flip, c.eori, cubie::edge::COUNT, 2); 154 | } 155 | 156 | int get_slice(const cubie::cube& c) { 157 | return get_combperm(c.eperm, cubie::edge::COUNT, 0xf00); 158 | } 159 | 160 | void set_slice(cubie::cube& c, int slice) { 161 | set_combperm(slice / N_PERM4, slice % N_PERM4, c.eperm, cubie::edge::COUNT, cubie::edge::FR); 162 | } 163 | 164 | int get_uedges(const cubie::cube& c) { 165 | return get_combperm(c.eperm, cubie::edge::COUNT, 0x00f); 166 | } 167 | 168 | void set_uedges(cubie::cube& c, int uedges) { 169 | set_combperm(uedges / N_PERM4, uedges % N_PERM4, c.eperm, cubie::edge::COUNT, cubie::edge::UR); 170 | } 171 | 172 | int get_dedges(const cubie::cube& c) { 173 | return get_combperm(c.eperm, cubie::edge::COUNT, 0x0f0); 174 | } 175 | 176 | void set_dedges(cubie::cube& c, int dedges) { 177 | set_combperm(dedges / N_PERM4, dedges % N_PERM4, c.eperm, cubie::edge::COUNT, cubie::edge::DR); 178 | } 179 | 180 | int get_corners(const cubie::cube& c) { 181 | return get_perm8(c.cperm); 182 | } 183 | 184 | void set_corners(cubie::cube& c, int corners) { 185 | set_perm8(corners, c.cperm); 186 | } 187 | 188 | /* Dedicated methods again more efficient than `*_posperm()` */ 189 | 190 | int get_slice1(const cubie::cube& c) { 191 | int slice1 = 0; 192 | for (int i = cubie::edge::COUNT - 1; i >= 0; i--) { 193 | if (c.eperm[i] >= cubie::edge::FR) 194 | slice1 |= 1 << i; 195 | } 196 | return enc_comb[slice1]; 197 | } 198 | 199 | void set_slice1(cubie::cube& c, int slice1) { 200 | slice1 = dec_comb[slice1]; 201 | int j = cubie::edge::FR; 202 | int cubie = 0; 203 | for (int i = 0; i < cubie::edge::COUNT; i++) 204 | c.eperm[i] = (slice1 & (1 << i)) ? j++ : cubie++; 205 | } 206 | 207 | int get_udedges2(const cubie::cube& c) { 208 | return get_perm8(c.eperm); 209 | } 210 | 211 | void set_udedges2(cubie::cube& c, int udedges2) { 212 | set_perm8(udedges2, c.eperm); 213 | } 214 | 215 | // Computing only exactly the moves that are needed and storing them tightly would only make things more complicated 216 | // during solving (in exchange for completely negligible setup/memory-gains) 217 | void init_move( 218 | uint16_t move_coord[][move::COUNT], 219 | int n_coord, 220 | int (*get_coord)(const cubie::cube&), 221 | void (*set_coord)(cubie::cube&, int), 222 | void (*mul)(const cubie::cube&, const cubie::cube&, cubie::cube&), 223 | bool phase2 = false 224 | ) { 225 | cubie::cube c1 = cubie::SOLVED_CUBE; // coords only affect perm or ori -> one would be uninitialized 226 | cubie::cube c2; 227 | 228 | for (int coord = 0; coord < n_coord; coord++) { 229 | set_coord(c1, coord); 230 | 231 | if (phase2) { // UDEDGES2 is only defined for phase 2 moves 232 | for (move::mask moves = move::p2mask; moves; moves &= moves - 1) { 233 | int m = ffsll(moves) - 1; 234 | mul(c1, move::cubes[m], c2); 235 | move_coord[coord][m] = get_coord(c2); 236 | } 237 | } else { 238 | for (int m = 0; m < move::COUNT; m++) { 239 | mul(c1, move::cubes[m], c2); 240 | move_coord[coord][m] = get_coord(c2); 241 | } 242 | } 243 | } 244 | } 245 | 246 | void init() { 247 | init_encdec(); 248 | 249 | init_move(move_flip, N_FLIP, get_flip, set_flip, cubie::edge::mul); 250 | init_move(move_twist, N_TWIST, get_twist, set_twist, cubie::corner::mul); 251 | init_move(move_edges4, N_SLICE, get_slice, set_slice, cubie::edge::mul); 252 | init_move(move_corners, N_CORNERS, get_corners, set_corners, cubie::corner::mul); 253 | init_move(move_udedges2, N_UDEDGES2, get_udedges2, set_udedges2, cubie::edge::mul, true); 254 | } 255 | 256 | } 257 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "cubie.h" 9 | #include "coord.h" 10 | #include "face.h" 11 | #include "move.h" 12 | #include "prun.h" 13 | #include "solve.h" 14 | #include "sym.h" 15 | 16 | const std::string BENCH_FILE = "bench.cubes"; 17 | 18 | void usage() { 19 | std::cout << "Usage: ./twophase " 20 | << "[-c] [-l MAX_LEN = 1] [-m MILLIS = 10] [-n N_SOLS = 1] [-s N_SPLITS = 1] [-t N_THREADS = 1] [-w N_WARMUPS = 0]" 21 | << std::endl; 22 | exit(1); 23 | } 24 | 25 | void init() { 26 | auto tick = std::chrono::high_resolution_clock::now(); 27 | std::cout << "Loading tables ..." << std::endl; 28 | 29 | face::init(); 30 | move::init(); 31 | coord::init(); 32 | sym::init(); 33 | if (prun::init(true)) { 34 | std::cout << "Error." << std::endl; 35 | exit(1); 36 | } 37 | 38 | std::cout << "Done. " << std::chrono::duration_cast( 39 | std::chrono::high_resolution_clock::now() - tick 40 | ).count() / 1000. << "s" << std::endl << std::endl; 41 | } 42 | 43 | void warmup(solve::Engine& solver, int count) { 44 | if (count == 0) 45 | return; 46 | 47 | std::cout << "Warming up ..." << std::endl; 48 | cubie::cube c; 49 | std::vector> sols; 50 | for (int i = 0; i < count; i++) { 51 | cubie::shuffle(c); 52 | solver.prepare(); 53 | solver.solve(c, sols); 54 | solver.finish(); 55 | std::cout << i << std::endl; 56 | } 57 | std::cout << "Done." << std::endl << std::endl; 58 | } 59 | 60 | bool check(const cubie::cube &c, const std::vector& sol) { 61 | cubie::cube c1; 62 | cubie::cube c2; 63 | 64 | c1 = c; 65 | for (int m : sol) { 66 | cubie::mul(c1, move::cubes[m], c2); 67 | std::swap(c1, c2); 68 | } 69 | 70 | return c1 == cubie::SOLVED_CUBE; 71 | } 72 | 73 | double mean(const std::vector>& sols, int (*len)(const std::vector&)) { 74 | double total = 0; 75 | for (auto& sol : sols) 76 | total += len(sol); 77 | return total / sols.size(); 78 | } 79 | 80 | int main(int argc, char *argv[]) { 81 | int n_threads = 1; 82 | int tlim = 10; 83 | int n_sols = 1; 84 | int max_len = -1; 85 | int n_splits = 1; 86 | bool compress = false; 87 | int n_warmups = 0; 88 | 89 | try { 90 | int opt; 91 | while ((opt = getopt(argc, argv, "cl:m:n:s:t:w:")) != -1) { 92 | switch (opt) { 93 | case 'c': 94 | compress = true; 95 | break; 96 | case 'l': 97 | max_len = std::stoi(optarg); 98 | break; 99 | case 'm': 100 | tlim = std::stoi(optarg); 101 | break; 102 | case 'n': 103 | if ((n_sols = std::stoi(optarg)) <= 0) { 104 | std::cout << "Error: Number of solutions (-n) must be >= 1." << std::endl; 105 | return 1; 106 | } 107 | break; 108 | case 's': 109 | if ((n_splits = std::stoi(optarg)) <= 0) { 110 | std::cout << "Error: Number of job splits (-s) must be >= 1." << std::endl; 111 | return 1; 112 | } 113 | break; 114 | case 't': 115 | if ((n_threads = std::stoi(optarg)) <= 0) { 116 | std::cout << "Error: Number of solver threads (-t) must be >= 1." << std::endl; 117 | return 1; 118 | } 119 | break; 120 | case 'w': 121 | if ((n_warmups = std::stoi(optarg)) <= 0) { 122 | std::cout << "Error: Number of warmup solves (-w) must be >= 0." << std::endl; 123 | return 1; 124 | } 125 | break; 126 | default: 127 | usage(); 128 | } 129 | } 130 | } catch (...) { // catch any integer conversion errors 131 | usage(); 132 | } 133 | 134 | std::cout << "This is rob-twophase v2.0; copyright Elias Frantar 2020." << std::endl << std::endl; 135 | init(); 136 | solve::Engine solver(n_threads, tlim, n_sols, max_len, n_splits); 137 | warmup(solver, n_warmups); 138 | 139 | std::cout << "Enter >>solve FACECUBE<< to solve, >>scramble<< to scramble or >>bench<< to benchmark." << std::endl << std::endl; 140 | 141 | std::string mode; 142 | while (std::cin) { 143 | solver.prepare(); 144 | std::cout << "Ready!" << std::endl; 145 | 146 | std::cin >> mode; 147 | if (mode == "bench") { 148 | try { 149 | std::ifstream fstream; 150 | fstream.open(BENCH_FILE); 151 | 152 | std::string s; 153 | std::vector cubes; 154 | while (std::getline(fstream, s)) { 155 | cubie::cube c; 156 | face::to_cubie(s, c); 157 | cubes.push_back(c); 158 | } 159 | if (cubes.size() == 0) { 160 | std::cout << "Error." << std::endl; 161 | continue; 162 | } 163 | 164 | std::vector> sols; 165 | std::vector times(cubes.size()); 166 | int failed = 0; 167 | 168 | std::cout << "Benchmarking ..." << std::endl; 169 | for (int i = 0; i < cubes.size(); i++) { 170 | std::cout << i << std::endl; 171 | 172 | solver.prepare(); 173 | auto tick = std::chrono::high_resolution_clock::now(); 174 | std::vector> tmp; 175 | solver.solve(cubes[i], tmp); 176 | times[i] = std::chrono::duration_cast( 177 | std::chrono::high_resolution_clock::now() - tick 178 | ).count() / 1000.; 179 | solver.finish(); 180 | 181 | if (tmp.size() == 0 || !check(cubes[i], tmp[0])) { 182 | std::cout << face::from_cubie(cubes[i]) << std::endl; 183 | failed++; 184 | } 185 | else 186 | sols.push_back(tmp[0]); 187 | } 188 | 189 | std::cout << std::endl; 190 | std::cout << "Failed: " << failed << std::endl; 191 | std::cout << "Avg. Time: " << std::accumulate(times.begin(), times.end(), 0.) / times.size() << " ms" << std::endl; 192 | std::cout << "Avg. Moves: " 193 | << mean(sols, move::len_ht) << " (HT), " 194 | << mean(sols, move::len_qt) << " (QT), " 195 | << mean(sols, move::len_axht) << " (AXHT), " 196 | << mean(sols, move::len_axqt) << " (AXQT)" 197 | << std::endl; 198 | 199 | int freq[100]; 200 | int min = 100; 201 | int max = 0; 202 | for (auto& sol : sols) { 203 | freq[sol.size()]++; 204 | min = std::min(min, (int) sol.size()); // errors without casting ... 205 | max = std::max(max, (int) sol.size()); 206 | } 207 | 208 | std::cout << std::endl; 209 | std::cout << "Distribution:" << std::endl; 210 | for (int len = min; len <= max; len++) 211 | std::cout << len << ": " << freq[len] << std::endl; 212 | std::cout << std::endl; 213 | } catch (...) { // any file reading errors 214 | std::cout << "Error." << std::endl; 215 | continue; 216 | } 217 | } else { 218 | cubie::cube c; 219 | std::vector> sols; 220 | 221 | if (mode == "solve") { 222 | std::string fcube; 223 | std::cin >> fcube; 224 | int err = face::to_cubie(fcube, c); 225 | if (err != 0) { 226 | std::cout << "Face-error " << err << "." << std::endl; 227 | continue; 228 | } 229 | err = cubie::check(c); 230 | if (err != 0) { 231 | std::cout << "Cubie-error " << err << "." << std::endl; 232 | continue; 233 | } 234 | } else if (mode == "scramble") { 235 | cubie::shuffle(c); 236 | cubie::cube tmp; 237 | cubie::inv(c, tmp); 238 | std::cout << face::from_cubie(tmp) << std::endl; // the solution we find will actually be a scramble for the inverse 239 | } else { 240 | std::cout << "Error." << std::endl; 241 | continue; 242 | } 243 | 244 | auto tick = std::chrono::high_resolution_clock::now(); 245 | solver.solve(c, sols); 246 | std::cout << std::chrono::duration_cast( 247 | std::chrono::high_resolution_clock::now() - tick 248 | ).count() / 1000. << "ms" << std::endl; 249 | 250 | for (std::vector& sol : sols) { 251 | int len = sol.size(); // always print uncompressed length 252 | if (compress) 253 | std::cout << move::compress(sol) << " "; 254 | else { 255 | for (int m : sol) 256 | std::cout << move::names[m] << " "; 257 | } 258 | std::cout << "(" << len << ")" << std::endl; 259 | } 260 | } 261 | } 262 | solver.finish(); // clean exit 263 | 264 | return 0; 265 | } 266 | -------------------------------------------------------------------------------- /src/move.cpp: -------------------------------------------------------------------------------- 1 | #include "move.h" 2 | 3 | namespace move { 4 | 5 | using namespace cubie::corner; 6 | using namespace cubie::edge; 7 | 8 | /* Select moves and order according to used metric */ 9 | #ifdef QT 10 | #ifdef AX 11 | const int map[] = { 12 | // U, U2, U', D, D2, D' 13 | 0, -1, 1, 2, -1, 3, 14 | // (U D), (U D2), (U D'), (U2 D), (U2 D2), (U2 D'), (U' D), (U' D2), (U' D') 15 | 4, -1, 5, -1, -1, -1, 6, -1, 7, 16 | // R, R2, R', L, L2, L' 17 | 8, 24, 9, 10, 25, 11, 18 | // (R L), (R L2), (R L'), (R2 L), (R2 L2), (R2 L'), (R' L), (R' L2), (R' L') 19 | 12, -1, 13, -1, 26, -1, 14, -1, 15, 20 | // F, F2, F', B, B2, B' 21 | 16, 27, 17, 18, 28, 19, 22 | // (F B), (F B2), (F B'), (F2 B), (F2 B2), (F2 B'), (F' B), (F' B2), (F' B') 23 | 20, -1, 21, -1, 29, -1, 22, -1, 23 24 | }; 25 | #else 26 | const int map[] = { 27 | 0, -1, 1, 2, -1, 3, 28 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 29 | 4, 12, 5, 6, 13, 7, 30 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 31 | 8, 14, 9, 10, 15, 11, 32 | -1, -1, -1, -1, -1, -1, -1, -1, -1 33 | }; 34 | #endif 35 | #else 36 | #ifdef AX 37 | const int map[] = { 38 | 0, 1, 2, 3, 4, 5, 39 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 40 | 15, 16, 17, 18, 19, 20, 41 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 42 | 30, 31, 32, 33, 34, 35, 43 | 36, 37, 38, 39, 40, 41, 42, 43, 44 44 | }; 45 | #else 46 | const int map[] = { 47 | 0, 1, 2, 3, 4, 5, 48 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 49 | 6, 7, 8, 9, 10, 11, 50 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 51 | 12, 13, 14, 15, 16, 17, 52 | -1, -1, -1, -1, -1, -1, -1, -1, -1 53 | }; 54 | #endif 55 | #endif 56 | 57 | std::string names[COUNT]; 58 | cubie::cube cubes[COUNT]; 59 | int inv[COUNT]; 60 | 61 | mask next[COUNT]; 62 | mask next_p1p2[COUNT]; 63 | mask qt_skip[COUNT]; 64 | 65 | mask p1mask = bit(45) - 1; 66 | mask p2mask = 0x10482097fff; // 000010000 010010 000010000 010010 111111111 111111; 67 | 68 | // For full set of 45 moves no matter the solving mode 69 | std::string names1[45]; 70 | int merge[45][45]; 71 | int unmap[COUNT]; 72 | 73 | // Translate bitmask from full moveset to configured one 74 | mask reindex(mask mm) { 75 | mask mm1 = 0; 76 | for (int m = 0; m < 45; m++) { 77 | if (map[m] != -1 && in(m, mm)) // drop unmapped moves 78 | mm1 |= bit(map[m]); 79 | } 80 | return mm1; 81 | } 82 | 83 | // Build full moveset first, then remap to configured one 84 | void init() { 85 | for (int m = 0; m < 45; m++) { 86 | if (map[m] != -1) 87 | unmap[map[m]] = m; 88 | } 89 | 90 | cubie::cube cubes1[45]; 91 | int inv1[45]; 92 | // Not initializing the following arrays apparently causes problems on MacOS 93 | mask next1[45] = {0}; 94 | mask qt_skip1[45] = {0}; 95 | 96 | std::string fnames[] = {"U", "D", "R", "L", "F", "B"}; 97 | std::string pnames[] = {"", "2", "'"}; 98 | cubie::cube fcubes[] = { 99 | { // U 100 | {UBR, URF, UFL, ULB, DFR, DLF, DBL, DRB}, 101 | {UB, UR, UF, UL, DR, DF, DL, DB, FR, FL, BL, BR}, 102 | {}, {} 103 | }, 104 | { // D 105 | {URF, UFL, ULB, UBR, DLF, DBL, DRB, DFR}, 106 | {UR, UF, UL, UB, DF, DL, DB, DR, FR, FL, BL, BR}, 107 | {}, {} 108 | }, 109 | { // R 110 | {DFR, UFL, ULB, URF, DRB, DLF, DBL, UBR}, 111 | {FR, UF, UL, UB, BR, DF, DL, DB, DR, FL, BL, UR}, 112 | {2, 0, 0, 1, 1, 0, 0, 2}, {} 113 | }, 114 | { // L 115 | {URF, ULB, DBL, UBR, DFR, UFL, DLF, DRB}, 116 | {UR, UF, BL, UB, DR, DF, FL, DB, FR, UL, DL, BR}, 117 | {0, 1, 2, 0, 0, 2, 1, 0}, {} 118 | }, 119 | { // F 120 | {UFL, DLF, ULB, UBR, URF, DFR, DBL, DRB}, 121 | {UR, FL, UL, UB, DR, FR, DL, DB, UF, DF, BL, BR}, 122 | {1, 2, 0, 0, 2, 1, 0, 0}, 123 | {0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0} 124 | }, 125 | { // B 126 | {URF, UFL, UBR, DRB, DFR, DLF, ULB, DBL}, 127 | {UR, UF, UL, BR, DR, DF, DL, BL, FR, FL, UB, DB}, 128 | {0, 0, 1, 2, 0, 0, 2, 1}, 129 | {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1} 130 | } 131 | }; 132 | 133 | for (int ax = 0; ax < 3; ax++) { 134 | int i1 = 15 * ax; // index to start first face moves 135 | int i2 = 15 * ax + 3; // index to start of second face moves 136 | int i3 = 15 * ax + 6; // index to start of axial moves 137 | 138 | int f1 = 2 * ax; // first face 139 | int f2 = 2 * ax + 1; // second face 140 | 141 | for (int cnt = 0; cnt < 3; cnt++) { 142 | int m = i1 + cnt; 143 | names1[m] = fnames[f1] + pnames[cnt]; 144 | if (cnt == 0) 145 | cubes1[m] = fcubes[f1]; 146 | else 147 | cubie::mul(cubes1[m - 1], fcubes[f1], cubes1[m]); 148 | inv1[m] = i1 + (2 - cnt); 149 | next1[m] |= mask(0x7) << i1; // block any moves on same face 150 | #ifdef AX 151 | next1[m] |= mask(0x38) << i1; // block also moves on opposite face 152 | #endif 153 | #ifdef QT 154 | if (cnt == 0) { // block all axial moves but the ones with M 155 | next1[m] |= mask(0x1ff ^ (0x7 << 3 * cnt)) << i3; 156 | continue; 157 | } 158 | #endif 159 | next1[m] |= mask(0x1ff) << i3; // block all axial moves 160 | } 161 | for (int cnt = 0; cnt < 3; cnt++) { 162 | int m = i2 + cnt; 163 | names1[m] = fnames[f2] + pnames[cnt]; 164 | if (cnt == 0) 165 | cubes1[m] = fcubes[f2]; 166 | else 167 | cubie::mul(cubes1[m - 1], fcubes[f2], cubes1[m]); 168 | inv1[m] = i2 + (2 - cnt); 169 | next1[m] |= mask(0x3f) << i1; // block all simple moves on both faces 170 | #ifdef QT 171 | if (cnt == 0) { // block all axial moves but the ones with M 172 | next1[m] |= mask(0x1ff ^ (0x49 << cnt)) << i3; // 0x49 == 0b1001001 173 | continue; 174 | } 175 | #endif 176 | next1[m] |= mask(0x1ff) << i3; 177 | } 178 | for (int cnt1 = 0; cnt1 < 3; cnt1++) { 179 | for (int cnt2 = 0; cnt2 < 3; cnt2++) { 180 | int m = i3 + 3 * cnt1 + cnt2; 181 | names1[m] = "(" + names1[i1 + cnt1] + " " + names1[i2 + cnt2] + ")"; 182 | cubie::mul(cubes1[i1 + cnt1], cubes1[i2 + cnt2], cubes1[m]); 183 | inv1[m] = i3 + 3 * (2 - cnt1) + (2 - cnt2); 184 | next1[m] |= mask(0x7fff) << 15 * ax; // block all simple and axial moves 185 | } 186 | } 187 | 188 | qt_skip1[i1] |= bit(i1); 189 | qt_skip1[i1] |= bit(i3); 190 | qt_skip1[i2] |= bit(i2); 191 | qt_skip1[i2] |= bit(i3); 192 | qt_skip1[i3] |= bit(i3); 193 | } 194 | // Half-slice moves commute 195 | next1[25] |= bit(10); 196 | next1[40] |= bit(10) | bit(25); 197 | #ifdef QT 198 | // Allow repetitions of purely clockwise moves 199 | for (int m : {0, 3, 6, 15, 18, 21, 30, 33, 36}) 200 | next1[m] ^= bit(m); 201 | #endif 202 | // Was built by blocking moves, but should actually indicate permitted ones 203 | for (int m = 0; m < 45; m++) 204 | next1[m] = ~next1[m]; 205 | 206 | for (int m = 0; m < 45; m++) { 207 | if (map[m] == -1) 208 | continue; 209 | int i = map[m]; 210 | 211 | names[i] = names1[m]; 212 | cubes[i] = cubes1[m]; 213 | inv[i] = map[inv1[m]]; 214 | next[i] = reindex(next1[m]); 215 | qt_skip[i] = reindex(qt_skip1[m]); 216 | } 217 | 218 | #ifdef QT 219 | // Unmapped moves are skipped automatically during reindexing 220 | p1mask &= ~0x10482090000; // 000010000 010010 000010000 010010 000000000 000000 221 | #endif 222 | #ifdef F5 223 | mask tmp = ~(mask(0xfff) << 33); 224 | p1mask &= tmp; 225 | p2mask &= tmp; 226 | #endif 227 | p1mask = reindex(p1mask); 228 | p2mask = reindex(p2mask); 229 | 230 | for (int m = 0; m < COUNT; m++) { 231 | if (p2mask & move::bit(m)) 232 | next_p1p2[m] = next[m]; // we can do normal blocking for phase 2 moves 233 | else { 234 | #ifndef AX 235 | #ifdef QT 236 | next_p1p2[m] = ~(mask(0x3) << 2 * (m / 2)); 237 | if (m / 2 > 1) 238 | next_p1p2[m] &= ~(move::bit(COUNT1 + (m / 2) - 2)); 239 | #else 240 | next_p1p2[m] = ~(mask(0x7) << 3 * (m / 3)); 241 | #endif 242 | #else 243 | next_p1p2[m] = next[m]; // no commutativity problems in axial mode 244 | #endif 245 | } 246 | } 247 | 248 | cubie::cube c; 249 | for (int m1 = 0; m1 < 45; m1++) { 250 | for (int m2 = 0; m2 < 45; m2++) { 251 | merge[m1][m2] = -1; 252 | cubie::mul(cubes1[m1], cubes1[m2], c); 253 | for (int i = 0; i < 45; i++) { 254 | if (c == cubes1[i]) { 255 | merge[m1][m2] = i; 256 | break; 257 | } 258 | } 259 | } 260 | } 261 | } 262 | 263 | void compress1(const std::vector& mseq, std::vector& into) { 264 | into.clear(); 265 | for (int m : mseq) { 266 | m = unmap[m]; 267 | if (into.size() == 0 || merge[into.back()][m] == -1) 268 | into.push_back(m); 269 | else { 270 | int tmp = into.back(); 271 | into.pop_back(); 272 | into.push_back(merge[tmp][m]); 273 | } 274 | } 275 | } 276 | 277 | std::string compress(const std::vector& mseq) { 278 | std::vector comp; 279 | compress1(mseq, comp); 280 | 281 | // Faster string building probably not worth it in a function like this 282 | std::string s; 283 | for (int i = 0; i < comp.size(); i++) { 284 | s += names1[comp[i]]; 285 | if (i != comp.size() - 1) 286 | s += " "; 287 | } 288 | return s; 289 | } 290 | 291 | int len(const std::vector& mseq, int cost[]) { 292 | std::vector comp; 293 | compress1(mseq, comp); 294 | 295 | int res = 0; 296 | for (int m : comp) 297 | res += cost[m]; 298 | return res; 299 | } 300 | 301 | int len_ht(const std::vector& mseq) { 302 | int cost[] = { 303 | 1, 1, 1, 1, 1, 1, 304 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 305 | 1, 1, 1, 1, 1, 1, 306 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 307 | 1, 1, 1, 1, 1, 1, 308 | 2, 2, 2, 2, 2, 2, 2, 2, 2 309 | }; 310 | return len(mseq, cost); 311 | } 312 | 313 | int len_axht(const std::vector& mseq) { 314 | int cost[] = { 315 | 1, 1, 1, 1, 1, 1, 316 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 317 | 1, 1, 1, 1, 1, 1, 318 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 319 | 1, 1, 1, 1, 1, 1, 320 | 1, 1, 1, 1, 1, 1, 1, 1, 1 321 | }; 322 | return len(mseq, cost); 323 | } 324 | 325 | int len_qt(const std::vector& mseq) { 326 | int cost[] = { 327 | 1, 2, 1, 1, 2, 1, 328 | 2, 3, 2, 3, 4, 3, 2, 3, 2, 329 | 1, 2, 1, 1, 2, 1, 330 | 2, 3, 2, 3, 4, 3, 2, 3, 2, 331 | 1, 2, 1, 1, 2, 1, 332 | 2, 3, 2, 3, 4, 3, 2, 3, 2 333 | }; 334 | return len(mseq, cost); 335 | } 336 | 337 | int len_axqt(const std::vector& mseq) { 338 | int cost[] = { 339 | 1, 2, 1, 1, 2, 1, 340 | 1, 2, 1, 2, 2, 2, 1, 2, 1, 341 | 1, 2, 1, 1, 2, 1, 342 | 1, 2, 1, 2, 2, 2, 1, 2, 1, 343 | 1, 2, 1, 1, 2, 1, 344 | 1, 2, 1, 2, 2, 2, 1, 2, 1 345 | }; 346 | return len(mseq, cost); 347 | } 348 | 349 | } 350 | -------------------------------------------------------------------------------- /src/solve.cpp: -------------------------------------------------------------------------------- 1 | #include "solve.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include "prun.h" 7 | #include "sym.h" 8 | 9 | namespace solve { 10 | 11 | class Search { 12 | 13 | int dir; // ID of search direction 14 | const coordc& cube; // starting position 15 | int p1depth; // phase 1 search depth 16 | move::mask d0moves; // mask for initial moves to consider 17 | bool& done; // when to terminate the search 18 | int& lenlim; // only find strictly shorter solutions 19 | Engine& solver; // report solutions to 20 | 21 | /* Keep track of reconstructed edges that remain valid in the current search path */ 22 | int uedges[50]; 23 | int dedges[50]; 24 | int edges_depth; 25 | 26 | int moves[50]; // current (partial) solution 27 | 28 | private: 29 | void phase1( 30 | int depth, int togo, int flip, int slice, int twist, int corners, move::mask next, move::mask qt_skip 31 | ); // phase 1 search; iterates through all solution with exactly `togo` moves 32 | bool phase2( 33 | int depth, int togo, int slice, int udedges2, int corners, move::mask next, move::mask qt_skip 34 | ); // phase 2 search; returns once any solution is found 35 | 36 | public: 37 | Search( 38 | int dir, 39 | const coordc& cube, 40 | int p1depth, move::mask d0moves, 41 | bool& done, int& lenlim, Engine& solver 42 | ) : dir(dir), cube(cube), p1depth(p1depth), d0moves(d0moves), done(done), lenlim(lenlim), solver(solver) {}; 43 | void run(); // perform the search 44 | 45 | }; 46 | 47 | void Search::run() { 48 | uedges[0] = cube.uedges; 49 | dedges[0] = cube.dedges; 50 | edges_depth = 0; 51 | 52 | move::mask next; 53 | prun::get_phase1(cube.flip, cube.slice, cube.twist, p1depth, next); 54 | next &= move::p1mask & d0moves; // block B-moves in F5 mode here and select current search split 55 | phase1(0, p1depth, cube.flip, cube.slice, cube.twist, cube.corners, next, 0); 56 | } 57 | 58 | void Search::phase1( 59 | int depth, int togo, int flip, int slice, int twist, int corners, move::mask next, move::mask qt_skip 60 | ) { 61 | if (done) 62 | return; 63 | if (togo == 0) { 64 | int tmp = prun::get_precheck(corners, slice); 65 | if (tmp >= lenlim - depth) // phase 2 precheck, only reconstruct edges if successful 66 | return; 67 | 68 | for (int i = edges_depth + 1; i <= depth; i++) { 69 | uedges[i] = coord::move_edges4[uedges[i - 1]][moves[i - 1]]; 70 | dedges[i] = coord::move_edges4[dedges[i - 1]][moves[i - 1]]; 71 | } 72 | edges_depth = depth - 1; 73 | int udedges2 = coord::merge_udedges2(uedges[depth], dedges[depth]); 74 | 75 | int delta = 1; 76 | #ifndef AX 77 | #ifdef QT 78 | delta++; // in vanilla QT mode the perm-parity indicates whether solution length is odd or even 79 | #endif 80 | #endif 81 | for (int togo1 = std::max(prun::get_phase2(corners, udedges2), tmp); togo1 < lenlim - depth; togo1 += delta) { 82 | if (phase2(depth, togo1, slice, udedges2, corners, move::p2mask & move::next_p1p2[moves[depth - 1]], qt_skip)) 83 | return; // once we have found a phase 2 solution, there cannot be any shorter ones -> quit 84 | } 85 | return; 86 | } 87 | 88 | depth++; 89 | togo--; 90 | while (next) { 91 | int m = ffsll(next) - 1; // get rightmost move index (`ffsll()` uses 1-based indexing) 92 | next &= next - 1; 93 | 94 | int flip1 = coord::move_flip[flip][m]; 95 | int slice1 = coord::move_edges4[slice][m]; 96 | int twist1 = coord::move_twist[twist][m]; 97 | move::mask next1; 98 | int dist1 = prun::get_phase1(flip1, slice1, twist1, togo, next1); 99 | 100 | // Check inside loop to avoid unnecessary recursion unwinds 101 | if (dist1 == togo || dist1 + togo >= 5) { // Rokicki optimization 102 | int corners1 = coord::move_corners[corners][m]; 103 | moves[depth - 1] = m; 104 | 105 | next1 &= move::p1mask & move::next[m]; 106 | move::mask qt_skip1; 107 | #ifdef QT // let `qt_skip` get completely optimized away when not in QT-mode 108 | qt_skip1 = move::qt_skip[m]; 109 | next1 &= ~(qt_skip & qt_skip1); 110 | #endif 111 | phase1(depth, togo, flip1, slice1, twist1, corners1, next1, qt_skip1); 112 | } 113 | } 114 | 115 | // We always want to maintain the maximum number of already reconstructed EDGES coordinates, hence we only 116 | // decrement when the depth level gets lower than the current valid index (note that we will typically also 117 | // visit other deeper branches in between that might not have any effect on this) 118 | if (edges_depth == depth - 1) 119 | edges_depth--; 120 | } 121 | 122 | bool Search::phase2( 123 | int depth, int togo, int slice, int udedges2, int corners, move::mask next, move::mask qt_skip 124 | ) { 125 | if (togo == 0) { 126 | if (slice != coord::N_SLICE2 * coord::SLICE1_SOLVED) // check if SLICE2 is also solved 127 | return false; 128 | 129 | searchres sol = {std::vector(depth), dir }; 130 | for (int i = 0; i < depth; i++) 131 | sol.first[i] = moves[i]; 132 | solver.report_sol(sol); 133 | 134 | return true; // we will not find any shorter solutions 135 | } 136 | 137 | while (next) { 138 | int m = ffsll(next) - 1; // get rightmost move index (`ffsll()` uses 1-based indexing) 139 | next &= next - 1; 140 | 141 | int slice1 = coord::move_edges4[slice][m]; 142 | int udedges21 = coord::move_udedges2[udedges2][m]; 143 | int corners1 = coord::move_corners[corners][m]; 144 | 145 | if (prun::get_phase2(corners1, udedges21) < togo) { 146 | #ifdef QT 147 | // As we never want to leave the set of phase 2 cubes (which we would by doing only a quarter-turn on an axis 148 | // for which only double-moves are permitted), we need special handling of the double moves. The simplest way 149 | // to do this is to treat a double moves simply as if two consecutive quarter-turns were added to the current 150 | // search path. 151 | if (m >= move::COUNT1) { 152 | if (togo <= 1) // we cannot do half turns when only a single quarter-turn is permitted 153 | break; 154 | 155 | int tmp = move::split[m]; 156 | moves[depth] = tmp; 157 | moves[depth + 1] = tmp; 158 | 159 | move::mask next1 = move::p2mask & move::next[m]; 160 | move::mask qt_skip1 = move::qt_skip[m]; 161 | next1 &= ~(qt_skip & qt_skip1); 162 | 163 | if (phase2(depth + 2, togo - 2, slice1, udedges21, corners1, next1, qt_skip1)) 164 | return true; 165 | continue; 166 | } 167 | #endif 168 | 169 | moves[depth] = m; 170 | if (phase2(depth + 1, togo - 1, slice1, udedges21, corners1, move::p2mask & move::next[m], 0)) 171 | return true; // return as soon as we have a solution 172 | } 173 | } 174 | 175 | return false; 176 | } 177 | 178 | Engine::Engine( 179 | int n_threads, int tlim, 180 | int n_sols, int max_len, int n_splits 181 | ) : n_threads(n_threads), tlim(tlim), n_sols(n_sols), max_len(max_len), n_splits(n_splits) { 182 | int tmp = (move::COUNT1 + n_splits - 1) / n_splits; // ceil to make sure that we always include all moves 183 | for (int i = 0; i < n_splits; i++) 184 | masks[i] = (move::mask(1) << tmp) - 1 << tmp * i; 185 | done = true; // make sure that the first `prepare()` will actually do something 186 | } 187 | 188 | void Engine::thread() { 189 | int mindir = 0; 190 | do { 191 | /* Select next job to execute; don't forget to lock */ 192 | job_mtx.lock(); 193 | for (int dir = 0; dir < N_DIRS; dir++) { 194 | if (depths[dir] < depths[mindir]) 195 | mindir = dir; 196 | } 197 | int split = splits[mindir]++; 198 | int togo = depths[mindir]; 199 | if (splits[mindir] == n_splits) { 200 | depths[mindir]++; 201 | splits[mindir] = 0; 202 | } 203 | job_mtx.unlock(); 204 | 205 | Search search(mindir, dirs[mindir], togo, masks[split], done, lenlim, *this); 206 | search.run(); 207 | } while (!done); // we should never actually get to the truly optimal depth anyways in general 208 | } 209 | 210 | void Engine::prepare() { 211 | if (!done) // avoid double preparation 212 | return; 213 | finish(); 214 | 215 | job_mtx.lock(); // make spawned threads wait for initialization of the cube to be solved 216 | for (int i = 0; i < n_threads; i++) 217 | threads.push_back(std::thread([&]() { this->thread(); })); 218 | 219 | done = false; 220 | lenlim = max_len > 0 ? max_len + 1: 50; // only search for strictly shorter solutions than this 221 | // `sols` is always emptied after a solve 222 | } 223 | 224 | void Engine::solve(const cubie::cube& c, std::vector>& res) { 225 | prepare(); // make sure we are prepared; will do nothing if that should already be the case 226 | 227 | cubie::cube tmp1, tmp2; 228 | cubie::cube invc; 229 | cubie::inv(c, invc); 230 | 231 | for (int dir = 0; dir < N_DIRS; dir++) { 232 | const cubie::cube& c1 = (dir & 1) ? invc : c; // reference is enough, we do not need to copy 233 | int rot = sym::ROT * (dir / 2); 234 | cubie::mul(sym::cubes[sym::inv[rot]], c1, tmp1); 235 | cubie::mul(tmp1, sym::cubes[rot], tmp2); 236 | 237 | dirs[dir].flip = coord::get_flip(tmp2); 238 | dirs[dir].slice = coord::get_slice(tmp2); 239 | dirs[dir].twist = coord::get_twist(tmp2); 240 | dirs[dir].uedges = coord::get_uedges(tmp2); 241 | dirs[dir].dedges = coord::get_dedges(tmp2); 242 | dirs[dir].corners = coord::get_corners(tmp2); 243 | 244 | move::mask tmp; // simply ignore, makes no sense anyways without proper `togo` 245 | depths[dir] = prun::get_phase1(dirs[dir].flip, dirs[dir].slice, dirs[dir].twist, 100, tmp); 246 | splits[dir] = 0; 247 | } 248 | 249 | job_mtx.unlock(); // start solving 250 | 251 | { // timeout 252 | std::unique_lock lock(tout_mtx); 253 | tout_cvar.wait_for(lock, std::chrono::milliseconds(tlim), [&]{ return done; }); 254 | if (!done) 255 | done = true; // if we get here, this was a timeout 256 | } 257 | std::lock_guard lock(sol_mtx); // make sure no thread is writing any more solutions 258 | 259 | res.resize(sols.size()); 260 | for (int i = 0; i < res.size(); i++) { 261 | const searchres& sol = sols.top(); 262 | res[i].resize(sol.first.size()); 263 | 264 | int rot = sym::ROT * (sol.second / 2); 265 | for (int j = 0; j < res[i].size(); j++) // undo rotation 266 | res[i][j] = sym::conj_move[sol.first[j]][rot]; 267 | if (sol.second & 1) { // undo inversion 268 | for (int j = 0; j < res[i].size(); j++) 269 | res[i][j] = move::inv[res[i][j]]; 270 | std::reverse(res[i].begin(), res[i].end()); 271 | } 272 | 273 | sols.pop(); 274 | } 275 | std::reverse(res.begin(), res.end()); // return solutions in order of increasing length 276 | } 277 | 278 | void Engine::report_sol(searchres& sol) { 279 | std::lock_guard lock(sol_mtx); 280 | 281 | if (done) // prevent any type of reporting after the solver has terminated (important for threading) 282 | return; 283 | 284 | sols.push(sol); // usually we only get here if we actually have a solution that will be added 285 | if (sols.size() > n_sols) 286 | sols.pop(); 287 | if (sols.size() == n_sols) { 288 | lenlim = sols.top().first.size(); // only search for strictly shorter solutions 289 | 290 | if (lenlim <= max_len) { // already found a solution that is short enough 291 | done = true; // end searching 292 | // Wake up timeout 293 | std::lock_guard lock(tout_mtx); 294 | tout_cvar.notify_one(); 295 | } 296 | } 297 | } 298 | 299 | void Engine::finish() { 300 | for (std::thread& t : threads) // wait for all existing threads to actually finish 301 | t.join(); 302 | threads.clear(); // they are now invalid 303 | } 304 | 305 | } 306 | -------------------------------------------------------------------------------- /src/prun.cpp: -------------------------------------------------------------------------------- 1 | #include "prun.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace prun { 8 | const std::string SAVE = "twophase-" 9 | #ifdef AX 10 | "ax" 11 | #endif 12 | #ifdef QT 13 | "qt" 14 | #else 15 | "ht" 16 | #endif 17 | #ifdef F5 18 | "-f5" 19 | #endif 20 | ".tbl" 21 | ; 22 | 23 | const int EMPTY = 0xff; 24 | 25 | #ifdef AX 26 | const int BITS_PER_AX = 16; // bits used for encoding an axis in the ext. phase 1 table 27 | #else 28 | const int BITS_PER_AX = 8; 29 | #endif 30 | #ifdef QT 31 | const int BITS_PER_M = 2; // bits per move 32 | const int N_SIMP = 4; // number of simple moves 33 | const int N_AX = 4; // number of axial moves 34 | #else 35 | const int BITS_PER_M = 1; 36 | const int N_SIMP = 6; 37 | const int N_AX = 9; 38 | #endif 39 | 40 | // Used to remap symmetry ext. phase 1 table entries back to actual situation 41 | move::mask remap[2][16][1 << BITS_PER_AX]; 42 | 43 | prun1 *phase1; 44 | uint8_t *phase2; 45 | uint8_t *precheck; 46 | 47 | inline int ones(int count) { return (1 << count) - 1; } 48 | 49 | int rev(int movec, int count, int off = 0, int step = BITS_PER_M) { 50 | movec >>= step * off; 51 | 52 | int rev = 0; 53 | for (int i = 0; i < step * count; i += step) { 54 | rev = (rev << step) | (movec & ones(step)); 55 | movec >>= step; 56 | } 57 | return rev << step * off; 58 | } 59 | 60 | int inv(int mask) { 61 | int n_per_face = N_SIMP / 2; 62 | 63 | int inv = rev(mask, n_per_face) | rev(mask, n_per_face, n_per_face); 64 | #ifdef AX 65 | inv |= rev(mask, N_AX, N_SIMP); 66 | #endif 67 | return inv; 68 | } 69 | 70 | int flip(int mask) { 71 | int per_face = N_SIMP / 2; 72 | int flipped = rev(mask, 2, 0, BITS_PER_M * per_face); 73 | 74 | #ifdef AX 75 | mask >>= BITS_PER_M * N_SIMP; 76 | 77 | // Flipping an axis means to transpose the axial move-mask (M N) (M N2) (M N') ... to (N M) (N M2) (N M') ... 78 | int fax = 0; 79 | for (int i = 0; i < per_face; i++) { 80 | for (int j = 0; j < per_face; j++) 81 | fax |= ((mask >> BITS_PER_M * (per_face * i + j)) & ones(BITS_PER_M)) << BITS_PER_M * (per_face * j + i); 82 | } 83 | flipped |= fax << BITS_PER_M * N_SIMP; 84 | #endif 85 | 86 | return flipped; 87 | } 88 | 89 | void init_base() { 90 | /* It is probably cleanest to simply handle the special HT case individually */ 91 | #ifndef AX 92 | #ifndef QT 93 | for (int eff = 0; eff < 16; eff++) { 94 | for (int mask = 0; mask < 256; mask++) { 95 | int mask1 = mask & 0xf; 96 | int mask2 = (mask & 0xf0) >> 4; 97 | 98 | if (sym::eff_inv(eff)) { 99 | mask1 = rev(mask1, 3, 1) | (mask1 & 1); 100 | mask2 = rev(mask2, 3, 1) | (mask2 & 1); 101 | } 102 | if (sym::eff_flip(eff)) 103 | std::swap(mask1, mask2); 104 | 105 | remap[0][eff][mask] = ((mask1 & 1) ? 0 : ~(mask1 >> 1) & 0x7) << 6 * sym::eff_shift(eff); 106 | remap[1][eff][mask] = ((mask1 & 1) ? ~(mask1 >> 1) & 0x7 : 0x7) << 6 * sym::eff_shift(eff); 107 | remap[0][eff][mask] |= ((mask2 & 1) ? 0 : ~(mask2 >> 1) & 0x7) << 6 * sym::eff_shift(eff) + 3; 108 | remap[1][eff][mask] |= ((mask2 & 1) ? ~(mask2 >> 1) & 0x7 : 0x7) << 6 * sym::eff_shift(eff) + 3; 109 | } 110 | } 111 | return; 112 | #endif 113 | #endif 114 | 115 | for (int eff = 0; eff < 16; eff++) { 116 | for (int mask = 0; mask < (1 << BITS_PER_AX); mask++) { 117 | move::mask mask1 = mask; 118 | #ifndef QT 119 | mask1 >>= 1; // first bit encodes direction in HT 120 | #endif 121 | 122 | if (sym::eff_inv(eff)) 123 | mask1 = inv(mask1); 124 | if (sym::eff_flip(eff)) 125 | mask1 = flip(mask1); 126 | 127 | #ifdef QT 128 | remap[0][eff][mask] = 0; 129 | remap[1][eff][mask] = 0; 130 | for (int i = 0; i < BITS_PER_AX / 2; i++) { 131 | remap[0][eff][mask] |= ((mask1 & 0x3) == 0) << i; 132 | remap[1][eff][mask] |= ((mask1 & 0x3) <= 1) << i; 133 | mask1 >>= 2; 134 | } 135 | remap[0][eff][mask] <<= (BITS_PER_AX / 2) * sym::eff_shift(eff); 136 | remap[1][eff][mask] <<= (BITS_PER_AX / 2) * sym::eff_shift(eff); 137 | #else 138 | move::mask o = ones(BITS_PER_AX - 1); // first bit indicates direction but is not a move 139 | remap[0][eff][mask] = ((mask & 1) ? 0 : ~mask1 & o) << (BITS_PER_AX - 1) * sym::eff_shift(eff); 140 | remap[1][eff][mask] = ((mask & 1) ? ~mask1 & o : o) << (BITS_PER_AX - 1) * sym::eff_shift(eff); 141 | #endif 142 | } 143 | } 144 | } 145 | 146 | void init_phase1() { 147 | int n_moves = std::bitset<64>(move::p1mask).count(); // make sure not to consider B-moves in F5-mode 148 | 149 | phase1 = new prun1[N_FS1TWIST]; 150 | std::fill(phase1, phase1 + N_FS1TWIST, EMPTY); 151 | 152 | phase1[coord::N_TWIST * sym::coord_c(sym::fslice1_sym[coord::fslice1(0, coord::SLICE1_SOLVED)])] = 0; 153 | int count = 0; 154 | int dist = 0; 155 | 156 | while (count < N_FS1TWIST) { 157 | int coord = 0; 158 | 159 | for (int fs1sym = 0; fs1sym < sym::N_FSLICE1; fs1sym++) { 160 | int fslice1 = sym::fslice1_raw[fs1sym]; 161 | int flip = coord::fslice1_to_flip(fslice1); 162 | int slice = coord::slice1_to_slice(coord::fslice1_to_slice1(fslice1)); 163 | 164 | for (int twist = 0; twist < coord::N_TWIST; twist++) { 165 | if ((phase1[coord] & 0xff) == dist) { 166 | count++; 167 | int deltas[move::COUNT1]; // easier encoding if B-face always exists (F5-mode ignores it anyways) 168 | 169 | for (int m = 0; m < n_moves; m++) { 170 | int slice11 = coord::slice_to_slice1(coord::move_edges4[slice][m]); 171 | int fslice11 = coord::fslice1(coord::move_flip[flip][m], slice11); 172 | int tmp = sym::fslice1_sym[fslice11]; 173 | int twist1 = sym::conj_twist[coord::move_twist[twist][m]][sym::coord_s(tmp)]; 174 | int fs1sym1 = sym::coord_c(tmp); 175 | int coord1 = coord::N_TWIST * fs1sym1 + twist1; 176 | 177 | if (phase1[coord1] == EMPTY) 178 | phase1[coord1] = dist + 1; 179 | deltas[m] = (phase1[coord1] & 0xff) - dist; 180 | coord1 -= twist1; // only TWIST part changes below 181 | 182 | int selfs = sym::fslice1_selfs[fs1sym1] >> 1; 183 | for (int s = 1; selfs > 0; s++) { // bit 0 is always on 184 | if (selfs & 1) { 185 | int coord2 = coord1 + sym::conj_twist[twist1][s]; 186 | if (phase1[coord2] == EMPTY) 187 | phase1[coord2] = dist + 1; 188 | } 189 | selfs >>= 1; 190 | } 191 | } 192 | 193 | prun1 prun = 0; 194 | #ifdef QT 195 | // In QT there is enough space to simply encode the effect of every move in 2 bits 196 | for (int m = n_moves - 1; m >= 0; m--) 197 | prun = (prun << 2) | (deltas[m] + 1); 198 | #else 199 | #ifndef AX 200 | int n_ax = 6; // in standard (HT) mode we have to treat every face as an individual axis for encoding 201 | int bits_per_ax = 4; 202 | #else 203 | int n_ax = 3; 204 | int bits_per_ax = BITS_PER_AX; 205 | #endif 206 | /* Encode from left to right to preserve indexing of moves */ 207 | for (int ax = n_ax - 1; ax >= 0; ax--) { 208 | bool away = false; // first bit of axis encoding (whether any move brings us further from the goal) 209 | for (int i = ax * (bits_per_ax - 1); i < (ax + 1) * (bits_per_ax - 1); i++) { 210 | if (deltas[i] != 0) { 211 | if (deltas[i] > 0) 212 | away = true; 213 | break; // stop immediately once we found a value != 0 214 | } 215 | } 216 | 217 | int tmp = 0; 218 | for (int i = (ax + 1) * (bits_per_ax - 1) - 1; i >= ax * (bits_per_ax - 1); i--) 219 | tmp = (tmp | (away ? deltas[i] : deltas[i] + 1)) << 1; 220 | tmp |= away; 221 | 222 | prun = (prun << bits_per_ax) | tmp; 223 | } 224 | #endif 225 | phase1[coord] |= prun << 8; 226 | } 227 | coord++; 228 | } 229 | } 230 | 231 | std::cout << dist << " " << count << std::endl; 232 | dist++; 233 | } 234 | } 235 | 236 | void init_phase2() { 237 | phase2 = new uint8_t[N_CORNUD2]; 238 | std::fill(phase2, phase2 + N_CORNUD2, EMPTY); 239 | 240 | phase2[0] = 0; 241 | int count = 0; 242 | int dist = 0; 243 | 244 | while (count < N_CORNUD2) { 245 | int coord = 0; 246 | 247 | for (int csym = 0; csym < sym::N_CORNERS; csym++) { 248 | int corners = sym::corners_raw[csym]; 249 | 250 | for (int udedges2 = 0; udedges2 < coord::N_UDEDGES2; udedges2++) { 251 | if (phase2[coord] == dist) { 252 | count++; 253 | 254 | for (move::mask moves = move::p2mask; moves; moves &= moves - 1) { 255 | int m = ffsll(moves) - 1; 256 | 257 | int dist1 = dist + 1; 258 | #ifdef QT 259 | if (m >= move::COUNT1) 260 | dist1++; // half-turns cost 2 in QTM 261 | #endif 262 | 263 | int corners1 = coord::move_corners[corners][m]; 264 | int udedges21 = coord::move_udedges2[udedges2][m]; 265 | int tmp = sym::corners_sym[corners1]; 266 | udedges21 = sym::conj_udedges2[udedges21][sym::coord_s(tmp)]; 267 | int csym1 = sym::coord_c(tmp); 268 | int coord1 = coord::N_UDEDGES2 * csym1 + udedges21; 269 | 270 | if (phase2[coord1] <= dist1) 271 | continue; 272 | phase2[coord1] = dist1; 273 | coord1 -= udedges21; 274 | 275 | int selfs = sym::corners_selfs[csym1] >> 1; 276 | for (int s = 1; selfs > 0; s++) { 277 | if (selfs & 1) { 278 | int coord2 = coord1 + sym::conj_udedges2[udedges21][s]; 279 | if (phase2[coord2] > dist1) 280 | phase2[coord2] = dist1; 281 | } 282 | selfs >>= 1; 283 | } 284 | } 285 | } 286 | coord++; 287 | } 288 | } 289 | 290 | std::cout << dist << " " << count << std::endl; 291 | dist++; 292 | } 293 | } 294 | 295 | void init_precheck() { 296 | precheck = new uint8_t[N_CSLICE2]; 297 | std::fill(precheck, precheck + N_CSLICE2, EMPTY); 298 | 299 | precheck[0] = 0; 300 | int dist = 0; 301 | int count = 0; 302 | 303 | while (count < N_CSLICE2) { 304 | int coord = 0; 305 | 306 | for (int corners = 0; corners < coord::N_CORNERS; corners++) { 307 | for (int slice2 = 0; slice2 < coord::N_SLICE2; slice2++) { 308 | if (precheck[coord] == dist) { 309 | count++; 310 | int slice = coord::slice2_to_slice(slice2); 311 | 312 | for (move::mask moves = move::p2mask; moves; moves &= moves - 1) { 313 | int m = ffsll(moves) - 1; 314 | 315 | int dist1 = dist + 1; 316 | #ifdef QT 317 | if (m >= move::COUNT1) 318 | dist1++; // half-turns cost 2 in QTM 319 | #endif 320 | 321 | int corners1 = coord::move_corners[corners][m]; 322 | int slice21 = coord::slice_to_slice2(coord::move_edges4[slice][m]); 323 | 324 | int coord1 = coord::N_SLICE2 * corners1 + slice21; 325 | if (precheck[coord1] > dist1) 326 | precheck[coord1] = dist1; 327 | } 328 | } 329 | coord++; 330 | } 331 | } 332 | 333 | std::cout << dist << " " << count << std::endl; 334 | dist++; 335 | } 336 | } 337 | 338 | int get_phase1(int flip, int slice, int twist, int togo, move::mask& next) { 339 | int tmp = sym::fslice1_sym[coord::fslice1(flip, coord::slice_to_slice1(slice))]; 340 | int s = sym::coord_s(tmp); 341 | prun1 prun = phase1[coord::N_TWIST * sym::coord_c(tmp) + sym::conj_twist[twist][s]]; 342 | 343 | int dist = prun & 0xff; 344 | int delta = togo - dist; 345 | 346 | // `delta` < 0 case can never happen during a real search 347 | if (delta > 1) 348 | next = move::p1mask; // all moves are possible 349 | else { 350 | prun >>= 8; // get rid of dist 351 | next = 0; 352 | for (int ax = 0; ax < 3; ax++) { 353 | next |= remap[delta][sym::effect[s][ax]][prun & ones(BITS_PER_AX)]; 354 | prun >>= BITS_PER_AX; 355 | } 356 | } 357 | 358 | return dist; 359 | } 360 | 361 | int get_phase2(int corners, int udedges) { 362 | int tmp = sym::corners_sym[corners]; 363 | return phase2[coord::N_UDEDGES2 * sym::coord_c(tmp) + sym::conj_udedges2[udedges][sym::coord_s(tmp)]]; 364 | } 365 | 366 | int get_precheck(int corners, int slice) { 367 | return precheck[coord::N_SLICE2 * corners + coord::slice_to_slice2(slice)]; 368 | } 369 | 370 | bool init(bool file) { 371 | init_base(); 372 | 373 | if (!file) { 374 | init_phase1(); 375 | init_phase2(); 376 | init_precheck(); 377 | return true; 378 | } 379 | 380 | FILE *f = fopen(SAVE.c_str(), "rb"); 381 | int err = 0; 382 | 383 | if (f == NULL) { 384 | init_phase1(); 385 | init_phase2(); 386 | init_precheck(); 387 | 388 | f = fopen(SAVE.c_str(), "wb"); 389 | if (fwrite(phase1, sizeof(prun1), N_FS1TWIST, f) != N_FS1TWIST) 390 | err = 1; 391 | if (fwrite(phase2, sizeof(uint8_t), N_CORNUD2, f) != N_CORNUD2) 392 | err = 1; 393 | if (fwrite(precheck, sizeof(uint8_t), N_CSLICE2, f) != N_CSLICE2) 394 | err = 1; 395 | if (err) 396 | remove(SAVE.c_str()); // delete file if there was some error writing it 397 | } else { 398 | phase1 = new prun1[N_FS1TWIST]; 399 | phase2 = new uint8_t[N_CORNUD2]; 400 | precheck = new uint8_t[N_CSLICE2]; 401 | if (fread(phase1, sizeof(prun1), N_FS1TWIST, f) != N_FS1TWIST) 402 | err = 1; 403 | if (fread(phase2, sizeof(uint8_t), N_CORNUD2, f) != N_CORNUD2) 404 | err = 1; 405 | if (fread(precheck, sizeof(uint8_t), N_CSLICE2, f) != N_CSLICE2) 406 | err = 1; 407 | } 408 | 409 | fclose(f); 410 | return err; 411 | } 412 | 413 | } 414 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------