DESERT 3.5.1
Loading...
Searching...
No Matches
mclink.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2021 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include <string.h>
40#include <rng.h>
41#include <tclcl.h>
42#include <iostream>
43#include "mclink.h"
44
46 : ber_good(0.0)
47 , ber_bad(0.0)
48 , p_gb(0.0)
49 , p_bg(0.0)
50 , last_update(NOW)
51 , step_period(5.0)
52 , ch_state(GOOD)
53{
54}
55
56
57MCLink::MCLink(double ber_good, double ber_bad,double p_gb, double p_bg,
58 double step_period,ChState ch_state)
59 : ber_good(ber_good)
60 , ber_bad(ber_bad)
61 , p_gb(p_gb)
62 , p_bg(p_bg)
63 , last_update(NOW)
64 , step_period(step_period)
65 , ch_state(ch_state)
66{
67 assert(ber_good >=0.0 && ber_good <= 1.0 &&
68 ber_bad >= 0.0 && ber_bad <= 1.0 &&
69 p_gb >= 0.0 && p_gb <= 1.0 && p_bg >= 0.0 && p_bg <= 1.0);
70}
71
74{
75 int n_step = floor((NOW - last_update) / step_period);
76 if (n_step == 0) {
77 return ch_state;
78 }
79
80 double c1 = 1.0 / (p_gb + p_bg);
81 double c2 = pow(1.0 - p_gb - p_bg, n_step) / (p_gb + p_bg);
82
83 if (ch_state == MCLink::GOOD) {
84 double new_p_gg = c1 * p_bg + c2 * p_gb;
85 if (RNG::defaultrng()->uniform_double() > new_p_gg) {
87 }
88 } else if (ch_state == MCLink::BAD) {
89 double new_p_bb = c1 * p_gb + c2 * p_bg;
90 if (RNG::defaultrng()->uniform_double() > new_p_bb) {
92 }
93 }
94 last_update += n_step * step_period;
95 return ch_state;
96}
97
98double
100{
102 if (ch_state == GOOD) {
103 return ber_good;
104 } else {
105 return ber_bad;
106 }
107}
108
109int
110MCLink::command(int argc, const char *const *argv)
111{
112 Tcl &tcl = Tcl::instance();
113
114 if (argc == 2) {
115 if (strcasecmp(argv[1], "getChState") == 0) {
116 tcl.resultf("%d", getChState());
117 return TCL_OK;
118 } else if (strcasecmp(argv[1], "getBER") == 0) {
119 tcl.resultf("%f", getBER());
120 return TCL_OK;
121 }
122 }
123
124 std::cerr << "TCL: unknown MCLink command" << std::endl;
125 return TCL_ERROR;
126}
127
128static class MCLinkClass : public TclClass
129{
130public:
132 : TclClass("Module/UW/HMMPHYSICAL/MCLINK")
133 {
134 }
135 TclObject *
136 create(int argc, const char *const * argv)
137 {
138 if (argc > 8) {
139 if (argc == 9) {
140 return (new MCLink(std::stod(argv[4]), std::stod(argv[5]),
141 std::stod(argv[6]), std::stod(argv[7]),std::stod(argv[8])));
142 }
143 if (argc == 10) {
144 MCLink::ChState ch_state;
145 if (strcasecmp(argv[9], "GOOD") == 0) {
146 ch_state = MCLink::GOOD;
147 } else if (strcasecmp(argv[9], "BAD") == 0) {
148 ch_state = MCLink::BAD;
149 } else {
150 std::cerr << "TCL: MCLink state must be GOOD or BAD"
151 << std::endl;
152 exit(1);
153 }
154 return (new MCLink(std::stod(argv[4]), std::stod(argv[5]),
155 std::stod(argv[6]), std::stod(argv[7]),std::stod(argv[8]),ch_state));
156 }
157 std::cerr << "TCL: check MCLink constructor args" << std::endl;
158 exit(1);
159 }
160
161 return (new MCLink);
162 }
TclObject * create(int argc, const char *const *argv)
Definition mclink.cpp:136