enumerate.h
Go to the documentation of this file.
1/* Copyright (C) 2008-2011 Xavier Pujol
2 (C) 2015 Michael Walter.
3 (C) 2016 Marc Stevens. (generic improvements, auxiliary solutions, subsolutions)
4 (C) 2016 Guillaume Bonnoron. (CVP improvements)
5
6 This file is part of fplll. fplll is free software: you
7 can redistribute it and/or modify it under the terms of the GNU Lesser
8 General Public License as published by the Free Software Foundation,
9 either version 2.1 of the License, or (at your option) any later version.
10
11 fplll is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with fplll. If not, see <http://www.gnu.org/licenses/>. */
18
19#ifndef FPLLL_ENUMERATE_H
20#define FPLLL_ENUMERATE_H
21
22#include <array>
26#include <fplll/gso_interface.h>
27#include <memory>
28
30
31template <typename ZT, typename FT> class EnumerationDyn : public EnumerationBase
32{
33public:
35 const vector<int> &max_indices = vector<int>())
36 : _gso(gso), _evaluator(evaluator)
37 {
38 _max_indices = max_indices;
39 std::fill(nodes.begin(), nodes.end(), 0);
40 }
41
42 void enumerate(int first, int last, FT &fmaxdist, long fmaxdistexpo,
43 const vector<FT> &target_coord = vector<FT>(),
44 const vector<enumxt> &subtree = vector<enumxt>(),
45 const vector<enumf> &pruning = vector<enumf>(), bool dual = false,
46 bool subtree_reset = false);
47
48 inline uint64_t get_nodes(const int level = -1) const
49 {
50 if (level == -1)
51 {
52 return std::accumulate(nodes.cbegin(), nodes.cend(), 0);
53 }
54 return nodes[level];
55 }
56
57 inline array<uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM> get_nodes_array() { return nodes; }
58
59private:
61 Evaluator<FT> &_evaluator;
62 vector<FT> target;
63
64 vector<enumf> pruning_bounds;
66 vector<FT> fx;
67
68 void prepare_enumeration(const vector<enumxt> &subtree, bool solvingsvp, bool subtree_reset);
69
70 void do_enumerate();
71
72 void set_bounds();
73 void reset(enumf cur_dist, int cur_depth);
74 virtual void process_solution(enumf newmaxdist);
75 virtual void process_subsolution(int offset, enumf newdist);
76};
77
78template <typename ZT, typename FT> class Enumeration
79{
80public:
82 const vector<int> &max_indices = vector<int>())
83 : _gso(gso), _evaluator(evaluator), _max_indices(max_indices), enumdyn(nullptr), _nodes{}
84 {
85 }
86
87 void enumerate(int first, int last, FT &fmaxdist, long fmaxdistexpo,
88 const vector<FT> &target_coord = vector<FT>(),
89 const vector<enumxt> &subtree = vector<enumxt>(),
90 const vector<enumf> &pruning = vector<enumf>(), bool dual = false,
91 bool subtree_reset = false)
92 {
93 // check for external enumerator and use that
94 if (get_external_enumerator() != nullptr && subtree.empty() && target_coord.empty())
95 {
96 if (enumext.get() == nullptr)
97 enumext.reset(new ExternalEnumeration<ZT, FT>(_gso, _evaluator));
98 if (enumext->enumerate(first, last, fmaxdist, fmaxdistexpo, pruning, dual))
99 {
100 _nodes = enumext->get_nodes_array();
101 return;
102 }
103 }
104 // if external enumerator is not available, not possible or when it fails then fall through to
105 // fplll enumeration
106 if (enumdyn.get() == nullptr)
107 enumdyn.reset(new EnumerationDyn<ZT, FT>(_gso, _evaluator, _max_indices));
108 enumdyn->enumerate(first, last, fmaxdist, fmaxdistexpo, target_coord, subtree, pruning, dual,
109 subtree_reset);
110 _nodes = enumdyn->get_nodes_array();
111 }
112
113 inline uint64_t get_nodes(const int level = -1) const
114 {
115 if (level == -1)
116 return std::accumulate(_nodes.begin(), _nodes.end(), 0);
117 return _nodes[level];
118 }
119
120 inline array<uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM> get_nodes_array() const { return _nodes; }
121
122private:
124 Evaluator<FT> &_evaluator;
125 vector<int> _max_indices;
126 std::unique_ptr<EnumerationDyn<ZT, FT>> enumdyn;
127 std::unique_ptr<ExternalEnumeration<ZT, FT>> enumext;
128 array<uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM> _nodes;
129};
130
132
133#endif
Definition: enumerate_base.h:57
array< uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM > nodes
Definition: enumerate_base.h:101
vector< int > _max_indices
Definition: enumerate_base.h:94
Definition: enumerate.h:32
void enumerate(int first, int last, FT &fmaxdist, long fmaxdistexpo, const vector< FT > &target_coord=vector< FT >(), const vector< enumxt > &subtree=vector< enumxt >(), const vector< enumf > &pruning=vector< enumf >(), bool dual=false, bool subtree_reset=false)
Definition: enumerate.cpp:59
array< uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM > get_nodes_array()
Definition: enumerate.h:57
EnumerationDyn(MatGSOInterface< ZT, FT > &gso, Evaluator< FT > &evaluator, const vector< int > &max_indices=vector< int >())
Definition: enumerate.h:34
uint64_t get_nodes(const int level=-1) const
Definition: enumerate.h:48
Definition: enumerate.h:79
Enumeration(MatGSOInterface< ZT, FT > &gso, Evaluator< FT > &evaluator, const vector< int > &max_indices=vector< int >())
Definition: enumerate.h:81
void enumerate(int first, int last, FT &fmaxdist, long fmaxdistexpo, const vector< FT > &target_coord=vector< FT >(), const vector< enumxt > &subtree=vector< enumxt >(), const vector< enumf > &pruning=vector< enumf >(), bool dual=false, bool subtree_reset=false)
Definition: enumerate.h:87
uint64_t get_nodes(const int level=-1) const
Definition: enumerate.h:113
array< uint64_t, FPLLL_EXTENUM_MAX_EXTENUM_DIM > get_nodes_array() const
Definition: enumerate.h:120
Definition: evaluator.h:66
Definition: enumerate_ext.h:105
Definition: gso_interface.h:60
#define FPLLL_END_NAMESPACE
Definition: defs.h:117
#define FPLLL_BEGIN_NAMESPACE
Definition: defs.h:114
std::function< extenum_fc_enumerate > get_external_enumerator()
Definition: enumerate_ext.cpp:46
fplll_extenum_enumf std::function< extenum_cb_set_config > std::function< extenum_cb_process_sol > std::function< extenum_cb_process_subsol > bool dual
Definition: enumerate_ext_api.h:91
fplll_extenum_enumf maxdist
Definition: enumerate_ext_api.h:89
FPLLL_BEGIN_NAMESPACE typedef double enumf
Definition: nr.h:42