TimQu
8 years ago
2 changed files with 0 additions and 678 deletions
@ -1,424 +0,0 @@ |
|||
#include <stdint.h>
|
|||
|
|||
#include "storm/utility/policyguessing.h"
|
|||
|
|||
#include "storm/utility/macros.h"
|
|||
#include "storm/utility/solver.h"
|
|||
#include "storm/solver/LinearEquationSolver.h"
|
|||
#include "storm/solver/GmmxxLinearEquationSolver.h"
|
|||
#include "graph.h"
|
|||
#include "ConstantsComparator.h"
|
|||
|
|||
namespace storm { |
|||
namespace utility{ |
|||
namespace policyguessing { |
|||
|
|||
template <typename ValueType> |
|||
void solveGame( storm::solver::GameSolver<ValueType>& solver, |
|||
std::vector<ValueType>& x, |
|||
std::vector<ValueType> const& b, |
|||
OptimizationDirection player1Goal, |
|||
OptimizationDirection player2Goal, |
|||
storm::storage::TotalScheduler& player1Scheduler, |
|||
storm::storage::TotalScheduler& player2Scheduler, |
|||
storm::storage::BitVector const& targetChoices, |
|||
ValueType const& prob0Value |
|||
){ |
|||
|
|||
storm::storage::SparseMatrix<ValueType> inducedA; |
|||
std::vector<ValueType> inducedB; |
|||
storm::storage::BitVector probGreater0States; |
|||
getInducedEquationSystem(solver, b, player1Scheduler, player2Scheduler, targetChoices, inducedA, inducedB, probGreater0States); |
|||
|
|||
solveLinearEquationSystem(inducedA, x, inducedB, probGreater0States, prob0Value, solver.getPrecision(), solver.getRelative()); |
|||
|
|||
solver.setTrackScheduler(); |
|||
bool resultCorrect = false; |
|||
while(!resultCorrect){ |
|||
solver.solveGame(player1Goal, player2Goal, x, b); |
|||
player1Scheduler = solver.getPlayer1Scheduler(); |
|||
player2Scheduler = solver.getPlayer2Scheduler(); |
|||
|
|||
//Check if the policies makes choices that lead to states from which no target state is reachable ("prob0"-states).
|
|||
getInducedEquationSystem(solver, b, player1Scheduler, player2Scheduler, targetChoices, inducedA, inducedB, probGreater0States); |
|||
resultCorrect = checkAndFixScheduler(solver, x, b, player1Scheduler, player2Scheduler, targetChoices, inducedA, inducedB, probGreater0States); |
|||
if(!resultCorrect){ |
|||
//If the Scheduler could not be fixed, it indicates that our guessed values were to high.
|
|||
STORM_LOG_WARN("Policies could not be fixed. Restarting Gamesolver. "); |
|||
solveLinearEquationSystem(inducedA, x, inducedB, probGreater0States, prob0Value, solver.getPrecision(), solver.getRelative()); |
|||
//x = std::vector<ValueType>(x.size(), storm::utility::zero<ValueType>());
|
|||
} |
|||
} |
|||
} |
|||
|
|||
template <typename ValueType> |
|||
void solveMinMaxLinearEquationSystem( storm::solver::MinMaxLinearEquationSolver<ValueType>& solver, |
|||
storm::storage::SparseMatrix<ValueType> const& A, |
|||
std::vector<ValueType>& x, |
|||
std::vector<ValueType> const& b, |
|||
OptimizationDirection goal, |
|||
storm::storage::TotalScheduler& scheduler, |
|||
storm::storage::BitVector const& targetChoices, |
|||
ValueType const& prob0Value |
|||
){ |
|||
storm::storage::SparseMatrix<ValueType> inducedA; |
|||
std::vector<ValueType> inducedB; |
|||
storm::storage::BitVector probGreater0States; |
|||
getInducedEquationSystem(A, b, scheduler, targetChoices, inducedA, inducedB, probGreater0States); |
|||
solveLinearEquationSystem(inducedA, x, inducedB, probGreater0States, prob0Value, solver.getPrecision(), solver.getRelative()); |
|||
|
|||
solver.setTrackScheduler(); |
|||
solver.setCachingEnabled(true); |
|||
bool resultCorrect = false; |
|||
while(!resultCorrect){ |
|||
solver.solveEquations(goal, x, b); |
|||
scheduler = std::move(*solver.getScheduler()); |
|||
|
|||
//Check if the Scheduler makes choices that lead to states from which no target state is reachable ("prob0"-states).
|
|||
getInducedEquationSystem(A, b, scheduler, targetChoices, inducedA, inducedB, probGreater0States); |
|||
resultCorrect = checkAndFixScheduler(A, x, b, scheduler, targetChoices, solver.getPrecision(), solver.getRelative(), inducedA, inducedB, probGreater0States); |
|||
|
|||
if(!resultCorrect){ |
|||
//If the Scheduler could not be fixed, it indicates that our guessed values were to high.
|
|||
STORM_LOG_WARN("Scheduler could not be fixed. Restarting MinMaxsolver." ); |
|||
solveLinearEquationSystem(inducedA, x, inducedB, probGreater0States, prob0Value, solver.getPrecision(), solver.getRelative()); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
template <typename ValueType> |
|||
void getInducedEquationSystem(storm::solver::GameSolver<ValueType> const& solver, |
|||
std::vector<ValueType> const& b, |
|||
storm::storage::TotalScheduler const& player1Scheduler, |
|||
storm::storage::TotalScheduler const& player2Scheduler, |
|||
storm::storage::BitVector const& targetChoices, |
|||
storm::storage::SparseMatrix<ValueType>& inducedA, |
|||
std::vector<ValueType>& inducedB, |
|||
storm::storage::BitVector& probGreater0States |
|||
){ |
|||
uint_fast64_t numberOfPlayer1States = solver.getPlayer1Matrix().getRowGroupCount(); |
|||
|
|||
//Get the rows of the player2matrix that are selected by the policies
|
|||
//Note that rows can be selected more then once and in an arbitrary order.
|
|||
std::vector<storm::storage::sparse::state_type> selectedRows(numberOfPlayer1States); |
|||
for (uint_fast64_t pl1State = 0; pl1State < numberOfPlayer1States; ++pl1State){ |
|||
auto const& pl1Row = solver.getPlayer1Matrix().getRow(solver.getPlayer1Matrix().getRowGroupIndices()[pl1State] + player1Scheduler.getChoice(pl1State)); |
|||
STORM_LOG_ASSERT(pl1Row.getNumberOfEntries()==1, ""); |
|||
uint_fast64_t pl2State = pl1Row.begin()->getColumn(); |
|||
selectedRows[pl1State] = solver.getPlayer2Matrix().getRowGroupIndices()[pl2State] + player2Scheduler.getChoice(pl2State); |
|||
} |
|||
//Get the matrix A, vector b, and the targetStates induced by this selection
|
|||
inducedA = solver.getPlayer2Matrix().selectRowsFromRowIndexSequence(selectedRows, false); |
|||
inducedB = std::vector<ValueType>(numberOfPlayer1States); |
|||
storm::utility::vector::selectVectorValues<ValueType>(inducedB, selectedRows, b); |
|||
storm::storage::BitVector inducedTarget(numberOfPlayer1States, false); |
|||
for (uint_fast64_t pl1State = 0; pl1State < numberOfPlayer1States; ++pl1State){ |
|||
if(targetChoices.get(selectedRows[pl1State])){ |
|||
inducedTarget.set(pl1State); |
|||
} |
|||
} |
|||
//Find the states from which no target state is reachable.
|
|||
probGreater0States = storm::utility::graph::performProbGreater0(inducedA.transpose(), storm::storage::BitVector(numberOfPlayer1States, true), inducedTarget); |
|||
} |
|||
|
|||
|
|||
template <typename ValueType> |
|||
void getInducedEquationSystem(storm::storage::SparseMatrix<ValueType> const& A, |
|||
std::vector<ValueType> const& b, |
|||
storm::storage::TotalScheduler const& scheduler, |
|||
storm::storage::BitVector const& targetChoices, |
|||
storm::storage::SparseMatrix<ValueType>& inducedA, |
|||
std::vector<ValueType>& inducedB, |
|||
storm::storage::BitVector& probGreater0States |
|||
){ |
|||
uint_fast64_t numberOfStates = A.getRowGroupCount(); |
|||
|
|||
//Get the matrix A, vector b, and the targetStates induced by the Scheduler
|
|||
std::vector<storm::storage::sparse::state_type> selectedRows(numberOfStates); |
|||
for(uint_fast64_t stateIndex = 0; stateIndex < numberOfStates; ++stateIndex){ |
|||
selectedRows[stateIndex] = (scheduler.getChoice(stateIndex)); |
|||
} |
|||
inducedA = A.selectRowsFromRowGroups(selectedRows, false); |
|||
inducedB = std::vector<ValueType>(numberOfStates); |
|||
storm::utility::vector::selectVectorValues<ValueType>(inducedB, selectedRows, A.getRowGroupIndices(), b); |
|||
storm::storage::BitVector inducedTarget(numberOfStates, false); |
|||
for (uint_fast64_t state = 0; state < numberOfStates; ++state){ |
|||
if(targetChoices.get(A.getRowGroupIndices()[state] + scheduler.getChoice(state))){ |
|||
inducedTarget.set(state); |
|||
} |
|||
} |
|||
//Find the states from which no target state is reachable.
|
|||
|