File: dep_selector_to_gecode.h

package info (click to toggle)
ruby-dep-selector 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 804 kB
  • ctags: 363
  • sloc: ruby: 2,970; cpp: 1,040; makefile: 5
file content (165 lines) | stat: -rw-r--r-- 4,813 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Author:: Christopher Walters (<cw@opscode.com>)
// Author:: Mark Anderson (<mark@opscode.com>)
// Copyright:: Copyright (c) 2010-2011 Opscode, Inc.
// License:: Apache License, Version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef dep_selector_to_gecode_h
#define dep_selector_to_gecode_h

#include "dep_selector_to_gecode_interface.h"
#include <iostream>	       
#include <vector>
#include <set>

#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>

using namespace Gecode;

// TODO:
// Allow retrieval of multiple solutions
// Understand how assign versions where necessary, and not assign unnecessary versions.
// Understand how to assign empty versions
//

// Extend:
// Add optimization functions
// Allow non-contiguous ranges in package dependencies. 

// TODO: Add locking 
struct VersionProblemPool 
{
    std::set<VersionProblem *> elems;
    VersionProblemPool();
    ~VersionProblemPool();
    void Add(VersionProblem * vp);
    void Delete(VersionProblem *vp);
    void ShowAll();
    void DeleteAll();
};

#define DEBUG_PREFIX_LENGTH 40

class VersionProblem : public Space
{
public:
  static const int UNRESOLVED_VARIABLE;
  static const int MIN_TRUST_LEVEL;  
  static const int MAX_TRUST_LEVEL;
  static const int MAX_PREFERRED_WEIGHT;

  static int instance_counter;

    VersionProblem(int packageCount, bool dumpStats = true, 
                   bool debug = false, 
                   const char * logId = 0);
  // Clone constructor; check gecode rules for this...
  VersionProblem(bool share, VersionProblem & s);
  virtual ~VersionProblem();

  int Size();
  int PackageCount();

  IntVar * GetPackageVersionVar(int packageId);

  virtual int AddPackage(int minVersion, int maxVersion, int currentVersion);

  virtual void AddVersionConstraint(int packageId, int version,
			    int dependentPackageId, int minDependentVersion, int maxDependentVersion);

  // We may wish to indicate that some packages have suspicious constraints, and when chosing packages to disable we 
  // would disable them first. 
  void MarkPackageSuspicious(int packageId);

  void MarkPackageRequired(int packageId); 

  // Packages marked by this method are preferentially chosen at
  // latest according to weights
  void MarkPackagePreferredToBeAtLatest(int packageId, int weight);

  void Finalize();
  
  virtual void constrain(const Space & _best_known_solution);

  int GetPackageVersion(int packageId);
  bool GetPackageDisabledState(int packageId);
  int GetMax(int packageId);
  int GetMin(int packageId);
  int GetDisabledVariableCount();
  
  // Support for gecode
  virtual Space* copy(bool share);

  // Debug and utility functions
  void Print(std::ostream &out);
  void PrintPackageVar(std::ostream & out, int packageId) ;
  const char * DebugPrefix() const { return debugPrefix; }

  static VersionProblem *InnerSolve(VersionProblem * problem, int & itercount);
  static VersionProblem *Solve(VersionProblem *problem);

 protected:
  int instance_id;
  int size;
  int version_constraint_count;
  int cur_package;
  bool dump_stats;
  bool debugLogging;
  char debugPrefix[DEBUG_PREFIX_LENGTH];
  char outputBuffer[1024];
  bool finalized;

  BoolVarArgs version_flags;
  IntVarArray package_versions;
  BoolVarArray disabled_package_variables;
  IntVar total_disabled;

  IntVar total_required_disabled;
  IntVar total_induced_disabled;
  IntVar total_suspicious_disabled;

  BoolVarArray at_latest;
  IntVar total_preferred_at_latest;
  IntVar total_not_preferred_at_latest;

  int * preferred_at_latest_weights;
  int * is_required;
  int * is_suspicious;

  VersionProblemPool *pool;

  bool CheckPackageId(int id);
  void AddPackagesPreferredToBeAtLatestObjectiveFunction(const VersionProblem & best_known_solution);
  void ConstrainVectorLessThanBest(IntVarArgs & current, IntVarArgs & best);
  void BuildCostVector(IntVarArgs & costVector) const;
 
  friend class VersionProblemPool;
};

template<class T> void PrintVarAligned(const char * message, T & var);
template<class S, class T> void PrintVarAligned(const char * message, S & var1, T & var2);

class Solver {
 public:
  Solver(VersionProblem *s);
  VersionProblem GetNextSolution();
 private:
  RBS<DFS,VersionProblem> solver;
};

#endif // dep_selector_to_gecode_h