Skip to content

Commit

Permalink
Add decision_variable_dependency to represent polynomial dependency…
Browse files Browse the repository at this point in the history
… between decision variables (#257)

Ready for binary encoding of integer variables.

- When an integer variable $x \in [0, 3]$ is binary encoded as $x = b_1
+ 2 b_2$ with $b_1, b_2 \in \{0, 1\}$, $x$ will disappear from objective
function and constraints.
- Thus, $x$ is not passed to solver which only solve with $b_1$ and
$b_2$. So we have to reconstruct $x$ from $b_1$ and $b_2$, but how?
- `decision_variable_dependency` field in `ommx.v1.Instance` stores this
relation, i.e. $x = b_1 + 2 b_2$ as a set of the ID of $x$ and a
polynomial $b_1 + 2 b_2$.
  • Loading branch information
termoshtt authored Dec 25, 2024
1 parent 4882922 commit 2754571
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 8 deletions.
3 changes: 3 additions & 0 deletions proto/ommx/v1/instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ message Instance {

// Constraints removed via preprocessing. These are restored when evaluated into `ommx.v1.Solution`.
repeated RemovedConstraint removed_constraints = 8;

// When a decision variable is dependent on another decision variable as polynomial, this map contains the ID of the dependent decision variable as key and the polynomial as value.
map<uint64, Function> decision_variable_dependency = 9;
}
3 changes: 3 additions & 0 deletions proto/ommx/v1/parametric_instance.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ message ParametricInstance {

// Constraints removed via preprocessing. These are restored when evaluated into `ommx.v1.Solution`.
repeated RemovedConstraint removed_constraints = 8;

// When a decision variable is dependent on another decision variable as polynomial, this map contains the ID of the dependent decision variable as key and the polynomial as value.
map<uint64, Function> decision_variable_dependency = 9;
}
16 changes: 10 additions & 6 deletions python/ommx/ommx/v1/instance_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions python/ommx/ommx/v1/instance_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions python/ommx/ommx/v1/parametric_instance_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions python/ommx/ommx/v1/parametric_instance_pb2.pyi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/ommx/src/convert/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl Instance {
parameters,
constraint_hints: self.constraint_hints,
removed_constraints,
decision_variable_dependency: self.decision_variable_dependency,
})
}

Expand Down Expand Up @@ -204,6 +205,7 @@ impl Instance {
parameters: vec![parameter],
constraint_hints: self.constraint_hints,
removed_constraints,
decision_variable_dependency: self.decision_variable_dependency,
})
}

Expand Down
3 changes: 3 additions & 0 deletions rust/ommx/src/convert/parametric_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl From<Instance> for ParametricInstance {
constraint_hints,
removed_constraints,
parameters: _, // Drop previous parameters
decision_variable_dependency,
}: Instance,
) -> Self {
Self {
Expand All @@ -36,6 +37,7 @@ impl From<Instance> for ParametricInstance {
parameters: Default::default(),
constraint_hints,
removed_constraints,
decision_variable_dependency,
}
}
}
Expand Down Expand Up @@ -86,6 +88,7 @@ impl ParametricInstance {
parameters: Some(parameters),
constraint_hints: self.constraint_hints,
removed_constraints: self.removed_constraints,
decision_variable_dependency: self.decision_variable_dependency,
})
}

Expand Down
8 changes: 8 additions & 0 deletions rust/ommx/src/convert/sample_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ impl Samples {
})
}

pub fn states_mut(&mut self) -> impl Iterator<Item = Result<&mut State>> {
self.entries.iter_mut().map(|v| {
v.state
.as_mut()
.context("ommx.v1.Samples.Entry must has state. Broken Data.")
})
}

/// Transpose `sample_id -> decision_variable_id -> value` to `decision_variable_id -> sample_id -> value`
pub fn transpose(&self) -> HashMap<u64, SampledValues> {
let mut map: HashMap<u64, HashMap<OrderedFloat<f64>, Vec<u64>>> = HashMap::new();
Expand Down
Loading

0 comments on commit 2754571

Please sign in to comment.