Lattice Land Parsing Library
Loading...
Searching...
No Matches
solver_output.hpp
Go to the documentation of this file.
1// Copyright 2024 Thibault Falque, Pierre Talbot
2
3#ifndef LALA_PARSING_SOLVER_OUTPUT_HPP
4#define LALA_PARSING_SOLVER_OUTPUT_HPP
5
6#include <functional>
7#include <peglib.h>
8#include <lala/logic/ast.hpp>
9
10namespace lala {
11
12enum class OutputType {
13 XCSP,
15};
16
17template<class Allocator>
19 using bstring = battery::string<Allocator>;
20 template<class T> using bvector = battery::vector<T, Allocator>;
21 using array_dim_t = bvector<battery::tuple<size_t,size_t>>;
22 using F = TFormula<Allocator>;
23
24 bvector<bstring> output_vars;
25 // For each array, we store its output dimension characteristics and the list of the variables in the array.
26 // We also encode sets in the same vector (the Boolean is `true` if it is a set variable).
27 bvector<battery::tuple<bstring, bool, array_dim_t, bvector<bstring>>> output_arrays;
28
29 OutputType type;
30 std::string join_str(const bvector<bstring>& vec, const std::string& separator, std::function<std::string(const bstring&)> toString) const {
31 std::string result;
32 for (size_t i = 0; i < vec.size(); ++i) {
33 result += toString(vec[i]);
34 if (i < vec.size() - 1) {
35 result += separator;
36 }
37 }
38 return result;
39 }
40
41public:
42 template <class Alloc2>
43 friend class SolverOutput;
44
45 CUDA SolverOutput(const Allocator& alloc)
46 : output_vars(alloc)
47 , output_arrays(alloc),type(OutputType::FLATZINC)
48 {}
49
50 CUDA SolverOutput(const Allocator& alloc,OutputType outputType)
51 : output_vars(alloc)
52 , output_arrays(alloc),type(outputType)
53 {}
54
57
58 template <class Alloc>
60 output_vars = other.output_vars;
61 output_arrays = other.output_arrays;
62 type = other.type;
63 return *this;
64 }
65
66 template<class Alloc2>
67 CUDA SolverOutput(const SolverOutput<Alloc2>& other, const Allocator& allocator = Allocator{})
68 : output_vars(other.output_vars, allocator)
69 , output_arrays(other.output_arrays, allocator),type(other.type)
70 {}
71
72 void add_array_var(const std::string& name, const bstring& var_name, const peg::SemanticValues& sv) {
73 int idx = -1;
74 auto array_name = bstring(name.data());
75 for(int i = 0; i < output_arrays.size(); ++i) {
76 if(battery::get<0>(output_arrays[i]) == array_name) {
77 idx = i;
78 break;
79 }
80 }
81 if(idx == -1) {
82 output_arrays.push_back(battery::make_tuple<bstring, bool, array_dim_t, bvector<bstring>>(bstring(array_name), false, {}, {}));
83 idx = static_cast<int>(output_arrays.size()) - 1;
84 // Add the dimension of the array.
85 for(int i = 0; i < sv.size(); ++i) {
86 auto range = std::any_cast<F>(sv[i]);
87 for(int j = 0; j < range.s().size(); ++j) {
88 const auto& itv = range.s()[j];
89 battery::get<2>(output_arrays[idx]).push_back(battery::make_tuple(battery::get<0>(itv).z(), battery::get<1>(itv).z()));
90 }
91 }
92 }
93 battery::get<3>(output_arrays[idx]).push_back(var_name);
94 }
95
96 void add_var(const bstring& var_name) {
97 output_vars.push_back(var_name);
98 }
99
101 template <class Alloc, class B, class Env>
102 CUDA void print_variable(const LVar<Alloc>& vname, const Env& benv, const B& b) const {
103 const auto& x = *(benv.variable_of(vname));
104 x.sort.print_value(b.project(x.avars[0]));
105 }
106 };
107
108 template <class Env, class A, class S>
109 CUDA void print_solution_flatzinc(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
110 for(int i = 0; i < output_vars.size(); ++i) {
111 printf("%s=", output_vars[i].data());
112 simplifier.print_variable(output_vars[i], env, sol);
113 printf(";\n");
114 }
115 for(int i = 0; i < output_arrays.size(); ++i) {
116 const auto& dims = battery::get<2>(output_arrays[i]);
117 const auto& array_vars = battery::get<3>(output_arrays[i]);
118 printf("%s=array%" PRIu64 "d(", battery::get<0>(output_arrays[i]).data(), dims.size());
119 for(int j = 0; j < dims.size(); ++j) {
120 printf("%" PRIu64 "..%" PRIu64 ",", battery::get<0>(dims[j]), battery::get<1>(dims[j]));
121 }
122 printf("[");
123 for(int j = 0; j < array_vars.size(); ++j) {
124 simplifier.print_variable(array_vars[j], env, sol);
125 if(j+1 != array_vars.size()) {
126 printf(",");
127 }
128 }
129 printf("]);\n");
130 }
131 }
132
133 template<class Env, class A, class S>
134 CUDA void print_solution_xml(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
135 auto vars = join_str(output_vars, " ", [](const bstring& s) -> std::string { return s.data(); });
136 printf("v <instantiation> <list>%s</list> <values>", vars.c_str());
137 for (int i = 0; i < output_vars.size(); ++i) {
138 simplifier.print_variable(output_vars[i], env, sol);
139 if(i+1 != output_vars.size()) {
140 printf(" ");
141 }
142 }
143 printf("</values> </instantiation>\n");
144 }
145
146 template <class Env, class A, class S>
147 CUDA void print_solution(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
148 if(type == OutputType::FLATZINC) {
149 print_solution_flatzinc(env, sol, simplifier);
150 }
151 else {
152 print_solution_xml(env, sol, simplifier);
153 }
154 }
155};
156
157} // namespace lala
158
159#endif
Definition solver_output.hpp:100
Definition solver_output.hpp:18
CUDA SolverOutput(const SolverOutput< Alloc2 > &other, const Allocator &allocator=Allocator{})
Definition solver_output.hpp:67
CUDA SolverOutput(const Allocator &alloc, OutputType outputType)
Definition solver_output.hpp:50
void add_var(const bstring &var_name)
Definition solver_output.hpp:96
CUDA SolverOutput(const Allocator &alloc)
Definition solver_output.hpp:45
CUDA void print_solution(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:147
SolverOutput(SolverOutput &&)=default
CUDA SolverOutput< Allocator > & operator=(const SolverOutput< Alloc > &other)
Definition solver_output.hpp:59
CUDA void print_solution_flatzinc(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:109
CUDA void print_solution_xml(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:134
void add_array_var(const std::string &name, const bstring &var_name, const peg::SemanticValues &sv)
Definition solver_output.hpp:72
SolverOutput< Allocator > & operator=(const SolverOutput< Allocator > &)=default
Definition flatzinc_parser.hpp:21
OutputType
Definition solver_output.hpp:12