dep_selector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ //
2
+ // Author:: Christopher Walters (<cw@opscode.com>)
3
+ // Author:: Mark Anderson (<mark@opscode.com>)
4
+ // Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ // License:: Apache License, Version 2.0
6
+ //
7
+ // Licensed under the Apache License, Version 2.0 (the "License");
8
+ // you may not use this file except in compliance with the License.
9
+ // You may obtain a copy of the License at
10
+ //
11
+ // http://www.apache.org/licenses/LICENSE-2.0
12
+ //
13
+ // Unless required by applicable law or agreed to in writing, software
14
+ // distributed under the License is distributed on an "AS IS" BASIS,
15
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ // See the License for the specific language governing permissions and
17
+ // limitations under the License.
18
+ //
19
+
20
+ #ifndef dep_selector_to_gecode_h
21
+ #define dep_selector_to_gecode_h
22
+
23
+ #include "dep_selector_to_gecode_interface.h"
24
+ #include <iostream>
25
+ #include <vector>
26
+
27
+ #include <gecode/driver.hh>
28
+ #include <gecode/int.hh>
29
+ #include <gecode/minimodel.hh>
30
+
31
+ using namespace Gecode;
32
+
33
+ // TODO:
34
+ // Allow retrieval of multiple solutions
35
+ // Understand how assign versions where necessary, and not assign unnecessary versions.
36
+ // Understand how to assign empty versions
37
+ //
38
+
39
+ // Extend:
40
+ // Add optimization functions
41
+ // Allow non-contiguous ranges in package dependencies.
42
+
43
+ class VersionProblem : public Space
44
+ {
45
+ public:
46
+ static const int UNRESOLVED_VARIABLE;
47
+ static const int MIN_TRUST_LEVEL;
48
+ static const int MAX_TRUST_LEVEL;
49
+ static const int MAX_PREFERRED_WEIGHT;
50
+
51
+ VersionProblem(int packageCount);
52
+ // Clone constructor; check gecode rules for this...
53
+ VersionProblem(bool share, VersionProblem & s);
54
+ virtual ~VersionProblem();
55
+
56
+ int Size();
57
+ int PackageCount();
58
+
59
+ IntVar & GetPackageVersionVar(int packageId);
60
+
61
+ virtual int AddPackage(int minVersion, int maxVersion, int currentVersion);
62
+
63
+ virtual bool AddVersionConstraint(int packageId, int version,
64
+ int dependentPackageId, int minDependentVersion, int maxDependentVersion);
65
+
66
+ // We may wish to indicate that some packages have suspicious constraints, and when chosing packages to disable we
67
+ // would disable them first.
68
+ void MarkPackageSuspicious(int packageId);
69
+
70
+ void MarkPackageRequired(int packageId);
71
+
72
+ // Packages marked by this method are preferentially chosen at
73
+ // latest according to weights
74
+ void MarkPackagePreferredToBeAtLatest(int packageId, int weight);
75
+
76
+ void Finalize();
77
+
78
+ virtual void constrain(const Space & _best_known_solution);
79
+
80
+ int GetPackageVersion(int packageId);
81
+ bool GetPackageDisabledState(int packageId);
82
+ int GetMax(int packageId);
83
+ int GetMin(int packageId);
84
+ int GetDisabledVariableCount();
85
+
86
+ // Support for gecode
87
+ virtual Space* copy(bool share);
88
+
89
+ // Debug and utility functions
90
+ void Print(std::ostream &out);
91
+ void PrintPackageVar(std::ostream & out, int packageId) ;
92
+
93
+ static VersionProblem *Solve(VersionProblem *problem);
94
+
95
+
96
+ protected:
97
+ int size;
98
+ int cur_package;
99
+ bool CheckPackageId(int id);
100
+ bool finalized;
101
+ // std::vector<int> test;
102
+ BoolVarArgs version_flags;
103
+ IntVarArray package_versions;
104
+ BoolVarArray disabled_package_variables;
105
+ IntVar total_disabled;
106
+
107
+ IntVar total_required_disabled;
108
+ IntVar total_induced_disabled;
109
+ IntVar total_suspicious_disabled;
110
+
111
+ BoolVarArray at_latest;
112
+ IntVar total_preferred_at_latest;
113
+ IntVar total_not_preferred_at_latest;
114
+
115
+ int * disabled_package_weights;
116
+ int * preferred_at_latest_weights;
117
+ int * is_required;
118
+ int * is_suspicious;
119
+
120
+ void AddPackagesPreferredToBeAtLatestObjectiveFunction(const VersionProblem & best_known_solution);
121
+ void ConstrainVectorLessThanBest(IntVarArgs & current, IntVarArgs & best);
122
+ void BuildCostVector(IntVarArgs & costVector) const;
123
+ };
124
+
125
+ template<class T> void PrintVarAligned(const char * message, T & var);
126
+ template<class S, class T> void PrintVarAligned(const char * message, S & var1, T & var2);
127
+
128
+ class Solver {
129
+ public:
130
+ Solver(VersionProblem *s);
131
+ VersionProblem GetNextSolution();
132
+ private:
133
+ Restart<VersionProblem> solver;
134
+ };
135
+
136
+ #endif dep_selector_to_gecode_h
@@ -0,0 +1,122 @@
1
+ //
2
+ // Author:: Christopher Walters (<cw@opscode.com>)
3
+ // Author:: Mark Anderson (<mark@opscode.com>)
4
+ // Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ // License:: Apache License, Version 2.0
6
+ //
7
+ // Licensed under the Apache License, Version 2.0 (the "License");
8
+ // you may not use this file except in compliance with the License.
9
+ // You may obtain a copy of the License at
10
+ //
11
+ // http://www.apache.org/licenses/LICENSE-2.0
12
+ //
13
+ // Unless required by applicable law or agreed to in writing, software
14
+ // distributed under the License is distributed on an "AS IS" BASIS,
15
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ // See the License for the specific language governing permissions and
17
+ // limitations under the License.
18
+ //
19
+
20
+ #include <iostream>
21
+
22
+ #include "dep_selector_to_gecode_interface.h"
23
+ #include "dep_selector_to_gecode.h"
24
+ //#include "version_problem_oc_ih.h"
25
+
26
+ //
27
+ // TODO:
28
+ // Trap all exceptions
29
+ // insure proper memory behaviour
30
+
31
+ // FFI friendly
32
+ VersionProblem * VersionProblemCreate(int packageCount)
33
+ {
34
+ return new VersionProblem(packageCount);
35
+ }
36
+
37
+ void VersionProblemDestroy(VersionProblem * p)
38
+ {
39
+ delete p;
40
+ }
41
+
42
+ int VersionProblemSize(VersionProblem *p)
43
+ {
44
+ return p->Size();
45
+ }
46
+
47
+ int VersionProblemPackageCount(VersionProblem *p)
48
+ {
49
+ return p->PackageCount();
50
+ }
51
+
52
+
53
+
54
+ void VersionProblemDump(VersionProblem *p)
55
+ {
56
+ p->Print(std::cout);
57
+ std::cout.flush();
58
+ }
59
+
60
+ void VersionProblemPrintPackageVar(VersionProblem *p, int packageId)
61
+ {
62
+ p->PrintPackageVar(std::cout, packageId);
63
+ std::cout.flush();
64
+ }
65
+
66
+ // Return ID #
67
+ int AddPackage(VersionProblem *problem, int min, int max, int currentVersion) {
68
+ problem->AddPackage(min,max,currentVersion);
69
+ }
70
+ // Add constraint for package pkg @ version,
71
+ // that dependentPackage is at version [minDependentVersion,maxDependentVersion]
72
+ // Returns false if system becomes insoluble.
73
+ bool AddVersionConstraint(VersionProblem *problem, int packageId, int version,
74
+ int dependentPackageId, int minDependentVersion, int maxDependentVersion)
75
+ {
76
+ problem->AddVersionConstraint(packageId, version, dependentPackageId, minDependentVersion, maxDependentVersion);
77
+ }
78
+
79
+ void MarkPackageSuspicious(VersionProblem *problem, int packageId)
80
+ {
81
+ problem->MarkPackageSuspicious(packageId);
82
+ }
83
+
84
+ void MarkPackagePreferredToBeAtLatest(VersionProblem *problem, int packageId, int weight)
85
+ {
86
+ problem->MarkPackagePreferredToBeAtLatest(packageId, weight);
87
+ }
88
+
89
+ void MarkPackageRequired(VersionProblem *problem, int packageId)
90
+ {
91
+ problem->MarkPackageRequired(packageId);
92
+ }
93
+
94
+ int GetPackageVersion(VersionProblem *problem, int packageId)
95
+ {
96
+ problem->GetPackageVersion(packageId);
97
+ }
98
+
99
+ bool GetPackageDisabledState(VersionProblem *problem, int packageId)
100
+ {
101
+ problem->GetPackageDisabledState(packageId);
102
+ }
103
+
104
+ int GetPackageMax(VersionProblem *problem, int packageId)
105
+ {
106
+ problem->GetMax(packageId);
107
+ }
108
+
109
+ int GetPackageMin(VersionProblem *problem, int packageId)
110
+ {
111
+ problem->GetMin(packageId);
112
+ }
113
+
114
+ int GetDisabledVariableCount(VersionProblem *problem)
115
+ {
116
+ problem->GetDisabledVariableCount();
117
+ }
118
+
119
+
120
+ VersionProblem * Solve(VersionProblem * problem) {
121
+ return VersionProblem::Solve(problem);
122
+ }
@@ -0,0 +1,70 @@
1
+ //
2
+ // Author:: Christopher Walters (<cw@opscode.com>)
3
+ // Author:: Mark Anderson (<mark@opscode.com>)
4
+ // Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ // License:: Apache License, Version 2.0
6
+ //
7
+ // Licensed under the Apache License, Version 2.0 (the "License");
8
+ // you may not use this file except in compliance with the License.
9
+ // You may obtain a copy of the License at
10
+ //
11
+ // http://www.apache.org/licenses/LICENSE-2.0
12
+ //
13
+ // Unless required by applicable law or agreed to in writing, software
14
+ // distributed under the License is distributed on an "AS IS" BASIS,
15
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ // See the License for the specific language governing permissions and
17
+ // limitations under the License.
18
+ //
19
+
20
+ #ifndef dep_selector_to_gecode_interface_h
21
+ #define dep_selector_to_gecode_interface_h
22
+
23
+ // Conceptual Api
24
+ #ifdef __cplusplus
25
+ extern "C" {
26
+ #endif __cplusplus
27
+
28
+ #ifdef __cplusplus
29
+ class VersionProblem;
30
+ #else
31
+ typedef struct VersionProblem VersionProblem;
32
+ #endif
33
+
34
+ VersionProblem * VersionProblemCreate(int packageCount);
35
+ void VersionProblemDestroy(VersionProblem * vp);
36
+
37
+
38
+ int VersionProblemSize(VersionProblem *p);
39
+ int VersionProblemPackageCount(VersionProblem *p);
40
+
41
+ // Return ID #
42
+ int AddPackage(VersionProblem *problem, int min, int max, int currentVersion);
43
+ // Add constraint for package pkg @ version,
44
+ // that dependentPackage is at version [minDependentVersion,maxDependentVersion]
45
+ // Returns false if system becomes insoluble.
46
+ bool AddVersionConstraint(VersionProblem *problem, int packageId, int version,
47
+ int dependentPackageId, int minDependentVersion, int maxDependentVersion);
48
+
49
+ void MarkPackageSuspicious(VersionProblem *problem, int packageId);
50
+ void MarkPackageRequired(VersionProblem *problem, int packageId);
51
+ void MarkPackagePreferredToBeAtLatest(VersionProblem *problem, int packageId, int weight);
52
+
53
+ int GetPackageVersion(VersionProblem *problem, int packageId);
54
+ bool GetPackageDisabledState(VersionProblem *problem, int packageId);
55
+
56
+ int GetPackageMax(VersionProblem *problem, int packageId);
57
+ int GetPackageMin(VersionProblem *problem, int packageId);
58
+
59
+ int GetDisabledVariableCount(VersionProblem *problem);
60
+
61
+ void VersionProblemDump(VersionProblem * problem);
62
+ void VersionProblemPrintPackageVar(VersionProblem * problem, int packageId);
63
+
64
+ VersionProblem * Solve(VersionProblem * problem);
65
+
66
+ #ifdef __cplusplus
67
+ }
68
+ #endif __cplusplus
69
+
70
+ #endif // dep_selector_to_gecode_interface_h
@@ -0,0 +1,36 @@
1
+ #
2
+ # Author:: Christopher Walters (<cw@opscode.com>)
3
+ # Author:: Mark Anderson (<mark@opscode.com>)
4
+ # Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ #
21
+ # GECODE needs to be built with
22
+ # ./configure --with-architectures=i386,x86_64
23
+ # to work properly here.
24
+ require 'mkmf'
25
+
26
+ $LIBS << " -lstdc++"
27
+
28
+ #$CFLAGS << "-g"
29
+
30
+ have_library('gecodesearch')
31
+ have_library('gecodeint')
32
+ have_library('gecodekernel')
33
+ have_library('gecodesupport')
34
+ have_library('gecodeminimodel')
35
+
36
+ create_makefile('dep_gecode')
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/ruby
2
+
3
+
4
+ require "ffi"
5
+
6
+ module VersionProblem
7
+ extend FFI:Library
8
+ # ffi_lib FFI:Library:????
9
+ attach_function 'VersionProblemCreate', [ ], :pointer
10
+ attach_function 'VersionProblemDump', [ :pointer ], :void
11
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Author:: Christopher Walters (<cw@opscode.com>)
3
+ # Author:: Mark Anderson (<mark@opscode.com>)
4
+ # Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'dep_selector/selector'
21
+ require 'dep_selector/dependency_graph'
22
+ require 'dep_selector/package'
23
+ require 'dep_selector/package_version'
24
+ require 'dep_selector/dependency'
25
+ require 'dep_selector/solution_constraint'
26
+ require 'dep_selector/version'
27
+ require 'dep_selector/version_constraint'
28
+ require 'dep_selector/exceptions'
29
+
30
+ # error reporting
31
+ require 'dep_selector/error_reporter'
32
+ require 'dep_selector/error_reporter/simple_tree_traverser'
@@ -0,0 +1,59 @@
1
+ #
2
+ # Author:: Christopher Walters (<cw@opscode.com>)
3
+ # Author:: Mark Anderson (<mark@opscode.com>)
4
+ # Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'dep_selector/exceptions'
21
+
22
+ module DepSelector
23
+ class DenselyPackedSet
24
+ attr_reader :sorted_elements
25
+
26
+ def initialize(elements)
27
+ @sorted_elements = elements.sort
28
+ @element_to_index = {}
29
+ @sorted_elements.each_with_index{|elt, idx| @element_to_index[elt] = idx}
30
+ end
31
+
32
+ def range
33
+ Range.new(0, @sorted_elements.size-1)
34
+ end
35
+
36
+ def index(element)
37
+ unless @element_to_index.has_key?(element)
38
+ msg = "#{element} is not a valid version for this package"
39
+ raise Exceptions::InvalidVersion.new(msg)
40
+ end
41
+ @element_to_index[element]
42
+ end
43
+
44
+ def [](constraint)
45
+ # TODO [cw/mark,2010/11/22]: don't actually need an array here, re-write
46
+ range = []
47
+ started = false
48
+ done = false
49
+ sorted_elements.each_with_index do |element, idx|
50
+ if constraint.include?(element)
51
+ raise "Currently only handle continuous gap between #{range.last} and #{idx} for #{constraint.to_s} over #{@sorted_elements.join(', ')}" if (range.any? && range.last+1 != idx)
52
+ range << idx
53
+ end
54
+ end
55
+
56
+ range.empty? ? [] : Range.new(range.first, range.last)
57
+ end
58
+ end
59
+ end