Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions latex/solution.template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ \section*{Strategy}
{{strategy}}
\end{verbatim}

\section*{Semigroup}

\begin{verbatim}
States:
{{states}}

{{semigroup}}
\end{verbatim}

\end{document}
3 changes: 3 additions & 0 deletions src/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Solution {
pub nfa: Nfa,
pub is_controllable: bool,
pub winning_strategy: Strategy,
pub semigroup: crate::semigroup::FlowSemigroup,
}

impl Solution {
Expand Down Expand Up @@ -35,6 +36,8 @@ impl Solution {

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

context.insert("semigroup", &self.semigroup.to_string());

// Render template
let rendered = tera
.render("template", &context)
Expand Down
24 changes: 13 additions & 11 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::flow;
use crate::graph::Graph;
use crate::ideal::Ideal;
use crate::nfa;
use crate::semigroup;
use crate::semigroup::{self, FlowSemigroup};
use crate::solution::Solution;
use crate::strategy::Strategy;
use clap::ValueEnum;
Expand All @@ -26,7 +26,7 @@ pub fn solve(nfa: &nfa::Nfa, output: &SolverOutput) -> Solution {
let final_states = nfa.final_states();
let edges = nfa.get_edges();
let letters = nfa.get_alphabet();
let strategy = match output {
let (strategy, semigroup) = match output {
SolverOutput::Strategy => {
compute_maximal_winning_strategy(dim, &final_states, edges, &letters)
}
Expand All @@ -39,6 +39,7 @@ pub fn solve(nfa: &nfa::Nfa, output: &SolverOutput) -> Solution {
nfa: nfa.clone(),
is_controllable,
winning_strategy: strategy,
semigroup,
}
}

Expand All @@ -47,7 +48,7 @@ fn compute_maximal_winning_strategy(
final_states: &[usize],
edges: HashMap<String, Graph>,
letters: &[&str],
) -> Strategy {
) -> (Strategy, FlowSemigroup) {
let maximal_finite_value = dim as coef;

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

let changed = update_strategy(
let (changed, semigroup) = update_strategy(
dim,
&mut strategy,
final_states,
Expand All @@ -67,10 +68,9 @@ fn compute_maximal_winning_strategy(
);

if !changed {
break;
return (strategy, semigroup);
}
}
strategy
}

fn compute_control_problem_solution(
Expand All @@ -79,8 +79,9 @@ fn compute_control_problem_solution(
final_states: &[usize],
edges: HashMap<String, Graph>,
letters: &[&str],
) -> Strategy {
) -> (Strategy, FlowSemigroup) {
let mut strategy = Strategy::get_maximal_strategy(dim, letters);
let mut semigroup = FlowSemigroup::new();

for maximal_finite_value in 1..dim as coef {
let mut step = 1;
Expand All @@ -92,13 +93,14 @@ fn compute_control_problem_solution(
);
step += 1;

let changed = update_strategy(
let (changed, new_semigroup) = update_strategy(
dim,
&mut strategy,
final_states,
&edges,
maximal_finite_value,
);
semigroup = new_semigroup;
let result = strategy.is_defined_on(source);

if !changed || !result {
Expand All @@ -109,7 +111,7 @@ fn compute_control_problem_solution(
break;
}
}
strategy
(strategy, semigroup)
}

fn update_strategy(
Expand All @@ -118,7 +120,7 @@ fn update_strategy(
final_states: &[usize],
edges: &HashMap<String, Graph>,
maximal_finite_value: u8,
) -> bool {
) -> (bool, FlowSemigroup) {
let final_ideal = get_omega_ideal(dim, final_states);
let action_flows = compute_action_flows(strategy, edges);
debug!("\nAction flows:\n{}", flows_to_string(&action_flows));
Expand All @@ -137,7 +139,7 @@ fn update_strategy(
debug!("Restricting strategy");
let changed = strategy.restrict_to(winning_downset, edges, maximal_finite_value);
debug!("Strategy after restriction:\n{}", strategy);
changed
(changed, semigroup)
}

fn get_omega_ideal(dim: usize, states: &[usize]) -> Ideal {
Expand Down
7 changes: 4 additions & 3 deletions src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl fmt::Display for Strategy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut letters = self.0.keys().collect::<Vec<_>>();
letters.sort();
let vec: Vec<String> = letters
let strategy = letters
.iter()
.map(|a| {
let downset = self.0.get(*a).unwrap();
Expand All @@ -81,8 +81,9 @@ impl fmt::Display for Strategy {
)
}
})
.collect();
write!(f, "{}", vec.join("\n"))
.collect::<Vec<_>>()
.join("\n");
write!(f, "{}", strategy)
}
}

Expand Down
Loading