2 #include <bernstein/bernstein.h>
3 #include <bernstein/piecewise_lst.h>
4 #include <barvinok/options.h>
5 #include <barvinok/bernstein.h>
7 #include "param_util.h"
8 #include "omega/convert.h"
10 using namespace GiNaC
;
11 using namespace bernstein
;
14 void maximize(PolyFunc
*polyfunc
, Map
<Variable_Ref
*, GiNaC::ex
>& variableMap
)
21 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
23 cout
<< "maximize " << polyfunc
->poly
<< " over ";
24 polyfunc
->domain
.simplify();
25 polyfunc
->domain
.print(stdout
);
27 Polyhedron
*D
= relation2Domain(polyfunc
->domain
, vv
, params
);
29 assert(polyfunc
->domain
.is_set());
30 int dim
= polyfunc
->domain
.n_set();
31 assert(D
->Dimension
== dim
+ params
.size());
32 Polyhedron
*ctx
= Universe_Polyhedron(params
.size());
34 for (int i
= 0; i
< dim
; ++i
) {
36 foreach_map(vr
,Variable_Ref
*,s
,ex
,variableMap
, {
37 if (vr
->vid
== vv
[i
]) {
43 exvars
.push_back(var
);
45 for (int i
= dim
; i
< vv
.size(); ++i
) {
46 Global_Var_ID global
= vv
[i
]->get_global_var();
48 foreach_map(vr
,Variable_Ref
*,s
,ex
,variableMap
, {
49 if (vr
->g
== global
) {
55 exparams
.push_back(var
);
57 exparams
.push_back(symbol(vv
[i
]->char_name()));
60 PP
= Polyhedron2Param_Polyhedron(D
, ctx
, options
);
61 piecewise_lst
*pl
= new piecewise_lst(exparams
);
62 for (Param_Domain
*Q
= PP
->D
; Q
; Q
= Q
->next
) {
63 GiNaC::matrix VM
= domainVertices(PP
, Q
, exparams
);
64 lst coeffs
= bernsteinExpansion(VM
, polyfunc
->poly
, exvars
, exparams
);
65 pl
->list
.push_back(guarded_lst(Polyhedron_Copy(Q
->Domain
), coeffs
));
67 cout
<< "coefficients: " << *pl
<< endl
;
69 cout
<< "maximum: " << *pl
<< endl
;
71 Param_Polyhedron_Free(PP
);
75 barvinok_options_free(options
);