|
From: <got...@us...> - 2010-02-13 20:48:32
|
Revision: 589
http://scstudio.svn.sourceforge.net/scstudio/?rev=589&view=rev
Author: gotthardp
Date: 2010-02-13 20:48:22 +0000 (Sat, 13 Feb 2010)
Log Message:
-----------
New feature: Initial implementation of MonteCarlo simulator and related enhancements. Limited functionality: only BMSC and only in text mode.
Modified Paths:
--------------
trunk/CMakeLists.txt
trunk/src/CMakeLists.txt
trunk/src/data/Z120/Context.cpp
trunk/src/data/Z120/Z120.g
trunk/src/data/time.h
trunk/tests/CMakeLists.txt
Added Paths:
-----------
trunk/src/montecarlo/
trunk/src/montecarlo/CMakeLists.txt
trunk/src/montecarlo/export.h
trunk/src/montecarlo/montecarlo.cpp
trunk/src/montecarlo/montecarlo.h
trunk/tests/montecarlo/
trunk/tests/montecarlo/CMakeLists.txt
trunk/tests/montecarlo/montecarlo_test.cpp
trunk/tests/montecarlo/test_cycle.mpr
trunk/tests/montecarlo/test_simple.mpr
Property Changed:
----------------
trunk/src/check/
trunk/src/data/
trunk/src/membership/
trunk/tests/acyclic/
trunk/tests/deadlock/
trunk/tests/fifo/
trunk/tests/local_choice/
trunk/tests/membership/
trunk/tests/time/
trunk/tests/universal_boundedness/
trunk/tests/z120_test/
Modified: trunk/CMakeLists.txt
===================================================================
--- trunk/CMakeLists.txt 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/CMakeLists.txt 2010-02-13 20:48:22 UTC (rev 589)
@@ -38,7 +38,9 @@
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS")
ENDIF(MSVC)
-FIND_PACKAGE(Boost REQUIRED)
+SET(Boost_USE_STATIC_LIBS ON)
+SET(Boost_USE_MULTITHREADED ON)
+FIND_PACKAGE(Boost COMPONENTS date_time thread REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
FIND_PACKAGE(Java)
Modified: trunk/src/CMakeLists.txt
===================================================================
--- trunk/src/CMakeLists.txt 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/src/CMakeLists.txt 2010-02-13 20:48:22 UTC (rev 589)
@@ -1,5 +1,6 @@
ADD_SUBDIRECTORY(data)
ADD_SUBDIRECTORY(check)
ADD_SUBDIRECTORY(membership)
+ADD_SUBDIRECTORY(montecarlo)
# $Id$
Property changes on: trunk/src/check
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/src/data
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
.time.h.swp
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Modified: trunk/src/data/Z120/Context.cpp
===================================================================
--- trunk/src/data/Z120/Context.cpp 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/src/data/Z120/Context.cpp 2010-02-13 20:48:22 UTC (rev 589)
@@ -909,7 +909,7 @@
TimeRelationEventPtr relation;
try
{
- relation = new TimeRelationEvent(MscTimeIntervalSet<double>(context->time));
+ relation = new TimeRelationEvent(context->time);
relation->set_directed(context->origin);
context->origin = false;
}
Modified: trunk/src/data/Z120/Z120.g
===================================================================
--- trunk/src/data/Z120/Z120.g 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/src/data/Z120/Z120.g 2010-02-13 20:48:22 UTC (rev 589)
@@ -1103,7 +1103,7 @@
( (('@')? ('(' | '[') (time_point)? ',')=> bounded_time
{
add_time_fun(context, (char*) $bounded_time.text->chars);
- } (measurement)?
+ }
| singular_time
{
add_time_fun(context, (char*) $singular_time.text->chars);
@@ -1139,6 +1139,7 @@
('@')? ('(' | '[')
(time_point)? ',' (time_point)?
(')' | ']')
+ (measurement)?
;
Modified: trunk/src/data/time.h
===================================================================
--- trunk/src/data/time.h 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/src/data/time.h 2010-02-13 20:48:22 UTC (rev 589)
@@ -19,6 +19,7 @@
#ifndef _TIME_H_
#define _TIME_H_
+#include "data/fcmp.h"
#include "data/export.h"
#include<limits.h>
@@ -221,41 +222,14 @@
const DecScaled operator+(const DecScaled& right) const
{
- int division = abs(m_exp-right.m_exp);
- DecScaled a = *this;
- DecScaled b = right;
-
- if(a.m_exp>b.m_exp)
- {
- a.m_mant*=ipow(10,division);
- a.m_exp-=division;
- }
- else
- {
- b.m_mant*=ipow(10,division);
- b.m_exp-=division;
- }
- return DecScaled(a.m_mant+b.m_mant,a.m_exp);
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return DecScaled(norm.first.m_mant+norm.second.m_mant, norm.first.m_exp);
}
const DecScaled operator-(const DecScaled& right) const
{
- long division = abs(m_exp-right.m_exp);
- DecScaled a = *this;
- DecScaled b = right;
-
- if(a.m_exp>b.m_exp)
- {
- a.m_mant*=ipow(10,division);
- a.m_exp-=division;
- }
- else
- {
- b.m_mant*=ipow(10,division);
- b.m_exp-=division;
- }
-
- return DecScaled(a.m_mant-b.m_mant,a.m_exp);
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return DecScaled(norm.first.m_mant-norm.second.m_mant, norm.first.m_exp);
}
const DecScaled operator*(const DecScaled& right) const
@@ -278,30 +252,46 @@
return m_mant!=right.m_mant || m_exp!=right.m_exp;
}
+ bool operator<(const DecScaled& right) const
+ {
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return norm.first.m_mant < norm.second.m_mant;
+ }
+
+ bool operator<=(const DecScaled& right) const
+ {
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return norm.first.m_mant <= norm.second.m_mant;
+ }
+
bool operator>(const DecScaled& right) const
{
- long division = abs(m_exp-right.m_exp);
- DecScaled a = *this;
- DecScaled b = right;
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return norm.first.m_mant > norm.second.m_mant;
+ }
- if(a.m_exp>b.m_exp)
- {
- a.m_mant*=ipow(10,division);
- a.m_exp-=division;
- }
- else
- {
- b.m_mant*=ipow(10,division);
- b.m_exp-=division;
- }
+ bool operator>=(const DecScaled& right) const
+ {
+ std::pair<DecScaled, DecScaled> norm = normalize(*this, right);
+ return norm.first.m_mant >= norm.second.m_mant;
+ }
- return a.m_mant>b.m_mant;
+ /**
+ * Print the number in a scientific notation
+ * http://en.wikipedia.org/wiki/Scientific_notation
+ */
+ friend std::ostream&
+ operator<<(std::ostream& os, const DecScaled& value)
+ {
+ return os << value.m_mant << "E" << value.m_exp;
}
- bool operator<(const DecScaled& right) const
+protected:
+ const std::pair<DecScaled, DecScaled>
+ normalize(const DecScaled &left, const DecScaled &right) const
{
- long division = abs(m_exp-right.m_exp);
- DecScaled a = *this;
+ long division = abs(left.m_exp-right.m_exp);
+ DecScaled a = left;
DecScaled b = right;
if(a.m_exp>b.m_exp)
@@ -315,18 +305,8 @@
b.m_exp-=division;
}
- return a.m_mant<b.m_mant;
+ return std::pair<DecScaled, DecScaled>(a, b);
}
-
- /**
- * Print the number in a scientific notation
- * http://en.wikipedia.org/wiki/Scientific_notation
- */
- friend std::ostream&
- operator<<(std::ostream& os, const DecScaled& value)
- {
- return os << value.m_mant << "E" << value.m_exp;
- }
}; // end of DecScaled class
@@ -443,6 +423,24 @@
return *this;
}
+ bool operator==(const T& right) const
+ { return m_closed && m_value == right; }
+
+ bool operator!=(const T& right) const
+ { return !m_closed || m_value != right; }
+
+ bool operator<(const T& right) const
+ { return m_value < right; }
+
+ bool operator<=(const T& right) const
+ { return (m_closed && m_value <= right) || m_value < right; }
+
+ bool operator>(const T& right) const
+ { return m_value > right; }
+
+ bool operator>=(const T& right) const
+ { return (m_closed && m_value >= right) || m_value > right; }
+
/**
* @warning: does not bother with left and right couple
*/
@@ -490,6 +488,31 @@
throw MscIntervalCoupleUncomparable();
}
};
+
+template<>
+bool MscIntervalCouple<double>::operator==(const double& right) const
+{ return m_closed && fcmp(m_value, right) == 0; }
+
+template<>
+bool MscIntervalCouple<double>::operator!=(const double& right) const
+{ return !m_closed || fcmp(m_value, right) != 0; }
+
+template<>
+bool MscIntervalCouple<double>::operator<(const double& right) const
+{ return fcmp(m_value, right) < 0; }
+
+template<>
+bool MscIntervalCouple<double>::operator<=(const double& right) const
+{ return (m_closed && fcmp(m_value, right) <= 0) || fcmp(m_value, right) < 0; }
+
+template<>
+bool MscIntervalCouple<double>::operator>(const double& right) const
+{ return fcmp(m_value, right) > 0; }
+
+template<>
+bool MscIntervalCouple<double>::operator>=(const double& right) const
+{ return (m_closed && fcmp(m_value, right) >= 0) || fcmp(m_value, right) > 0; }
+
// end of MscIntervalCouple class
template<class P>
@@ -751,6 +774,13 @@
return !((m_begin==right.m_begin)&&(m_end==right.m_end));
}
+ bool includes(const T& value) const
+ {
+ // open and closed intervals are handled by the comparison operators
+ // see MscIntervalCouple<>::operator <=
+ return m_begin <= value && m_end >= value;
+ }
+
/**
* \brief tests if the interval is valid
* Tests beginning and ending value
@@ -1218,6 +1248,19 @@
return set.m_set!=this->m_set;
}
+ bool includes(const T& value) const
+ {
+ for(IntervalList::const_iterator it = m_set.begin();
+ it != m_set.end(); it++)
+ {
+ // if included in at least one of the intervals in the union
+ if(it->includes(value))
+ return true;
+ }
+
+ return false;
+ }
+
void clear(){
m_set.clear();
}
Property changes on: trunk/src/membership
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/src/montecarlo
___________________________________________________________________
Added: svn:ignore
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Added: trunk/src/montecarlo/CMakeLists.txt
===================================================================
--- trunk/src/montecarlo/CMakeLists.txt (rev 0)
+++ trunk/src/montecarlo/CMakeLists.txt 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,18 @@
+ADD_LIBRARY(scmontecarlo SHARED
+ export.h
+# module.cpp
+ montecarlo.cpp
+ montecarlo.h
+)
+
+TARGET_LINK_LIBRARIES(scmontecarlo
+ scmsc
+ ${Boost_LIBRARIES}
+)
+
+INSTALL(TARGETS scmontecarlo
+ RUNTIME DESTINATION bin
+ LIBRARY DESTINATION lib
+ ARCHIVE DESTINATION lib)
+
+# $Id$
Property changes on: trunk/src/montecarlo/CMakeLists.txt
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/src/montecarlo/export.h
===================================================================
--- trunk/src/montecarlo/export.h (rev 0)
+++ trunk/src/montecarlo/export.h 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,37 @@
+/*
+ * scstudio - Sequence Chart Studio
+ * http://scstudio.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * Copyright (c) 2008 Petr Gotthard <pet...@ce...>
+ *
+ * $Id$
+ */
+
+#ifndef _SCMONTECARLO_EXPORT_H
+#define _SCMONTECARLO_EXPORT_H
+
+#if defined(_MSC_VER)
+#pragma warning(disable: 4251)
+
+#if defined(scmontecarlo_EXPORTS)
+#define SCMONTECARLO_EXPORT __declspec(dllexport)
+#else
+#define SCMONTECARLO_EXPORT __declspec(dllimport)
+#endif
+
+#else
+#define SCMONTECARLO_EXPORT
+#endif
+
+#endif /* _SCMONTECARLO_EXPORT_H */
+
+// $Id$
Property changes on: trunk/src/montecarlo/export.h
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/src/montecarlo/montecarlo.cpp
===================================================================
--- trunk/src/montecarlo/montecarlo.cpp (rev 0)
+++ trunk/src/montecarlo/montecarlo.cpp 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,269 @@
+/*
+ * scstudio - Sequence Chart Studio
+ * http://scstudio.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * Copyright (c) 2010 Petr Gotthard <pet...@ce...>
+ *
+ * $Id$
+ */
+
+#include "montecarlo.h"
+
+double MonteCarlo::Dealer::traverse(double time_begin, const MscPtr& msc)
+{
+ BMscPtr bmsc = boost::dynamic_pointer_cast<BMsc>(msc);
+ if(bmsc)
+ return traverse(time_begin, bmsc);
+
+ return time_begin;
+}
+
+double MonteCarlo::Dealer::traverse(double time_begin, const BMscPtr& bmsc)
+{
+ double time_end = time_begin;
+
+ for(InstancePtrList::const_iterator instance = bmsc->get_instances().begin();
+ instance != bmsc->get_instances().end(); instance++)
+ {
+ // get instance duration
+ double itime = traverse_area(time_begin, (*instance)->get_first().get());
+ // calculate maximal duration of all instances
+ time_end = std::max(time_end, itime);
+ }
+
+ return time_end;
+}
+
+double MonteCarlo::Dealer::traverse_area(double time_begin, EventArea* area)
+{
+ if(!area)
+ return time_begin;
+
+ StrictOrderArea* strict = dynamic_cast<StrictOrderArea*>(area);
+ if(strict)
+ {
+ if(strict->is_empty())
+ // get area duration
+ return traverse_area(time_begin, strict->get_next().get());
+ else
+ // get time of the last event
+ return traverse_event(time_begin, strict->get_first().get());
+ }
+
+ CoregionArea* coregion = dynamic_cast<CoregionArea*>(area);
+ if(coregion)
+ {
+ if(coregion->is_empty())
+ // get area duration
+ return traverse_area(time_begin, coregion->get_next().get());
+ else
+ {
+ double time_end = time_begin;
+
+ for(CoregionEventPVector::const_iterator min = coregion->get_minimal_events().begin();
+ min != coregion->get_minimal_events().end(); min++)
+ {
+ // get time of the last event
+ double etime = traverse_event(time_begin, *min);
+ // find the latest event in this coregion
+ time_end = std::max(time_end, etime);
+ }
+
+ return time_end;
+ }
+ }
+
+ // this point should never be reached
+ return time_begin;
+}
+
+double MonteCarlo::Dealer::traverse_event(double time_begin, Event* event)
+{
+ double event_time = time_begin;
+
+ EventTimeMap::iterator itime = m_timestamps.find(event);
+ if(itime != m_timestamps.end())
+ {
+ if(m_completed_events.find(event) == m_completed_events.end())
+ throw std::runtime_error("Cycle detected");
+
+ event_time = std::max(time_begin, itime->second);
+ }
+
+ // store time of this event
+ m_timestamps[event] = event_time;
+
+ double time_end = event_time;
+
+ StrictEvent* strict = dynamic_cast<StrictEvent*>(event);
+ if(strict)
+ // find the latest event
+ time_end = traverse_strict_event(event_time, strict);
+
+ CoregionEvent* coregion = dynamic_cast<CoregionEvent*>(event);
+ if(coregion)
+ // find the latest event in this coregion
+ time_end = traverse_coregion_event(event_time, coregion);
+
+ m_completed_events.insert(event);
+ return time_end;
+}
+
+double MonteCarlo::Dealer::traverse_strict_event(double time_begin, StrictEvent* event)
+{
+ // store matching event time in m_timestamps
+ traverse_matching_event(time_begin, event);
+
+ if(event->get_successor().get())
+ // get time of the last event
+ return traverse_event(time_begin, event->get_successor().get());
+ else
+ // get area duration
+ return traverse_area(time_begin, event->get_area()->get_next().get());
+}
+
+double MonteCarlo::Dealer::traverse_coregion_event(double time_begin, CoregionEvent* event)
+{
+ // store matching event time in m_timestamps
+ traverse_matching_event(time_begin, event);
+
+ if(event->get_successors().size()!=0)
+ {
+ double time_end = time_begin;
+
+ const CoregEventRelPtrVector& successors = event->get_successors();
+ CoregEventRelPtrVector::const_iterator successor;
+ for(successor=successors.begin(); successor!=successors.end(); successor++)
+ {
+ // get time of the last event
+ double etime = traverse_event(time_begin, (*successor)->get_successor());
+ // find the latest event in this coregion
+ time_end = std::max(time_end, etime);
+ }
+
+ return time_end;
+ }
+ else
+ // get area duration
+ return traverse_area(time_begin, event->get_area()->get_next().get());
+}
+
+void MonteCarlo::Dealer::traverse_matching_event(double time_begin, Event* event)
+{
+ if(event->is_send() && event->get_matching_event())
+ {
+ // generate delay for this message transmission
+ double delay = m_rng();
+
+ traverse_event(time_begin+delay, event->get_matching_event());
+ // event time is stored in m_timestamps
+ }
+}
+
+MonteCarlo::MonteCarlo()
+{
+ m_process_count = 1;
+}
+
+int MonteCarlo::do_sample(boost::mt19937& seeder, const MscPtr& msc)
+{
+ Dealer dealer(seeder);
+
+ // (1) generate random timestamps
+ dealer.traverse(0, msc.get());
+
+ // (2) verify constraints and extract measurements
+ MeasurementsMap measurements;
+
+ const Dealer::EventTimeMap& timestamps = dealer.get_timestamps();
+ // walk through all timestamps
+ for(Dealer::EventTimeMap::const_iterator tpos = timestamps.begin();
+ tpos != timestamps.end(); tpos++)
+ {
+ const TimeRelationEventPtrList& relations = tpos->first->get_time_relations();
+ for(TimeRelationEventPtrList::const_iterator rpos = relations.begin();
+ rpos != relations.end(); rpos++)
+ {
+ if((*rpos)->get_event_a() != tpos->first)
+ continue;
+
+ Dealer::EventTimeMap::const_iterator tpos2 = timestamps.find((*rpos)->get_event_b());
+ assert(tpos2 != timestamps.end());
+
+ double duration = fabs(tpos->second - tpos2->second);
+
+ // directed constraint applies only to events
+ // when they dynamically occur in the direction indicated
+ if(!(*rpos)->is_directed() ||
+ /* (*rpos)->is_directed() && */ tpos->second < tpos2->second)
+ {
+ // if the constraint is violated, reject this trace
+ if(!(*rpos)->get_interval_set().includes(duration))
+ return 1; // rejected
+ }
+
+ if(!(*rpos)->get_measurement().empty())
+ measurements[(*rpos)->get_measurement()] = duration;
+ }
+ }
+
+ // the trace is accepted
+ // copy calculated measurements to the result set
+ boost::mutex::scoped_lock l(m_mutex);
+
+ m_measurements = measurements;
+ m_stoprequested = true;
+
+ return 0; // accepted
+}
+
+const MonteCarlo::MeasurementsMap& MonteCarlo::run(const MscPtr& msc)
+{
+ std::vector<boost::shared_ptr<boost::thread> > threads;
+ m_stoprequested = false;
+
+ boost::variate_generator<boost::mt19937&, boost::uniform_int<> > seed(
+ boost::mt19937((boost::int32_t)time(NULL)),
+ boost::uniform_int<>(0, MAXINT));
+
+ if(m_process_count > 1)
+ {
+ threads.resize(m_process_count-1);
+ // execute additional threads
+ for(int i = 0; i < m_process_count-1; i++)
+ threads[i].reset(new boost::thread(boost::bind(&MonteCarlo::thread_main, this, seed(), msc)));
+ }
+
+ // do also simulation execution
+ thread_main(seed(), msc);
+
+ if(m_process_count > 1)
+ {
+ // wait for the executed threads
+ for(int i = 0; i < m_process_count-1; i++)
+ threads[i]->join();
+ }
+
+ return m_measurements;
+}
+
+void MonteCarlo::thread_main(boost::int32_t seed, const MscPtr& msc)
+{
+ // pseudo-random number generators should not be initialized frequently
+ // to assure reentrance, we have one generator per thread
+ boost::mt19937 seeder(seed);
+
+ while(!m_stoprequested)
+ do_sample(seeder, msc);
+}
+
+// $Id$
Property changes on: trunk/src/montecarlo/montecarlo.cpp
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/src/montecarlo/montecarlo.h
===================================================================
--- trunk/src/montecarlo/montecarlo.h (rev 0)
+++ trunk/src/montecarlo/montecarlo.h 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,79 @@
+/*
+ * scstudio - Sequence Chart Studio
+ * http://scstudio.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * Copyright (c) 2010 Petr Gotthard <pet...@ce...>
+ *
+ * $Id$
+ */
+
+#include <boost/random.hpp>
+#include <boost/thread.hpp>
+
+#include "data/msc.h"
+#include "montecarlo/export.h"
+
+class SCMONTECARLO_EXPORT MonteCarlo
+{
+public:
+
+ class Dealer
+ {
+ public:
+ Dealer(boost::mt19937& seeder) :
+ m_rng(seeder, boost::uniform_real<>(0,10))
+ { }
+
+ double traverse(double time_begin, const MscPtr& msc);
+ double traverse(double time_begin, const BMscPtr& bmsc);
+
+ typedef std::map<EventPtr, double> EventTimeMap;
+ const EventTimeMap& get_timestamps() const
+ { return m_timestamps; }
+
+ private:
+ boost::variate_generator<boost::mt19937&, boost::uniform_real<> > m_rng;
+
+ double traverse_area(double time_begin, EventArea* area);
+ double traverse_event(double time_begin, Event* event);
+ double traverse_strict_event(double time_begin, StrictEvent* event);
+ double traverse_coregion_event(double time_begin, CoregionEvent* event);
+ void traverse_matching_event(double time_begin, Event* event);
+
+ EventTimeMap m_timestamps;
+
+ typedef std::set<EventPtr> EventPtrSet;
+ EventPtrSet m_completed_events;
+ };
+
+ MonteCarlo();
+
+ void set_process_count(int i)
+ { m_process_count = i; }
+
+ int do_sample(boost::mt19937& seeder, const MscPtr& msc);
+
+ typedef std::map<std::string, double> MeasurementsMap;
+ const MeasurementsMap& run(const MscPtr& msc);
+
+ void thread_main(boost::int32_t seed, const MscPtr& msc);
+
+private:
+ int m_process_count;
+
+ volatile bool m_stoprequested;
+ boost::mutex m_mutex;
+
+ MeasurementsMap m_measurements;
+};
+
+// $Id$
Property changes on: trunk/src/montecarlo/montecarlo.h
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Modified: trunk/tests/CMakeLists.txt
===================================================================
--- trunk/tests/CMakeLists.txt 2010-02-12 08:32:16 UTC (rev 588)
+++ trunk/tests/CMakeLists.txt 2010-02-13 20:48:22 UTC (rev 589)
@@ -85,6 +85,7 @@
ADD_SUBDIRECTORY(z120_test)
ADD_SUBDIRECTORY(membership)
+ADD_SUBDIRECTORY(montecarlo)
ADD_EXECUTABLE(checker_test
checker_test.cpp
Property changes on: trunk/tests/acyclic
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/deadlock
___________________________________________________________________
Added: svn:ignore
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/fifo
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/local_choice
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/membership
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/montecarlo
___________________________________________________________________
Added: svn:ignore
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Added: trunk/tests/montecarlo/CMakeLists.txt
===================================================================
--- trunk/tests/montecarlo/CMakeLists.txt (rev 0)
+++ trunk/tests/montecarlo/CMakeLists.txt 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,10 @@
+ADD_EXECUTABLE(montecarlo_test
+ montecarlo_test.cpp
+)
+
+TARGET_LINK_LIBRARIES(montecarlo_test
+ scmontecarlo
+ scmsc
+ scZ120
+)
+
Property changes on: trunk/tests/montecarlo/CMakeLists.txt
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/tests/montecarlo/montecarlo_test.cpp
===================================================================
--- trunk/tests/montecarlo/montecarlo_test.cpp (rev 0)
+++ trunk/tests/montecarlo/montecarlo_test.cpp 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,72 @@
+/*
+ * scstudio - Sequence Chart Studio
+ * http://scstudio.sourceforge.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * Copyright (c) 2010 Petr Gotthard <pet...@ce...>
+ *
+ * $Id$
+ */
+
+#include <iostream>
+
+#include "data/Z120/z120.h"
+#include "montecarlo/montecarlo.h"
+
+int main(int argc, char** argv)
+{
+ if(argc < 2)
+ {
+ std::cerr << "Usage: " << argv[0] << " <filename>" << std::endl;
+ return 1;
+ }
+
+ Z120 z120;
+
+ StreamReportPrinter printer(std::wcerr);
+ z120.set_printer(&printer);
+
+ std::vector<MscPtr> msc;
+
+ try
+ {
+ msc = z120.load_msc(argv[1]);
+ }
+ catch(std::exception& exc)
+ {
+ std::cerr << "EXCEPTION: " << exc.what() << std::endl;
+ return 1;
+ }
+
+ try
+ {
+ MonteCarlo simulator;
+
+ // run the simulation
+ const MonteCarlo::MeasurementsMap& measurements = simulator.run(msc[0]);
+
+ // print the measurements
+ for(MonteCarlo::MeasurementsMap::const_iterator mpos = measurements.begin();
+ mpos != measurements.end(); mpos++)
+ {
+ std::cout << mpos->first << " = " << mpos->second << std::endl;
+ }
+ }
+ catch(std::exception& exc)
+ {
+ std::cerr << "EXCEPTION: Simulation failed: " << exc.what() << std::endl;
+ return 2;
+ }
+
+ return 0;
+}
+
+// $Id$
Property changes on: trunk/tests/montecarlo/montecarlo_test.cpp
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/tests/montecarlo/test_cycle.mpr
===================================================================
--- trunk/tests/montecarlo/test_cycle.mpr (rev 0)
+++ trunk/tests/montecarlo/test_cycle.mpr 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,14 @@
+mscdocument Vykres1;
+msc Stranka_1;
+inst Client;
+inst Server;
+Client: instance;
+label e0;
+in Response,1 from Client;
+out Request,0 to Server;
+endinstance;
+Server: instance;
+in Request,0 from Client;
+out Response,1 to Server;
+endinstance;
+endmsc;
Property changes on: trunk/tests/montecarlo/test_cycle.mpr
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Added: trunk/tests/montecarlo/test_simple.mpr
===================================================================
--- trunk/tests/montecarlo/test_simple.mpr (rev 0)
+++ trunk/tests/montecarlo/test_simple.mpr 2010-02-13 20:48:22 UTC (rev 589)
@@ -0,0 +1,14 @@
+mscdocument Vykres1;
+msc Stranka_1;
+inst Client;
+inst Server;
+Client: instance;
+label e0;
+out Request,0 to Server;
+time e1 &t1, e1 (1,2] &t2;
+endinstance;
+Server: instance;
+label e1;
+in Request,0 from Client;
+endinstance;
+endmsc;
Property changes on: trunk/tests/montecarlo/test_simple.mpr
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Property changes on: trunk/tests/time
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/universal_boundedness
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
Property changes on: trunk/tests/z120_test
___________________________________________________________________
Modified: svn:ignore
- CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
+ CMakeFiles
cmake_install.cmake
CTestTestfile.cmake
*.dir
Makefile
*.vcproj
*.vcproj.*
*.swp
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|