Skip to content

Commit 5c99217

Browse files
committed
added semigroup in tex output
1 parent eb5d268 commit 5c99217

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

latex/solution.template.tex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ \section*{Strategy}
4747
{{strategy}}
4848
\end{verbatim}
4949

50+
\section*{Semigroup}
51+
52+
\begin{verbatim}
53+
States:
54+
{{states}}
55+
56+
{{semigroup}}
57+
\end{verbatim}
58+
5059
\end{document}

src/solution.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Solution {
88
pub nfa: Nfa,
99
pub is_controllable: bool,
1010
pub winning_strategy: Strategy,
11+
pub semigroup: crate::semigroup::FlowSemigroup,
1112
}
1213

1314
impl Solution {
@@ -35,6 +36,8 @@ impl Solution {
3536

3637
context.insert("strategy", &self.winning_strategy.to_string());
3738

39+
context.insert("semigroup", &self.semigroup.to_string());
40+
3841
// Render template
3942
let rendered = tera
4043
.render("template", &context)

src/solver.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::flow;
33
use crate::graph::Graph;
44
use crate::ideal::Ideal;
55
use crate::nfa;
6-
use crate::semigroup;
6+
use crate::semigroup::{self, FlowSemigroup};
77
use crate::solution::Solution;
88
use crate::strategy::Strategy;
99
use clap::ValueEnum;
@@ -26,7 +26,7 @@ pub fn solve(nfa: &nfa::Nfa, output: &SolverOutput) -> Solution {
2626
let final_states = nfa.final_states();
2727
let edges = nfa.get_edges();
2828
let letters = nfa.get_alphabet();
29-
let strategy = match output {
29+
let (strategy, semigroup) = match output {
3030
SolverOutput::Strategy => {
3131
compute_maximal_winning_strategy(dim, &final_states, edges, &letters)
3232
}
@@ -39,6 +39,7 @@ pub fn solve(nfa: &nfa::Nfa, output: &SolverOutput) -> Solution {
3939
nfa: nfa.clone(),
4040
is_controllable,
4141
winning_strategy: strategy,
42+
semigroup,
4243
}
4344
}
4445

@@ -47,7 +48,7 @@ fn compute_maximal_winning_strategy(
4748
final_states: &[usize],
4849
edges: HashMap<String, Graph>,
4950
letters: &[&str],
50-
) -> Strategy {
51+
) -> (Strategy, FlowSemigroup) {
5152
let maximal_finite_value = dim as coef;
5253

5354
let mut strategy = Strategy::get_maximal_strategy(dim, letters);
@@ -58,7 +59,7 @@ fn compute_maximal_winning_strategy(
5859
info!("Computing the maximal winning strategy step {}", step);
5960
step += 1;
6061

61-
let changed = update_strategy(
62+
let (changed, semigroup) = update_strategy(
6263
dim,
6364
&mut strategy,
6465
final_states,
@@ -67,10 +68,9 @@ fn compute_maximal_winning_strategy(
6768
);
6869

6970
if !changed {
70-
break;
71+
return (strategy, semigroup);
7172
}
7273
}
73-
strategy
7474
}
7575

7676
fn compute_control_problem_solution(
@@ -79,8 +79,9 @@ fn compute_control_problem_solution(
7979
final_states: &[usize],
8080
edges: HashMap<String, Graph>,
8181
letters: &[&str],
82-
) -> Strategy {
82+
) -> (Strategy, FlowSemigroup) {
8383
let mut strategy = Strategy::get_maximal_strategy(dim, letters);
84+
let mut semigroup = FlowSemigroup::new();
8485

8586
for maximal_finite_value in 1..dim as coef {
8687
let mut step = 1;
@@ -92,13 +93,14 @@ fn compute_control_problem_solution(
9293
);
9394
step += 1;
9495

95-
let changed = update_strategy(
96+
let (changed, new_semigroup) = update_strategy(
9697
dim,
9798
&mut strategy,
9899
final_states,
99100
&edges,
100101
maximal_finite_value,
101102
);
103+
semigroup = new_semigroup;
102104
let result = strategy.is_defined_on(source);
103105

104106
if !changed || !result {
@@ -109,7 +111,7 @@ fn compute_control_problem_solution(
109111
break;
110112
}
111113
}
112-
strategy
114+
(strategy, semigroup)
113115
}
114116

115117
fn update_strategy(
@@ -118,7 +120,7 @@ fn update_strategy(
118120
final_states: &[usize],
119121
edges: &HashMap<String, Graph>,
120122
maximal_finite_value: u8,
121-
) -> bool {
123+
) -> (bool, FlowSemigroup) {
122124
let final_ideal = get_omega_ideal(dim, final_states);
123125
let action_flows = compute_action_flows(strategy, edges);
124126
debug!("\nAction flows:\n{}", flows_to_string(&action_flows));
@@ -137,7 +139,7 @@ fn update_strategy(
137139
debug!("Restricting strategy");
138140
let changed = strategy.restrict_to(winning_downset, edges, maximal_finite_value);
139141
debug!("Strategy after restriction:\n{}", strategy);
140-
changed
142+
(changed, semigroup)
141143
}
142144

143145
fn get_omega_ideal(dim: usize, states: &[usize]) -> Ideal {

src/strategy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl fmt::Display for Strategy {
6868
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6969
let mut letters = self.0.keys().collect::<Vec<_>>();
7070
letters.sort();
71-
let vec: Vec<String> = letters
71+
let strategy = letters
7272
.iter()
7373
.map(|a| {
7474
let downset = self.0.get(*a).unwrap();
@@ -81,8 +81,9 @@ impl fmt::Display for Strategy {
8181
)
8282
}
8383
})
84-
.collect();
85-
write!(f, "{}", vec.join("\n"))
84+
.collect::<Vec<_>>()
85+
.join("\n");
86+
write!(f, "{}", strategy)
8687
}
8788
}
8889

0 commit comments

Comments
 (0)