Best Python code snippet using lemoncheesecake
test_boolean.py
Source:test_boolean.py
1import unittest2from psychsim.pwl import WORLD, stateKey3from rddl2psychsim.conversion.converter import Converter4__author__ = 'Pedro Sequeira'5__email__ = 'pedrodbs@gmail.com'6class TestBoolean(unittest.TestCase):7 def test_and_false(self):8 rddl = '''9 domain my_test {10 pvariables { 11 p : { state-fluent, bool, default = false };12 q : { state-fluent, bool, default = true };13 a : { action-fluent, bool, default = false }; 14 };15 cpfs { p' = p ^ q; };16 reward = 0;17 }18 non-fluents my_test_empty { domain = my_test; }19 instance my_test_inst { domain = my_test; init-state { a; }; }20 '''21 conv = Converter()22 conv.convert_str(rddl)23 conv.world.step()24 p = conv.world.getState(WORLD, 'p', unique=True)25 q = conv.world.getState(WORLD, 'q', unique=True)26 self.assertEqual(p, False)27 self.assertEqual(q, True)28 def test_and_self(self):29 rddl = '''30 domain my_test {31 pvariables { 32 p : { state-fluent, bool, default = true };33 a : { action-fluent, bool, default = false }; 34 };35 cpfs { p' = p ^ p; };36 reward = 0;37 }38 non-fluents my_test_empty { domain = my_test; }39 instance my_test_inst { domain = my_test; init-state { a; }; }40 '''41 conv = Converter()42 conv.convert_str(rddl)43 conv.world.step()44 p = conv.world.getState(WORLD, 'p', unique=True)45 self.assertEqual(p, True)46 def test_and_multi_linear(self):47 rddl = '''48 domain my_test {49 pvariables { 50 p : { state-fluent, bool, default = true };51 q : { state-fluent, bool, default = true };52 r : { state-fluent, bool, default = true };53 a : { action-fluent, bool, default = false }; 54 };55 cpfs { p' = p ^ q ^ r; };56 reward = 0;57 }58 non-fluents my_test_empty { domain = my_test; }59 instance my_test_inst { domain = my_test; init-state { a; }; }60 '''61 conv = Converter()62 conv.convert_str(rddl)63 conv.world.step()64 p = conv.world.getState(WORLD, 'p', unique=True)65 q = conv.world.getState(WORLD, 'q', unique=True)66 r = conv.world.getState(WORLD, 'r', unique=True)67 self.assertEqual(p, True)68 self.assertEqual(q, True)69 self.assertEqual(r, True)70 def test_and_false_const(self):71 rddl = '''72 domain my_test {73 pvariables { 74 p : { state-fluent, bool, default = true };75 a : { action-fluent, bool, default = false }; 76 };77 cpfs { p' = p ^ 0; };78 reward = 0;79 }80 non-fluents my_test_empty { domain = my_test; }81 instance my_test_inst { domain = my_test; init-state { a; }; }82 '''83 conv = Converter()84 conv.convert_str(rddl)85 p = conv.world.getState(WORLD, 'p', unique=True)86 self.assertEqual(p, True)87 conv.world.step()88 p = conv.world.getState(WORLD, 'p', unique=True)89 self.assertEqual(p, False)90 def test_and_false_consts(self):91 rddl = '''92 domain my_test {93 pvariables { 94 p : { state-fluent, bool, default = true };95 a : { action-fluent, bool, default = false }; 96 };97 cpfs { p' = 1 ^ 0; };98 reward = 0;99 }100 non-fluents my_test_empty { domain = my_test; }101 instance my_test_inst { domain = my_test; init-state { a; }; }102 '''103 conv = Converter()104 conv.convert_str(rddl)105 p = conv.world.getState(WORLD, 'p', unique=True)106 self.assertEqual(p, True)107 conv.world.step()108 p = conv.world.getState(WORLD, 'p', unique=True)109 self.assertEqual(p, False)110 def test_and_false_rel(self):111 rddl = '''112 domain my_test {113 pvariables { 114 p : { state-fluent, bool, default = true };115 q : { state-fluent, int, default = 3 };116 r : { state-fluent, int, default = 2 };117 a : { action-fluent, bool, default = false }; 118 };119 cpfs { p' = q > 3 ^ r < 3; };120 reward = 0;121 }122 non-fluents my_test_empty { domain = my_test; }123 instance my_test_inst { domain = my_test; init-state { a; }; }124 '''125 conv = Converter()126 conv.convert_str(rddl)127 p = conv.world.getState(WORLD, 'p', unique=True)128 self.assertEqual(p, True)129 conv.world.step()130 p = conv.world.getState(WORLD, 'p', unique=True)131 self.assertEqual(p, False)132 def test_and_multi_rel(self):133 rddl = '''134 domain my_test {135 pvariables { 136 p : { state-fluent, bool, default = false };137 q : { state-fluent, int, default = 3 };138 r : { state-fluent, int, default = 2 };139 s : { state-fluent, int, default = 1 };140 a : { action-fluent, bool, default = false }; 141 };142 cpfs { p' = q >= 3 ^ r < 3 ^ s == 1; };143 reward = 0;144 }145 non-fluents my_test_empty { domain = my_test; }146 instance my_test_inst { domain = my_test; init-state { a; }; }147 '''148 conv = Converter()149 conv.convert_str(rddl)150 p = conv.world.getState(WORLD, 'p', unique=True)151 self.assertEqual(p, False)152 conv.world.step()153 p = conv.world.getState(WORLD, 'p', unique=True)154 self.assertEqual(p, True)155 def test_and_true(self):156 rddl = '''157 domain my_test {158 pvariables { 159 p : { state-fluent, bool, default = true };160 q : { state-fluent, bool, default = true };161 a : { action-fluent, bool, default = false }; 162 };163 cpfs { p' = p ^ q; };164 reward = 0;165 }166 non-fluents my_test_empty { domain = my_test; }167 instance my_test_inst { domain = my_test; init-state { a; }; }168 '''169 conv = Converter()170 conv.convert_str(rddl)171 conv.world.step()172 p = conv.world.getState(WORLD, 'p', unique=True)173 q = conv.world.getState(WORLD, 'q', unique=True)174 self.assertEqual(p, True)175 self.assertEqual(q, True)176 def test_and_true_not(self):177 rddl = '''178 domain my_test {179 pvariables { 180 p : { state-fluent, bool, default = true };181 q : { state-fluent, bool, default = false };182 a : { action-fluent, bool, default = false }; 183 };184 cpfs { p' = p ^ ~q; };185 reward = 0;186 }187 non-fluents my_test_empty { domain = my_test; }188 instance my_test_inst { domain = my_test; init-state { a; }; }189 '''190 conv = Converter()191 conv.convert_str(rddl)192 conv.world.step()193 p = conv.world.getState(WORLD, 'p', unique=True)194 q = conv.world.getState(WORLD, 'q', unique=True)195 self.assertEqual(p, True)196 self.assertEqual(q, False)197 def test_and_true2(self):198 rddl = '''199 domain my_test {200 pvariables { 201 p : { state-fluent, bool, default = true };202 q : { state-fluent, bool, default = true };203 r : { state-fluent, bool, default = true };204 a : { action-fluent, bool, default = false }; 205 };206 cpfs { p' = p ^ q ^ r; };207 reward = 0;208 }209 non-fluents my_test_empty { domain = my_test; }210 instance my_test_inst { domain = my_test; init-state { a; }; }211 '''212 conv = Converter()213 conv.convert_str(rddl)214 conv.world.step()215 p = conv.world.getState(WORLD, 'p', unique=True)216 q = conv.world.getState(WORLD, 'q', unique=True)217 r = conv.world.getState(WORLD, 'r', unique=True)218 self.assertEqual(p, True)219 self.assertEqual(q, True)220 self.assertEqual(r, True)221 def test_and_false_num(self):222 rddl = '''223 domain my_test {224 pvariables { 225 p : { state-fluent, bool, default = false };226 q : { state-fluent, real, default = 0.5 };227 r : { state-fluent, int, default = 1 };228 a : { action-fluent, bool, default = false }; 229 };230 cpfs { p' = q ^ r; }; // sum needed to be > 1.5231 reward = 0;232 }233 non-fluents my_test_empty { domain = my_test; }234 instance my_test_inst { domain = my_test; init-state { a; }; }235 '''236 conv = Converter()237 conv.convert_str(rddl)238 conv.world.step()239 p = conv.world.getState(WORLD, 'p', unique=True)240 q = conv.world.getState(WORLD, 'q', unique=True)241 r = conv.world.getState(WORLD, 'r', unique=True)242 self.assertEqual(p, False)243 self.assertEqual(q, 0.5)244 self.assertEqual(r, 1)245 def test_and_false_num2(self):246 rddl = '''247 domain my_test {248 pvariables { 249 p : { state-fluent, bool, default = false };250 q : { state-fluent, real, default = 0.5 };251 r : { state-fluent, int, default = 0 };252 a : { action-fluent, bool, default = false }; 253 };254 cpfs { p' = 4 * q ^ r; }; // sum needed to be > (4 * 1 + 1) - 0.5255 reward = 0;256 }257 non-fluents my_test_empty { domain = my_test; }258 instance my_test_inst { domain = my_test; init-state { a; }; }259 '''260 conv = Converter()261 conv.convert_str(rddl)262 conv.world.step()263 p = conv.world.getState(WORLD, 'p', unique=True)264 q = conv.world.getState(WORLD, 'q', unique=True)265 r = conv.world.getState(WORLD, 'r', unique=True)266 self.assertEqual(p, False)267 self.assertEqual(q, 0.5)268 self.assertEqual(r, 0)269 def test_and_true_self(self):270 rddl = '''271 domain my_test {272 pvariables { 273 p : { state-fluent, bool, default = true };274 a : { action-fluent, bool, default = false }; 275 };276 cpfs { p' = p ^ p; };277 reward = 0;278 }279 non-fluents my_test_empty { domain = my_test; }280 instance my_test_inst { domain = my_test; init-state { a; }; }281 '''282 conv = Converter()283 conv.convert_str(rddl)284 conv.world.step()285 p = conv.world.getState(WORLD, 'p', unique=True)286 self.assertEqual(p, True)287 def test_and_true_const(self):288 rddl = '''289 domain my_test {290 pvariables { 291 p : { state-fluent, bool, default = true };292 a : { action-fluent, bool, default = false }; 293 };294 cpfs { p' = p ^ 1; };295 reward = 0;296 }297 non-fluents my_test_empty { domain = my_test; }298 instance my_test_inst { domain = my_test; init-state { a; }; }299 '''300 conv = Converter()301 conv.convert_str(rddl)302 p = conv.world.getState(WORLD, 'p', unique=True)303 self.assertEqual(p, True)304 conv.world.step()305 p = conv.world.getState(WORLD, 'p', unique=True)306 self.assertEqual(p, True)307 def test_and_true_const2(self):308 rddl = '''309 domain my_test {310 pvariables { 311 p : { state-fluent, bool, default = true };312 a : { action-fluent, bool, default = false }; 313 };314 cpfs { p' = p ^ 4.9; };315 reward = 0;316 }317 non-fluents my_test_empty { domain = my_test; }318 instance my_test_inst { domain = my_test; init-state { a; }; }319 '''320 conv = Converter()321 conv.convert_str(rddl)322 p = conv.world.getState(WORLD, 'p', unique=True)323 self.assertEqual(p, True)324 conv.world.step()325 p = conv.world.getState(WORLD, 'p', unique=True)326 self.assertEqual(p, True)327 def test_and_true_const3(self):328 rddl = '''329 domain my_test {330 pvariables { 331 p : { state-fluent, bool, default = true };332 a : { action-fluent, bool, default = false }; 333 };334 cpfs { p' = p ^ -1; };335 reward = 0;336 }337 non-fluents my_test_empty { domain = my_test; }338 instance my_test_inst { domain = my_test; init-state { a; }; }339 '''340 conv = Converter()341 conv.convert_str(rddl)342 p = conv.world.getState(WORLD, 'p', unique=True)343 self.assertEqual(p, True)344 conv.world.step()345 p = conv.world.getState(WORLD, 'p', unique=True)346 self.assertEqual(p, True)347 def test_and_true_consts(self):348 rddl = '''349 domain my_test {350 pvariables { 351 p : { state-fluent, bool, default = false };352 a : { action-fluent, bool, default = false }; 353 };354 cpfs { p' = 3 ^ 1; };355 reward = 0;356 }357 non-fluents my_test_empty { domain = my_test; }358 instance my_test_inst { domain = my_test; init-state { a; }; }359 '''360 conv = Converter()361 conv.convert_str(rddl)362 p = conv.world.getState(WORLD, 'p', unique=True)363 self.assertEqual(p, False)364 conv.world.step()365 p = conv.world.getState(WORLD, 'p', unique=True)366 self.assertEqual(p, True)367 def test_and_true_rel(self):368 rddl = '''369 domain my_test {370 pvariables { 371 p : { state-fluent, bool, default = false };372 q : { state-fluent, int, default = 3 };373 r : { state-fluent, int, default = 2 };374 a : { action-fluent, bool, default = false }; 375 };376 cpfs { p' = q >= 3 ^ r < 3; };377 reward = 0;378 }379 non-fluents my_test_empty { domain = my_test; }380 instance my_test_inst { domain = my_test; init-state { a; }; }381 '''382 conv = Converter()383 conv.convert_str(rddl)384 p = conv.world.getState(WORLD, 'p', unique=True)385 self.assertEqual(p, False)386 conv.world.step()387 p = conv.world.getState(WORLD, 'p', unique=True)388 self.assertEqual(p, True)389 def test_or_false(self):390 rddl = '''391 domain my_test {392 pvariables { 393 p : { state-fluent, bool, default = false };394 q : { state-fluent, bool, default = false };395 a : { action-fluent, bool, default = false }; 396 };397 cpfs { p' = p | q; };398 reward = 0;399 }400 non-fluents my_test_empty { domain = my_test; }401 instance my_test_inst { domain = my_test; init-state { a; }; }402 '''403 conv = Converter()404 conv.convert_str(rddl)405 conv.world.step()406 p = conv.world.getState(WORLD, 'p', unique=True)407 q = conv.world.getState(WORLD, 'q', unique=True)408 self.assertEqual(p, False)409 self.assertEqual(q, False)410 def test_or_self(self):411 rddl = '''412 domain my_test {413 pvariables { 414 p : { state-fluent, bool, default = true };415 a : { action-fluent, bool, default = false }; 416 };417 cpfs { p' = p | p; };418 reward = 0;419 }420 non-fluents my_test_empty { domain = my_test; }421 instance my_test_inst { domain = my_test; init-state { a; }; }422 '''423 conv = Converter()424 conv.convert_str(rddl)425 conv.world.step()426 p = conv.world.getState(WORLD, 'p', unique=True)427 self.assertEqual(p, True)428 def test_or_multi(self):429 rddl = '''430 domain my_test {431 pvariables { 432 p : { state-fluent, bool, default = false };433 q : { state-fluent, bool, default = false };434 r : { state-fluent, bool, default = true };435 a : { action-fluent, bool, default = false }; 436 };437 cpfs { p' = p | q | r; };438 reward = 0;439 }440 non-fluents my_test_empty { domain = my_test; }441 instance my_test_inst { domain = my_test; init-state { a; }; }442 '''443 conv = Converter()444 conv.convert_str(rddl)445 conv.world.step()446 p = conv.world.getState(WORLD, 'p', unique=True)447 self.assertEqual(p, True)448 def test_or_false_const(self):449 rddl = '''450 domain my_test {451 pvariables { 452 p : { state-fluent, bool, default = false };453 a : { action-fluent, bool, default = false }; 454 };455 cpfs { p' = p | 0; };456 reward = 0;457 }458 non-fluents my_test_empty { domain = my_test; }459 instance my_test_inst { domain = my_test; init-state { a; }; }460 '''461 conv = Converter()462 conv.convert_str(rddl)463 p = conv.world.getState(WORLD, 'p', unique=True)464 self.assertEqual(p, False)465 conv.world.step()466 p = conv.world.getState(WORLD, 'p', unique=True)467 self.assertEqual(p, False)468 def test_or_false_const2(self):469 rddl = '''470 domain my_test {471 pvariables { 472 p : { state-fluent, bool, default = true };473 a : { action-fluent, bool, default = false }; 474 };475 cpfs { p' = 0 | 0; };476 reward = 0;477 }478 non-fluents my_test_empty { domain = my_test; }479 instance my_test_inst { domain = my_test; init-state { a; }; }480 '''481 conv = Converter()482 conv.convert_str(rddl)483 p = conv.world.getState(WORLD, 'p', unique=True)484 self.assertEqual(p, True)485 conv.world.step()486 p = conv.world.getState(WORLD, 'p', unique=True)487 self.assertEqual(p, False)488 def test_or_false_rel(self):489 rddl = '''490 domain my_test {491 pvariables { 492 p : { state-fluent, bool, default = true };493 q : { state-fluent, int, default = 3 };494 r : { state-fluent, int, default = 2 };495 a : { action-fluent, bool, default = false }; 496 };497 cpfs { p' = q > 3 | r < 1; };498 reward = 0;499 }500 non-fluents my_test_empty { domain = my_test; }501 instance my_test_inst { domain = my_test; init-state { a; }; }502 '''503 conv = Converter()504 conv.convert_str(rddl)505 p = conv.world.getState(WORLD, 'p', unique=True)506 self.assertEqual(p, True)507 conv.world.step()508 p = conv.world.getState(WORLD, 'p', unique=True)509 self.assertEqual(p, False)510 def test_or_true(self):511 rddl = '''512 domain my_test {513 pvariables { 514 p : { state-fluent, bool, default = true };515 q : { state-fluent, bool, default = false };516 a : { action-fluent, bool, default = false }; 517 };518 cpfs { p' = p | q; };519 reward = 0;520 }521 non-fluents my_test_empty { domain = my_test; }522 instance my_test_inst { domain = my_test; init-state { a; }; }523 '''524 conv = Converter()525 conv.convert_str(rddl)526 conv.world.step()527 p = conv.world.getState(WORLD, 'p', unique=True)528 q = conv.world.getState(WORLD, 'q', unique=True)529 self.assertEqual(p, True)530 self.assertEqual(q, False)531 def test_or_true2(self):532 rddl = '''533 domain my_test {534 pvariables { 535 p : { state-fluent, bool, default = false };536 q : { state-fluent, bool, default = false };537 r : { state-fluent, bool, default = true };538 a : { action-fluent, bool, default = false }; 539 };540 cpfs { p' = p | q | r; };541 reward = 0;542 }543 non-fluents my_test_empty { domain = my_test; }544 instance my_test_inst { domain = my_test; init-state { a; }; }545 '''546 conv = Converter()547 conv.convert_str(rddl)548 conv.world.step()549 p = conv.world.getState(WORLD, 'p', unique=True)550 q = conv.world.getState(WORLD, 'q', unique=True)551 r = conv.world.getState(WORLD, 'r', unique=True)552 self.assertEqual(p, True)553 self.assertEqual(q, False)554 self.assertEqual(r, True)555 def test_or_true_not(self):556 rddl = '''557 domain my_test {558 pvariables { 559 p : { state-fluent, bool, default = false };560 q : { state-fluent, bool, default = false };561 a : { action-fluent, bool, default = false }; 562 };563 cpfs { p' = p | ~q; };564 reward = 0;565 }566 non-fluents my_test_empty { domain = my_test; }567 instance my_test_inst { domain = my_test; init-state { a; }; }568 '''569 conv = Converter()570 conv.convert_str(rddl)571 conv.world.step()572 p = conv.world.getState(WORLD, 'p', unique=True)573 q = conv.world.getState(WORLD, 'q', unique=True)574 self.assertEqual(p, True)575 self.assertEqual(q, False)576 def test_or_true_self(self):577 rddl = '''578 domain my_test {579 pvariables { 580 p : { state-fluent, bool, default = true };581 a : { action-fluent, bool, default = false }; 582 };583 cpfs { p' = p | p; };584 reward = 0;585 }586 non-fluents my_test_empty { domain = my_test; }587 instance my_test_inst { domain = my_test; init-state { a; }; }588 '''589 conv = Converter()590 conv.convert_str(rddl)591 conv.world.step()592 p = conv.world.getState(WORLD, 'p', unique=True)593 self.assertEqual(p, True)594 def test_or_true_const(self):595 rddl = '''596 domain my_test {597 pvariables { 598 p : { state-fluent, bool, default = false };599 a : { action-fluent, bool, default = false }; 600 };601 cpfs { p' = p | 1; };602 reward = 0;603 }604 non-fluents my_test_empty { domain = my_test; }605 instance my_test_inst { domain = my_test; init-state { a; }; }606 '''607 conv = Converter()608 conv.convert_str(rddl)609 p = conv.world.getState(WORLD, 'p', unique=True)610 self.assertEqual(p, False)611 conv.world.step()612 p = conv.world.getState(WORLD, 'p', unique=True)613 self.assertEqual(p, True)614 def test_or_true_const2(self):615 rddl = '''616 domain my_test {617 pvariables { 618 p : { state-fluent, bool, default = false };619 a : { action-fluent, bool, default = false }; 620 };621 cpfs { p' = p | 4.9; };622 reward = 0;623 }624 non-fluents my_test_empty { domain = my_test; }625 instance my_test_inst { domain = my_test; init-state { a; }; }626 '''627 conv = Converter()628 conv.convert_str(rddl)629 p = conv.world.getState(WORLD, 'p', unique=True)630 self.assertEqual(p, False)631 conv.world.step()632 p = conv.world.getState(WORLD, 'p', unique=True)633 self.assertEqual(p, True)634 def test_or_true_const3(self):635 rddl = '''636 domain my_test {637 pvariables { 638 p : { state-fluent, bool, default = false };639 a : { action-fluent, bool, default = false }; 640 };641 cpfs { p' = p | -1; };642 reward = 0;643 }644 non-fluents my_test_empty { domain = my_test; }645 instance my_test_inst { domain = my_test; init-state { a; }; }646 '''647 conv = Converter()648 conv.convert_str(rddl)649 p = conv.world.getState(WORLD, 'p', unique=True)650 self.assertEqual(p, False)651 conv.world.step()652 p = conv.world.getState(WORLD, 'p', unique=True)653 self.assertEqual(p, True)654 def test_or_true_consts(self):655 rddl = '''656 domain my_test {657 pvariables { 658 p : { state-fluent, bool, default = false };659 a : { action-fluent, bool, default = false }; 660 };661 cpfs { p' = 6 | 0; };662 reward = 0;663 }664 non-fluents my_test_empty { domain = my_test; }665 instance my_test_inst { domain = my_test; init-state { a; }; }666 '''667 conv = Converter()668 conv.convert_str(rddl)669 p = conv.world.getState(WORLD, 'p', unique=True)670 self.assertEqual(p, False)671 conv.world.step()672 p = conv.world.getState(WORLD, 'p', unique=True)673 self.assertEqual(p, True)674 def test_or_true_rel(self):675 rddl = '''676 domain my_test {677 pvariables { 678 p : { state-fluent, bool, default = false };679 q : { state-fluent, int, default = 3 };680 r : { state-fluent, int, default = 2 };681 a : { action-fluent, bool, default = false }; 682 };683 cpfs { p' = q > 2 | r <= 1; };684 reward = 0;685 }686 non-fluents my_test_empty { domain = my_test; }687 instance my_test_inst { domain = my_test; init-state { a; }; }688 '''689 conv = Converter()690 conv.convert_str(rddl)691 p = conv.world.getState(WORLD, 'p', unique=True)692 self.assertEqual(p, False)693 conv.world.step()694 p = conv.world.getState(WORLD, 'p', unique=True)695 self.assertEqual(p, True)696 def test_or_true_rel_multi(self):697 rddl = '''698 domain my_test {699 pvariables { 700 p : { state-fluent, bool, default = false };701 q : { state-fluent, int, default = 3 };702 r : { state-fluent, int, default = 2 };703 s : { state-fluent, int, default = 1 };704 a : { action-fluent, bool, default = false }; 705 };706 cpfs { p' = q > 2 | r <= 1 | s == 0; };707 reward = 0;708 }709 non-fluents my_test_empty { domain = my_test; }710 instance my_test_inst { domain = my_test; init-state { a; }; }711 '''712 conv = Converter()713 conv.convert_str(rddl)714 p = conv.world.getState(WORLD, 'p', unique=True)715 self.assertEqual(p, False)716 conv.world.step()717 p = conv.world.getState(WORLD, 'p', unique=True)718 self.assertEqual(p, True)719 def test_or_multi_var_equal(self):720 rddl = '''721 domain my_test {722 pvariables { 723 p : { state-fluent, bool, default = true };724 q : { state-fluent, int, default = 3 };725 a : { action-fluent, bool, default = false }; 726 };727 cpfs { p' = q == 2 | q == 1 | q == 0; };728 reward = 0;729 }730 non-fluents my_test_empty { domain = my_test; }731 instance my_test_inst { domain = my_test; init-state { a; }; }732 '''733 conv = Converter()734 conv.convert_str(rddl)735 dyn = conv.world.getDynamics(stateKey(WORLD, 'p'), True)[0]736 self.assertEqual(len(dyn.branch.planes), 3) # disjunction over possible const alues737 p = conv.world.getState(WORLD, 'p', unique=True)738 self.assertEqual(p, True)739 conv.world.step()740 p = conv.world.getState(WORLD, 'p', unique=True)741 self.assertEqual(p, False)742 def test_or_multi_var_equal_and(self):743 rddl = '''744 domain my_test {745 pvariables { 746 p : { state-fluent, bool, default = false };747 q : { state-fluent, int, default = 2 };748 r : { state-fluent, int, default = 1 };749 a : { action-fluent, bool, default = false }; 750 };751 cpfs { p' = (q == 2 ^ r > 0) | (q == 1 ^ r > 1) | (q == 0 ^ r > 2); };752 reward = 0;753 }754 non-fluents my_test_empty { domain = my_test; }755 instance my_test_inst { domain = my_test; init-state { a; }; }756 '''757 conv = Converter()758 conv.convert_str(rddl)759 dyn = conv.world.getDynamics(stateKey(WORLD, 'p'), True)[0]760 self.assertEqual(len(dyn.branch.planes), 1)761 self.assertEqual(len(dyn.branch.planes[0][1]), 3) # switch over all const values762 p = conv.world.getState(WORLD, 'p', unique=True)763 self.assertEqual(p, False)764 conv.world.step()765 p = conv.world.getState(WORLD, 'p', unique=True)766 self.assertEqual(p, True)767 def test_or_multi_var_equal_and2(self):768 rddl = '''769 domain my_test {770 pvariables { 771 p : { state-fluent, bool, default = false };772 q : { state-fluent, int, default = 2 };773 r : { state-fluent, int, default = 1 };774 a : { action-fluent, bool, default = false }; 775 };776 cpfs { p' = (q == 2 ^ r > 0) | (q == 1 ^ r > 1) | (q == 0 ^ r > 2) | p | r == 0; };777 reward = 0;778 }779 non-fluents my_test_empty { domain = my_test; }780 instance my_test_inst { domain = my_test; init-state { a; }; }781 '''782 conv = Converter()783 conv.convert_str(rddl)784 dyn = conv.world.getDynamics(stateKey(WORLD, 'p'), True)[0]785 self.assertEqual(len(dyn.branch.planes), 1) # switch first, then p, then r == 0 in OR tree786 self.assertEqual(len(dyn.branch.planes[0][1]), 3) # switch over all const values787 p = conv.world.getState(WORLD, 'p', unique=True)788 self.assertEqual(p, False)789 conv.world.step()790 p = conv.world.getState(WORLD, 'p', unique=True)791 self.assertEqual(p, True)792 def test_and_or_true_rel(self):793 rddl = '''794 domain my_test {795 pvariables { 796 p : { state-fluent, bool, default = false };797 q : { state-fluent, int, default = 3 };798 r : { state-fluent, int, default = 2 };799 a : { action-fluent, bool, default = false }; 800 };801 cpfs { p' = p | q >= 3 ^ r < 3; };802 reward = 0;803 }804 non-fluents my_test_empty { domain = my_test; }805 instance my_test_inst { domain = my_test; init-state { a; }; }806 '''807 conv = Converter()808 conv.convert_str(rddl)809 dyn = conv.world.getDynamics(stateKey(WORLD, 'p'), True)[0]810 self.assertEqual(len(dyn.branch.planes), 1)811 self.assertIsNone(dyn.children[True].branch)812 self.assertEqual(len(dyn.children[False].branch.planes), 2)813 p = conv.world.getState(WORLD, 'p', unique=True)814 self.assertEqual(p, False)815 conv.world.step()816 p = conv.world.getState(WORLD, 'p', unique=True)817 self.assertEqual(p, True)818 def test_and_or_false_rel(self):819 rddl = '''820 domain my_test {821 pvariables { 822 p : { state-fluent, bool, default = false };823 q : { state-fluent, int, default = 3 };824 r : { state-fluent, int, default = 2 };825 a : { action-fluent, bool, default = false }; 826 };827 cpfs { p' = (p | q > 3) ^ r < 3; };828 reward = 0;829 }830 non-fluents my_test_empty { domain = my_test; }831 instance my_test_inst { domain = my_test; init-state { a; }; }832 '''833 conv = Converter()834 conv.convert_str(rddl)835 dyn = conv.world.getDynamics(stateKey(WORLD, 'p'), True)[0]836 self.assertEqual(len(dyn.branch.planes), 2)837 self.assertEqual(len(dyn.children[True].branch.planes), 1)838 self.assertIsNone(dyn.children[False].branch)839 p = conv.world.getState(WORLD, 'p', unique=True)840 self.assertEqual(p, False)841 conv.world.step()842 p = conv.world.getState(WORLD, 'p', unique=True)843 self.assertEqual(p, False)844 def test_not_false(self):845 rddl = '''846 domain my_test {847 pvariables { 848 p : { state-fluent, bool, default = true };849 a : { action-fluent, bool, default = false }; 850 };851 cpfs { p' = ~p; };852 reward = 0;853 }854 non-fluents my_test_empty { domain = my_test; }855 instance my_test_inst { domain = my_test; init-state { a; }; }856 '''857 conv = Converter()858 conv.convert_str(rddl)859 p = conv.world.getState(WORLD, 'p', unique=True)860 self.assertEqual(p, True)861 conv.world.step()862 p = conv.world.getState(WORLD, 'p', unique=True)863 self.assertEqual(p, False)864 def test_not_false_const(self):865 rddl = '''866 domain my_test {867 pvariables { 868 p : { state-fluent, bool, default = true };869 a : { action-fluent, bool, default = false }; 870 };871 cpfs { p' = ~4.2; };872 reward = 0;873 }874 non-fluents my_test_empty { domain = my_test; }875 instance my_test_inst { domain = my_test; init-state { a; }; }876 '''877 conv = Converter()878 conv.convert_str(rddl)879 p = conv.world.getState(WORLD, 'p', unique=True)880 self.assertEqual(p, True)881 conv.world.step()882 p = conv.world.getState(WORLD, 'p', unique=True)883 self.assertEqual(p, False)884 def test_not_true(self):885 rddl = '''886 domain my_test {887 pvariables { 888 p : { state-fluent, bool, default = false };889 a : { action-fluent, bool, default = false }; 890 };891 cpfs { p' = ~p; };892 reward = 0;893 }894 non-fluents my_test_empty { domain = my_test; }895 instance my_test_inst { domain = my_test; init-state { a; }; }896 '''897 conv = Converter()898 conv.convert_str(rddl)899 p = conv.world.getState(WORLD, 'p', unique=True)900 self.assertEqual(p, False)901 conv.world.step()902 p = conv.world.getState(WORLD, 'p', unique=True)903 self.assertEqual(p, True)904 def test_not_true_const(self):905 rddl = '''906 domain my_test {907 pvariables { 908 p : { state-fluent, bool, default = false };909 a : { action-fluent, bool, default = false }; 910 };911 cpfs { p' = ~0.0; };912 reward = 0;913 }914 non-fluents my_test_empty { domain = my_test; }915 instance my_test_inst { domain = my_test; init-state { a; }; }916 '''917 conv = Converter()918 conv.convert_str(rddl)919 p = conv.world.getState(WORLD, 'p', unique=True)920 self.assertEqual(p, False)921 conv.world.step()922 p = conv.world.getState(WORLD, 'p', unique=True)923 self.assertEqual(p, True)924 def test_not_and_true(self):925 rddl = '''926 domain my_test {927 pvariables { 928 p : { state-fluent, bool, default = false };929 q : { state-fluent, bool, default = true };930 a : { action-fluent, bool, default = false }; 931 };932 cpfs { p' = ~(p ^ q); };933 reward = 0;934 }935 non-fluents my_test_empty { domain = my_test; }936 instance my_test_inst { domain = my_test; init-state { a; }; }937 '''938 conv = Converter()939 conv.convert_str(rddl)940 conv.world.step()941 p = conv.world.getState(WORLD, 'p', unique=True)942 q = conv.world.getState(WORLD, 'q', unique=True)943 self.assertEqual(p, True)944 self.assertEqual(q, True)945 def test_not_and_true2(self):946 rddl = '''947 domain my_test {948 pvariables { 949 p : { state-fluent, bool, default = true };950 q : { state-fluent, bool, default = false };951 a : { action-fluent, bool, default = false }; 952 };953 cpfs { p' = ~(p ^ q); };954 reward = 0;955 }956 non-fluents my_test_empty { domain = my_test; }957 instance my_test_inst { domain = my_test; init-state { a; }; }958 '''959 conv = Converter()960 conv.convert_str(rddl)961 conv.world.step()962 p = conv.world.getState(WORLD, 'p', unique=True)963 q = conv.world.getState(WORLD, 'q', unique=True)964 self.assertEqual(p, True)965 self.assertEqual(q, False)966 def test_not_and_rel(self):967 rddl = '''968 domain my_test {969 pvariables { 970 p : { state-fluent, bool, default = true };971 q : { state-fluent, int, default = 1 };972 r : { state-fluent, int, default = 3 };973 a : { action-fluent, bool, default = false }; 974 };975 cpfs { p' = ~(q <= 2 ^ r > 2); };976 reward = 0;977 }978 non-fluents my_test_empty { domain = my_test; }979 instance my_test_inst { domain = my_test; init-state { a; }; }980 '''981 conv = Converter()982 conv.convert_str(rddl)983 p = conv.world.getState(WORLD, 'p', unique=True)984 self.assertEqual(p, True)985 conv.world.step()986 p = conv.world.getState(WORLD, 'p', unique=True)987 q = conv.world.getState(WORLD, 'q', unique=True)988 r = conv.world.getState(WORLD, 'r', unique=True)989 self.assertEqual(q, 1)990 self.assertEqual(r, 3)991 self.assertEqual(p, False)992 def test_not_or_true(self):993 rddl = '''994 domain my_test {995 pvariables { 996 p : { state-fluent, bool, default = false };997 q : { state-fluent, bool, default = false };998 a : { action-fluent, bool, default = false }; 999 };1000 cpfs { p' = ~(p | q); };1001 reward = 0;1002 }1003 non-fluents my_test_empty { domain = my_test; }1004 instance my_test_inst { domain = my_test; init-state { a; }; }1005 '''1006 conv = Converter()1007 conv.convert_str(rddl)1008 conv.world.step()1009 p = conv.world.getState(WORLD, 'p', unique=True)1010 q = conv.world.getState(WORLD, 'q', unique=True)1011 self.assertEqual(p, True)1012 self.assertEqual(q, False)1013 def test_not_or_false(self):1014 rddl = '''1015 domain my_test {1016 pvariables { 1017 p : { state-fluent, bool, default = true };1018 q : { state-fluent, bool, default = false };1019 a : { action-fluent, bool, default = false }; 1020 };1021 cpfs { p' = ~(p | q); };1022 reward = 0;1023 }1024 non-fluents my_test_empty { domain = my_test; }1025 instance my_test_inst { domain = my_test; init-state { a; }; }1026 '''1027 conv = Converter()1028 conv.convert_str(rddl)1029 conv.world.step()1030 p = conv.world.getState(WORLD, 'p', unique=True)1031 q = conv.world.getState(WORLD, 'q', unique=True)1032 self.assertEqual(p, False)1033 self.assertEqual(q, False)1034 def test_not_or_rel(self):1035 rddl = '''1036 domain my_test {1037 pvariables { 1038 p : { state-fluent, bool, default = false };1039 q : { state-fluent, int, default = 1 };1040 r : { state-fluent, int, default = 3 };1041 a : { action-fluent, bool, default = false }; 1042 };1043 cpfs { p' = ~(q < 0 | r > 3); };1044 reward = 0;1045 }1046 non-fluents my_test_empty { domain = my_test; }1047 instance my_test_inst { domain = my_test; init-state { a; }; }1048 '''1049 conv = Converter()1050 conv.convert_str(rddl)1051 p = conv.world.getState(WORLD, 'p', unique=True)1052 self.assertEqual(p, False)1053 conv.world.step()1054 p = conv.world.getState(WORLD, 'p', unique=True)1055 q = conv.world.getState(WORLD, 'q', unique=True)1056 r = conv.world.getState(WORLD, 'r', unique=True)1057 self.assertEqual(q, 1)1058 self.assertEqual(r, 3)1059 self.assertEqual(p, True)1060 def test_not_not_true(self):1061 rddl = '''1062 domain my_test {1063 pvariables { 1064 p : { state-fluent, bool, default = true };1065 a : { action-fluent, bool, default = false }; 1066 };1067 cpfs { p' = ~ ~p; };1068 reward = 0;1069 }1070 non-fluents my_test_empty { domain = my_test; }1071 instance my_test_inst { domain = my_test; init-state { a; }; }1072 '''1073 conv = Converter()1074 conv.convert_str(rddl)1075 p = conv.world.getState(WORLD, 'p', unique=True)1076 self.assertEqual(p, True)1077 conv.world.step()1078 p = conv.world.getState(WORLD, 'p', unique=True)1079 self.assertEqual(p, True)1080 def test_invalid_not_if(self):1081 rddl = '''1082 domain my_test {1083 pvariables { 1084 p : { state-fluent, bool, default = false };1085 q : { state-fluent, int, default = 1 };1086 r : { state-fluent, int, default = 3 };1087 a : { action-fluent, bool, default = false }; 1088 };1089 cpfs { p' = ~( if (q < 0) then r > 2 else r < 3 ); };1090 reward = 0;1091 }1092 non-fluents my_test_empty { domain = my_test; }1093 instance my_test_inst { domain = my_test; init-state { a; }; }1094 '''1095 conv = Converter()1096 with self.assertRaises(ValueError):1097 conv.convert_str(rddl)1098 def test_comb_true(self):1099 rddl = '''1100 domain my_test {1101 pvariables { 1102 p : { state-fluent, bool, default = false };1103 q : { state-fluent, bool, default = false };1104 a : { action-fluent, bool, default = false }; 1105 };1106 cpfs { p' = (~(p ^ q) | false) ^ ~false; };1107 reward = 0;1108 }1109 non-fluents my_test_empty { domain = my_test; }1110 instance my_test_inst { domain = my_test; init-state { a; }; }1111 '''1112 conv = Converter()1113 conv.convert_str(rddl)1114 p_ = conv.world.getState(WORLD, 'p', unique=True)1115 q = conv.world.getState(WORLD, 'q', unique=True)1116 self.assertEqual(p_, False)1117 self.assertEqual(q, False)1118 conv.world.step()1119 p = conv.world.getState(WORLD, 'p', unique=True)1120 self.assertEqual(p, (not (p and q) or False) and not False)1121 def test_equiv_false(self):1122 rddl = '''1123 domain my_test {1124 pvariables { 1125 p : { state-fluent, bool, default = false };1126 q : { state-fluent, bool, default = true };1127 a : { action-fluent, bool, default = false }; 1128 };1129 cpfs { p' = p <=> q; };1130 reward = 0;1131 }1132 non-fluents my_test_empty { domain = my_test; }1133 instance my_test_inst { domain = my_test; init-state { a; }; }1134 '''1135 conv = Converter()1136 conv.convert_str(rddl)1137 conv.world.step()1138 p = conv.world.getState(WORLD, 'p', unique=True)1139 q = conv.world.getState(WORLD, 'q', unique=True)1140 self.assertEqual(p, False)1141 self.assertEqual(q, True)1142 def test_equiv_false2(self):1143 rddl = '''1144 domain my_test {1145 pvariables { 1146 p : { state-fluent, bool, default = true };1147 q : { state-fluent, bool, default = false };1148 a : { action-fluent, bool, default = false }; 1149 };1150 cpfs { p' = p <=> q; };1151 reward = 0;1152 }1153 non-fluents my_test_empty { domain = my_test; }1154 instance my_test_inst { domain = my_test; init-state { a; }; }1155 '''1156 conv = Converter()1157 conv.convert_str(rddl)1158 conv.world.step()1159 p = conv.world.getState(WORLD, 'p', unique=True)1160 q = conv.world.getState(WORLD, 'q', unique=True)1161 self.assertEqual(p, False)1162 self.assertEqual(q, False)1163 def test_equiv_false_const(self):1164 rddl = '''1165 domain my_test {1166 pvariables { 1167 p : { state-fluent, bool, default = true };1168 a : { action-fluent, bool, default = false }; 1169 };1170 cpfs { p' = p <=> 0.0; };1171 reward = 0;1172 }1173 non-fluents my_test_empty { domain = my_test; }1174 instance my_test_inst { domain = my_test; init-state { a; }; }1175 '''1176 conv = Converter()1177 conv.convert_str(rddl)1178 p = conv.world.getState(WORLD, 'p', unique=True)1179 self.assertEqual(p, True)1180 conv.world.step()1181 p = conv.world.getState(WORLD, 'p', unique=True)1182 self.assertEqual(p, False)1183 def test_equiv_false_consts(self):1184 rddl = '''1185 domain my_test {1186 pvariables { 1187 p : { state-fluent, bool, default = true };1188 a : { action-fluent, bool, default = false }; 1189 };1190 cpfs { p' = 0 <=> 1.0; };1191 reward = 0;1192 }1193 non-fluents my_test_empty { domain = my_test; }1194 instance my_test_inst { domain = my_test; init-state { a; }; }1195 '''1196 conv = Converter()1197 conv.convert_str(rddl)1198 p = conv.world.getState(WORLD, 'p', unique=True)1199 self.assertEqual(p, True)1200 conv.world.step()1201 p = conv.world.getState(WORLD, 'p', unique=True)1202 self.assertEqual(p, False)1203 def test_equiv_true(self):1204 rddl = '''1205 domain my_test {1206 pvariables { 1207 p : { state-fluent, bool, default = true };1208 q : { state-fluent, bool, default = true };1209 a : { action-fluent, bool, default = false }; 1210 };1211 cpfs { p' = p <=> q; };1212 reward = 0;1213 }1214 non-fluents my_test_empty { domain = my_test; }1215 instance my_test_inst { domain = my_test; init-state { a; }; }1216 '''1217 conv = Converter()1218 conv.convert_str(rddl)1219 conv.world.step()1220 p = conv.world.getState(WORLD, 'p', unique=True)1221 q = conv.world.getState(WORLD, 'q', unique=True)1222 self.assertEqual(p, True)1223 self.assertEqual(q, True)1224 def test_equiv_true2(self):1225 rddl = '''1226 domain my_test {1227 pvariables { 1228 p : { state-fluent, bool, default = false };1229 q : { state-fluent, bool, default = false };1230 a : { action-fluent, bool, default = false }; 1231 };1232 cpfs { p' = p <=> q; };1233 reward = 0;1234 }1235 non-fluents my_test_empty { domain = my_test; }1236 instance my_test_inst { domain = my_test; init-state { a; }; }1237 '''1238 conv = Converter()1239 conv.convert_str(rddl)1240 conv.world.step()1241 p = conv.world.getState(WORLD, 'p', unique=True)1242 q = conv.world.getState(WORLD, 'q', unique=True)1243 self.assertEqual(p, True)1244 self.assertEqual(q, False)1245 def test_equiv_true_const(self):1246 rddl = '''1247 domain my_test {1248 pvariables { 1249 p : { state-fluent, bool, default = false };1250 a : { action-fluent, bool, default = false }; 1251 };1252 cpfs { p' = p <=> 0.0; };1253 reward = 0;1254 }1255 non-fluents my_test_empty { domain = my_test; }1256 instance my_test_inst { domain = my_test; init-state { a; }; }1257 '''1258 conv = Converter()1259 conv.convert_str(rddl)1260 p = conv.world.getState(WORLD, 'p', unique=True)1261 self.assertEqual(p, False)1262 conv.world.step()1263 p = conv.world.getState(WORLD, 'p', unique=True)1264 self.assertEqual(p, True)1265 def test_equiv_true_consts(self):1266 rddl = '''1267 domain my_test {1268 pvariables { 1269 p : { state-fluent, bool, default = false };1270 a : { action-fluent, bool, default = false }; 1271 };1272 cpfs { p' = -1 <=> 1.0; }; // both have true boolean value1273 reward = 0;1274 }1275 non-fluents my_test_empty { domain = my_test; }1276 instance my_test_inst { domain = my_test; init-state { a; }; }1277 '''1278 conv = Converter()1279 conv.convert_str(rddl)1280 p = conv.world.getState(WORLD, 'p', unique=True)1281 self.assertEqual(p, False)1282 conv.world.step()1283 p = conv.world.getState(WORLD, 'p', unique=True)1284 self.assertEqual(p, True)1285 def test_equiv_self(self):1286 rddl = '''1287 domain my_test {1288 pvariables { 1289 p : { state-fluent, bool, default = false };1290 a : { action-fluent, bool, default = false }; 1291 };1292 cpfs { p' = p <=> p; };1293 reward = 0;1294 }1295 non-fluents my_test_empty { domain = my_test; }1296 instance my_test_inst { domain = my_test; init-state { a; }; }1297 '''1298 conv = Converter()1299 conv.convert_str(rddl)1300 p = conv.world.getState(WORLD, 'p', unique=True)1301 self.assertEqual(p, False)1302 conv.world.step()1303 p = conv.world.getState(WORLD, 'p', unique=True)1304 self.assertEqual(p, True)1305 def test_equiv_triple(self):1306 rddl = '''1307 domain my_test {1308 pvariables { 1309 p : { state-fluent, bool, default = false };1310 q : { state-fluent, bool, default = false };1311 r : { state-fluent, bool, default = true };1312 a : { action-fluent, bool, default = false }; 1313 };1314 cpfs { p' = (p <=> q) <=> r; };1315 reward = 0;1316 }1317 non-fluents my_test_empty { domain = my_test; }1318 instance my_test_inst { domain = my_test; init-state { a; }; }1319 '''1320 conv = Converter()1321 conv.convert_str(rddl)1322 p = conv.world.getState(WORLD, 'p', unique=True)1323 q = conv.world.getState(WORLD, 'q', unique=True)1324 r = conv.world.getState(WORLD, 'r', unique=True)1325 self.assertEqual(p, False)1326 self.assertEqual(q, False)1327 self.assertEqual(r, True)1328 conv.world.step()1329 p = conv.world.getState(WORLD, 'p', unique=True)1330 self.assertEqual(p, True)1331 def test_equiv_rel(self):1332 rddl = '''1333 domain my_test {1334 pvariables { 1335 p : { state-fluent, bool, default = false };1336 q : { state-fluent, int, default = 1 };1337 a : { action-fluent, bool, default = false }; 1338 };1339 cpfs { p' = (q >= 2) <=> (q < 0); };1340 reward = 0;1341 }1342 non-fluents my_test_empty { domain = my_test; }1343 instance my_test_inst { domain = my_test; init-state { a; }; }1344 '''1345 conv = Converter()1346 conv.convert_str(rddl)1347 p = conv.world.getState(WORLD, 'p', unique=True)1348 self.assertEqual(p, False)1349 conv.world.step()1350 p = conv.world.getState(WORLD, 'p', unique=True)1351 q = conv.world.getState(WORLD, 'q', unique=True)1352 self.assertEqual(p, True)1353 self.assertEqual(q, 1)1354 def test_imply_false(self):1355 rddl = '''1356 domain my_test {1357 pvariables { 1358 p : { state-fluent, bool, default = true };1359 q : { state-fluent, bool, default = false };1360 a : { action-fluent, bool, default = false }; 1361 };1362 cpfs { p' = p => q; };1363 reward = 0;1364 }1365 non-fluents my_test_empty { domain = my_test; }1366 instance my_test_inst { domain = my_test; init-state { a; }; }1367 '''1368 conv = Converter()1369 conv.convert_str(rddl)1370 conv.world.step()1371 p = conv.world.getState(WORLD, 'p', unique=True)1372 q = conv.world.getState(WORLD, 'q', unique=True)1373 self.assertEqual(p, False)1374 self.assertEqual(q, False)1375 def test_imply_false_const(self):1376 rddl = '''1377 domain my_test {1378 pvariables { 1379 p : { state-fluent, bool, default = true };1380 a : { action-fluent, bool, default = false }; 1381 };1382 cpfs { p' = p => 0.0; };1383 reward = 0;1384 }1385 non-fluents my_test_empty { domain = my_test; }1386 instance my_test_inst { domain = my_test; init-state { a; }; }1387 '''1388 conv = Converter()1389 conv.convert_str(rddl)1390 p = conv.world.getState(WORLD, 'p', unique=True)1391 self.assertEqual(p, True)1392 conv.world.step()1393 p = conv.world.getState(WORLD, 'p', unique=True)1394 self.assertEqual(p, False)1395 def test_imply_false_consts(self):1396 rddl = '''1397 domain my_test {1398 pvariables { 1399 p : { state-fluent, bool, default = true };1400 a : { action-fluent, bool, default = false }; 1401 };1402 cpfs { p' = -1 => 0.0; };1403 reward = 0;1404 }1405 non-fluents my_test_empty { domain = my_test; }1406 instance my_test_inst { domain = my_test; init-state { a; }; }1407 '''1408 conv = Converter()1409 conv.convert_str(rddl)1410 p = conv.world.getState(WORLD, 'p', unique=True)1411 self.assertEqual(p, True)1412 conv.world.step()1413 p = conv.world.getState(WORLD, 'p', unique=True)1414 self.assertEqual(p, False)1415 def test_imply_true(self):1416 rddl = '''1417 domain my_test {1418 pvariables { 1419 p : { state-fluent, bool, default = true };1420 q : { state-fluent, bool, default = true };1421 a : { action-fluent, bool, default = false }; 1422 };1423 cpfs { p' = p => q; };1424 reward = 0;1425 }1426 non-fluents my_test_empty { domain = my_test; }1427 instance my_test_inst { domain = my_test; init-state { a; }; }1428 '''1429 conv = Converter()1430 conv.convert_str(rddl)1431 conv.world.step()1432 p = conv.world.getState(WORLD, 'p', unique=True)1433 q = conv.world.getState(WORLD, 'q', unique=True)1434 self.assertEqual(p, True)1435 self.assertEqual(q, True)1436 def test_imply_true2(self):1437 rddl = '''1438 domain my_test {1439 pvariables { 1440 p : { state-fluent, bool, default = false };1441 q : { state-fluent, bool, default = false };1442 a : { action-fluent, bool, default = false }; 1443 };1444 cpfs { p' = p => q; };1445 reward = 0;1446 }1447 non-fluents my_test_empty { domain = my_test; }1448 instance my_test_inst { domain = my_test; init-state { a; }; }1449 '''1450 conv = Converter()1451 conv.convert_str(rddl)1452 conv.world.step()1453 p = conv.world.getState(WORLD, 'p', unique=True)1454 q = conv.world.getState(WORLD, 'q', unique=True)1455 self.assertEqual(p, True)1456 self.assertEqual(q, False)1457 def test_imply_true3(self):1458 rddl = '''1459 domain my_test {1460 pvariables { 1461 p : { state-fluent, bool, default = false };1462 q : { state-fluent, bool, default = true };1463 a : { action-fluent, bool, default = false }; 1464 };1465 cpfs { p' = p => q; };1466 reward = 0;1467 }1468 non-fluents my_test_empty { domain = my_test; }1469 instance my_test_inst { domain = my_test; init-state { a; }; }1470 '''1471 conv = Converter()1472 conv.convert_str(rddl)1473 conv.world.step()1474 p = conv.world.getState(WORLD, 'p', unique=True)1475 q = conv.world.getState(WORLD, 'q', unique=True)1476 self.assertEqual(p, True)1477 self.assertEqual(q, True)1478 def test_imply_true_const(self):1479 rddl = '''1480 domain my_test {1481 pvariables { 1482 p : { state-fluent, bool, default = false };1483 a : { action-fluent, bool, default = false }; 1484 };1485 cpfs { p' = p => 1.0; };1486 reward = 0;1487 }1488 non-fluents my_test_empty { domain = my_test; }1489 instance my_test_inst { domain = my_test; init-state { a; }; }1490 '''1491 conv = Converter()1492 conv.convert_str(rddl)1493 p = conv.world.getState(WORLD, 'p', unique=True)1494 self.assertEqual(p, False)1495 conv.world.step()1496 p = conv.world.getState(WORLD, 'p', unique=True)1497 self.assertEqual(p, True)1498 def test_imply_true_const2(self):1499 rddl = '''1500 domain my_test {1501 pvariables { 1502 p : { state-fluent, bool, default = false };1503 a : { action-fluent, bool, default = false }; 1504 };1505 cpfs { p' = p => 0; };1506 reward = 0;1507 }1508 non-fluents my_test_empty { domain = my_test; }1509 instance my_test_inst { domain = my_test; init-state { a; }; }1510 '''1511 conv = Converter()1512 conv.convert_str(rddl)1513 p = conv.world.getState(WORLD, 'p', unique=True)1514 self.assertEqual(p, False)1515 conv.world.step()1516 p = conv.world.getState(WORLD, 'p', unique=True)1517 self.assertEqual(p, True)1518 def test_imply_true_const3(self):1519 rddl = '''1520 domain my_test {1521 pvariables { 1522 p : { state-fluent, bool, default = false };1523 a : { action-fluent, bool, default = false }; 1524 };1525 cpfs { p' = 0 => p; };1526 reward = 0;1527 }1528 non-fluents my_test_empty { domain = my_test; }1529 instance my_test_inst { domain = my_test; init-state { a; }; }1530 '''1531 conv = Converter()1532 conv.convert_str(rddl)1533 p = conv.world.getState(WORLD, 'p', unique=True)1534 self.assertEqual(p, False)1535 conv.world.step()1536 p = conv.world.getState(WORLD, 'p', unique=True)1537 self.assertEqual(p, True)1538 def test_imply_true_consts(self):1539 rddl = '''1540 domain my_test {1541 pvariables { 1542 p : { state-fluent, bool, default = false };1543 a : { action-fluent, bool, default = false }; 1544 };1545 cpfs { p' = -1 => 1.0; };1546 reward = 0;1547 }1548 non-fluents my_test_empty { domain = my_test; }1549 instance my_test_inst { domain = my_test; init-state { a; }; }1550 '''1551 conv = Converter()1552 conv.convert_str(rddl)1553 p = conv.world.getState(WORLD, 'p', unique=True)1554 self.assertEqual(p, False)1555 conv.world.step()1556 p = conv.world.getState(WORLD, 'p', unique=True)1557 self.assertEqual(p, True)1558 def test_imply_triple(self):1559 rddl = '''1560 domain my_test {1561 pvariables { 1562 p : { state-fluent, bool, default = false };1563 q : { state-fluent, bool, default = true };1564 r : { state-fluent, bool, default = false };1565 a : { action-fluent, bool, default = false }; 1566 };1567 cpfs { p' = (p => q) => r; }; 1568 reward = 0;1569 }1570 non-fluents my_test_empty { domain = my_test; }1571 instance my_test_inst { domain = my_test; init-state { a; }; }1572 '''1573 conv = Converter()1574 conv.convert_str(rddl)1575 p = conv.world.getState(WORLD, 'p', unique=True)1576 q = conv.world.getState(WORLD, 'q', unique=True)1577 r = conv.world.getState(WORLD, 'r', unique=True)1578 self.assertEqual(p, False)1579 self.assertEqual(q, True)1580 self.assertEqual(r, False)1581 conv.world.step()1582 p = conv.world.getState(WORLD, 'p', unique=True)1583 self.assertEqual(p, False)1584 def test_imply_rel(self):1585 rddl = '''1586 domain my_test {1587 pvariables { 1588 p : { state-fluent, bool, default = true };1589 q : { state-fluent, int, default = 2 };1590 a : { action-fluent, bool, default = false }; 1591 };1592 cpfs { p' = (q >= 2) => (q < 0); };1593 reward = 0;1594 }1595 non-fluents my_test_empty { domain = my_test; }1596 instance my_test_inst { domain = my_test; init-state { a; }; }1597 '''1598 conv = Converter()1599 conv.convert_str(rddl)1600 p = conv.world.getState(WORLD, 'p', unique=True)1601 self.assertEqual(p, True)1602 conv.world.step()1603 p = conv.world.getState(WORLD, 'p', unique=True)1604 q = conv.world.getState(WORLD, 'q', unique=True)1605 self.assertEqual(p, False)1606 self.assertEqual(q, 2)1607 def test_imply_rel2(self):1608 rddl = '''1609 domain my_test {1610 pvariables { 1611 p : { state-fluent, bool, default = true };1612 q : { state-fluent, int, default = 2 };1613 a : { action-fluent, bool, default = false }; 1614 };1615 cpfs { p' = p | (q >= 2) => (q < 0); };1616 reward = 0;1617 }1618 non-fluents my_test_empty { domain = my_test; }1619 instance my_test_inst { domain = my_test; init-state { a; }; }1620 '''1621 conv = Converter()1622 conv.convert_str(rddl)1623 p = conv.world.getState(WORLD, 'p', unique=True)1624 self.assertEqual(p, True)1625 conv.world.step()1626 p = conv.world.getState(WORLD, 'p', unique=True)1627 q = conv.world.getState(WORLD, 'q', unique=True)1628 self.assertEqual(p, False)1629 self.assertEqual(q, 2)1630 def test_imply_action(self):1631 rddl = '''1632 domain my_test {1633 pvariables { 1634 p : { state-fluent, bool, default = true };1635 q : { state-fluent, bool, default = true };1636 a : { action-fluent, bool, default = false }; 1637 };1638 cpfs { p' = q => ~ a; };1639 reward = 0;1640 }1641 non-fluents my_test_empty { domain = my_test; }1642 instance my_test_inst { domain = my_test; init-state { a; }; }1643 '''1644 conv = Converter()1645 conv.convert_str(rddl)1646 p = conv.world.getState(WORLD, 'p', unique=True)1647 q = conv.world.getState(WORLD, 'q', unique=True)1648 self.assertEqual(p, True)1649 self.assertEqual(q, True)1650 conv.world.step()1651 p = conv.world.getState(WORLD, 'p', unique=True)1652 self.assertEqual(p, False)1653 def test_not_imply_action(self):1654 rddl = '''1655 domain my_test {1656 pvariables { 1657 p : { state-fluent, bool, default = false };1658 q : { state-fluent, bool, default = true };1659 a : { action-fluent, bool, default = false }; 1660 };1661 cpfs { p' = ~(q => ~ a); };1662 reward = 0;1663 }1664 non-fluents my_test_empty { domain = my_test; }1665 instance my_test_inst { domain = my_test; init-state { a; }; }1666 '''1667 conv = Converter()1668 conv.convert_str(rddl)1669 p = conv.world.getState(WORLD, 'p', unique=True)1670 q = conv.world.getState(WORLD, 'q', unique=True)1671 self.assertEqual(p, False)1672 self.assertEqual(q, True)1673 conv.world.step()1674 p = conv.world.getState(WORLD, 'p', unique=True)1675 self.assertEqual(p, True)1676if __name__ == '__main__':...
model_palu.py
Source:model_palu.py
1#!/usr/bin/env python2# coding: utf-83##########################################utils librairies ##########################################4import pandas as pd5import numpy as np6import warnings7warnings.filterwarnings("ignore")8from sklearn import preprocessing9from imblearn.over_sampling import SMOTE10import matplotlib.pyplot as plt 11from matplotlib.ticker import NullFormatter12plt.rc("font", size=14)13from sklearn.linear_model import LogisticRegression14import seaborn as sns15sns.set(style="white")16sns.set(style="whitegrid", color_codes=True)17from sklearn.model_selection import train_test_split18from sklearn import metrics19from sklearn.tree import DecisionTreeClassifier20import tensorflow as tf21from sklearn.ensemble import RandomForestClassifier22from sklearn.metrics import confusion_matrix23from sklearn.metrics import classification_report24from sklearn import metrics25from sklearn.neural_network import MLPClassifier26from sklearn.preprocessing import StandardScaler27from sklearn.linear_model import LogisticRegression28import random29random.seed(1000)30from sklearn.svm import SVC 31from sklearn.metrics import classification_report, confusion_matrix32from sklearn.metrics import roc_auc_score33from sklearn.metrics import roc_curve34##########################################chargement des données ######################################################35def input_data(path):36 palu=pd.read_excel(path)37 return palu38#################################################################################################################39def creat_dataFrame(data):40 paluu=pd.DataFrame(data, columns = ['PPOIDS', 'TEMPERATURE', 'S_M8_APPETIT', 'S_FATIGUE', 'S_ARTHRALGI',41 'S_T_DIGESTIF', 'S_VERTIGE', 'S_FRISSON', 'S_MYALGIE', 'S_DABDO', 42 'S_VOMISS', 'S_NAUSEE', 'S_CEPHALE', 'S_FIEVRE','TDR'])43 return paluu44###########################Split##########################################################################################45# Nous séparons notre variable cible en deux parties. Une partie pour entrainer y_train et une partie pour tester y_test46def split_data(palu, paluu):47 X = paluu48 y = palu['Diagnostic']49 X1_train, MX_test, y1_train, My_test = train_test_split(X, y, random_state=0)50 return X1_train, MX_test, y1_train, My_test 51#####################Suréchantillonnage avec l'algorithme SMOTE################################################################52def smote(X1_train,y1_train):53 Re= SMOTE(random_state=0)54 #X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.3, random_state=0)55 columns = X1_train.columns56 Re_data_X1,Re_data_y1=Re.fit_sample(X1_train, y1_train)57 Re_data_X1 = pd.DataFrame(data=Re_data_X1 ,columns= columns)58 X_train =Re_data_X159 y_train=Re_data_y160 return X_train, y_train61##############################################################################################################################62#def log_regression((X_train,y_train,MX_test,My_test):63 # LR= LogisticRegression(random_state=0,solver='lbfgs',max_iter=1000)64 # LR.fit(X_train, y_train)65 # palu_pred = LR.predict(MX_test)66 #palu_pred=pd.DataFrame(palu_pred)67 #score = metrics.accuracy_score(My_test, y_pred1)68############################################################################################################################69def log_regression(X_train,y_train,MX_test,My_test):70 LR= LogisticRegression(random_state=0,solver='lbfgs',max_iter=1000)71 LR.fit(X_train, y_train)72 palu_pred = LR.predict(MX_test)73 palu_pred=pd.DataFrame(palu_pred)74 score = metrics.accuracy_score(My_test, palu_pred)75 report= classification_report(My_test, palu_pred)76 score=print(score)77 report=print(report)78 return palu_pred, score,report79################################ Decision TreeClassifier #############################################################################80def DecisionTreeClassifier(X_train,y_train,MX_test,My_test):81 from sklearn.tree import DecisionTreeClassifier82 epochs = [1,5,10, 15, 20,25]# List to store the average RMSE for each value of max_depth:83 accuracy = []84 for n in epochs:85 clf = DecisionTreeClassifier(max_depth = n, random_state = 0)86 clf.fit(X_train, y_train) 87 palu_pred=clf.predict(MX_test)88 score = metrics.accuracy_score(My_test, palu_pred)89 accuracy.append(score)90 ind=accuracy.index(max(accuracy))91 best_score=accuracy[ind]92 report= classification_report(My_test, palu_pred)93 score=print(score)94 report=print(report)95 return palu_pred, score,report96 97 98#################################################### RandomForestClassifier ####################################################99def RandomForestClassifier(X_train,y_train,MX_test,My_test):100 from sklearn.ensemble import RandomForestClassifier101 epochs = [10,50,100, 200, 300,400, 500]# List to store the average RMSE for each value of max_depth:102 accuracy = []103 for n in epochs:104 rf=RandomForestClassifier(n_estimators= n)105 #Train the model using the training sets y_pred=clf.predict(X_test)106 rf.fit(X_train,y_train)107 y_pred=rf.predict(MX_test)108 score = metrics.accuracy_score(My_test, y_pred)109 accuracy.append(score)110 ind=accuracy.index(max(accuracy))111 best_score=accuracy[ind]112 report=classification_report(My_test, y_pred)113 score=print(score)114 report=print(report)115 return y_pred, score,report116############################################ SVM Classifier kernel= linear ################################################################117def linear_svm(X_train,y_train,MX_test,My_test):118 svclassifier1 = SVC(kernel='linear', gamma='auto',probability=True) 119 svclassifier1.fit(X_train, y_train) 120 y_pred1 = svclassifier1.predict(MX_test) 121 score=metrics.accuracy_score(My_test, y_pred1)122 #matrix=confusion_matrix(My_test, y_pred1)123 report=classification_report(My_test, y_pred1)124 score=print(score)125 report=print(report)126 return y_pred1, score, report127###########################################SVM Classifier kernel= sigmoid ################################################################128def sigmoid_svm(X_train,y_train,MX_test,My_test):129 svclassifier2 = SVC(kernel='sigmoid', gamma='auto',probability=True) 130 svclassifier2.fit(X_train, y_train) 131 y_pred2 = svclassifier2.predict(MX_test) 132 score=metrics.accuracy_score(My_test, y_pred2)133 #matrix=confusion_matrix(My_test, y_pred1)134 rport=classification_report(My_test, y_pred2)135 return y_pred2, score, rport136###################################################SVM Classifier kernel= gaussien ####################################################137def gaussien_svm(X_train,y_train,MX_test,My_test):138 svclassifier = SVC(kernel='rbf', gamma='auto',probability=True) 139 svclassifier.fit(X_train, y_train) 140 y_pred = svclassifier.predict(MX_test) 141 score=metrics.accuracy_score(My_test, y_pred)142 #matrix=confusion_matrix(My_test, y_pred1)143 report=classification_report(My_test, y_pred)144 return y_pred,score,report145############################################## MLPClassifier #################################################################146def MLPClassifier(X_train,y_train,MX_test,My_test):147 from sklearn.neural_network import MLPClassifier148 from sklearn.preprocessing import StandardScaler149 epochs = [1,5,10, 15, 20,25]# List to store the average RMSE for each value of max_depth:150 scaler = StandardScaler()151 scaler.fit(X_train)152 X1_train = scaler.transform(X_train)153 X1_test = scaler.transform(MX_test)154 accuracy = []155 for n in epochs:156 #clf = DecisionTreeClassifier(max_depth = depth, random_state = 0)157 mlp = MLPClassifier(hidden_layer_sizes=(15,15,n),max_iter=200, solver='lbfgs')158 #clf.fit(X_train, y_train) 159 mlp.fit(X1_train,y_train)160 #y_pred1=clf.predict(MX_test)161 predictions = mlp.predict(X1_test)162 score = metrics.accuracy_score(My_test, predictions)163 accuracy.append(score)164 ind=accuracy.index(max(accuracy))165 best_score=accuracy[ind]166 reported=classification_report(My_test, predictions)167 score=print(best_score)168 reported=print(reported)169 return predictions,best_score,reported170#########################################################################################################################171def plot_data(data,predicted):172# plot with various axes scales173 plt.figure(figsize=[8,4],dpi=200)174 logit_roc_auc = roc_auc_score(data, predicted)175 fpr, tpr, thresholds = roc_curve(data,predicted)176 plt.plot(fpr, tpr, label='logistic regression(area = %0.2f)' % logit_roc_auc)177 plt.plot([0, 1], [0, 1],'r--')178 plt.xlim([0.0, 1.0])179 plt.ylim([0.0, 1.05])180 plt.xlabel('False Positive Rate')181 plt.ylabel('True Positive Rate')182 plt.title('Courbe ROC')183 plt.legend(loc="lower right")184 #plt.savefig('Log_ROC')185 plt.show()...
test.py
Source:test.py
1##########################################utils librairies ##########################################2import pandas as pd3import numpy as np4from sklearn import preprocessing5import matplotlib.pyplot as plt 6from matplotlib.ticker import NullFormatter7plt.rc("font", size=14)8from sklearn.linear_model import LogisticRegression9import seaborn as sns10sns.set(style="white")11sns.set(style="whitegrid", color_codes=True)12from sklearn.model_selection import train_test_split13from sklearn import metrics14from sklearn.tree import DecisionTreeClassifier15import tensorflow as tf16from sklearn.ensemble import RandomForestClassifier17from sklearn.metrics import confusion_matrix18from sklearn.metrics import classification_report19from sklearn import metrics20from sklearn.neural_network import MLPClassifier21from sklearn.preprocessing import StandardScaler22from sklearn.linear_model import LogisticRegression23import random24random.seed(1000)25from sklearn.svm import SVC 26from sklearn.metrics import classification_report, confusion_matrix27##########################################chargement des données ######################################################28def input_data(path):29 palu=pd.read_csv(path)30 paluu=pd.DataFrame(palu, columns = ['PPOIDS', 'TEMPERATURE', 'S_M8_APPETIT', 'S_FATIGUE', 'S_ARTHRALGI',31 'S_T_DIGESTIF', 'S_VERTIGE', 'S_FRISSON', 'S_MYALGIE', 'S_DABDO', 32 'S_VOMISS', 'S_NAUSEE', 'S_CEPHALE', 'S_FIEVRE','TDR'])33 return paluu34###########################Split##########################################################################################35# Nous séparons notre variable cible en deux parties. Une partie pour entrainer y_train et une partie pour tester y_test36def split_data(data):37 X = data38 y = data['Diagnostic']39 X1_train, MX_test, y1_train, My_test = train_test_split(X, y, random_state=0)40 return X1_train, MX_test, y1_train, My_test 41#####################Suréchantillonnage avec l'algorithme SMOTE################################################################42def smote(data1,data2):43 X1 = data144 y1 =palu['Diagnostic']45 y1= pd.DataFrame(y1)46 Re= SMOTE(random_state=0)47 #X1_train, X1_test, y1_train, y1_test = train_test_split(X1, y1, test_size=0.3, random_state=0)48 columns = X1_train.columns49 Re_data_X1,Re_data_y1=Re.fit_sample(X1_train, y1_train)50 Re_data_X1 = pd.DataFrame(data=Re_data_X1 ,columns= columns)51 X_train =Re_data_X152 y_train=Re_data_y153 return X_train, y_train54##############################################################################################################################55#def log_regression((X_train,y_train,MX_test,My_test):56 # LR= LogisticRegression(random_state=0,solver='lbfgs',max_iter=1000)57 # LR.fit(X_train, y_train)58 # palu_pred = LR.predict(MX_test)59 #palu_pred=pd.DataFrame(palu_pred)60 #score = metrics.accuracy_score(My_test, y_pred1)61############################################################################################################################62def log_regression(X_train,y_train,MX_test,My_test):63 LR= LogisticRegression(random_state=0,solver='lbfgs',max_iter=1000)64 LR.fit(X_train, y_train)65 palu_pred = LR.predict(MX_test)66 palu_pred=pd.DataFrame(palu_pred)67 return palu_pred68################################ Decision TreeClassifier #############################################################################69def DecisionTreeClassifier(X_train,y_train,MX_test,My_test):70 epochs = [1,5,10, 15, 20,25]# List to store the average RMSE for each value of max_depth:71 accuracy = []72 for depth in epochs:73 clf = DecisionTreeClassifier(max_depth = depth, random_state = 0)74 clf.fit(X_train, y_train) 75 y_pred1=clf.predict(MX_test)76 score = metrics.accuracy_score(My_test, y_pred1)77 accuracy.append(score)78 ind=accuracy.index(max(accuracy))79 best_score=accuracy[ind]80 return accuracy, best_score81#################################################### RandomForestClassifier ####################################################82def RandomForestClassifier(X_train,y_train,MX_test,My_test):83 rf=RandomForestClassifier()84 rf.fit(X_train,y_train)85 y_pred2=rf.predict(MX_test)86 epochs = [10,50,100, 200, 300,400, 500]# List to store the average RMSE for each value of max_depth:87 accuracy = []88 for n in epochs:89 rf=RandomForestClassifier(n_estimators= n)90 #Train the model using the training sets y_pred=clf.predict(X_test)91 rf.fit(X_train,y_train)92 y_pred2=rf.predict(MX_test)93 score = metrics.accuracy_score(My_test, y_pred2)94 accuracy.append(score)95 ind=accuracy.index(max(accuracy))96 best_score=accuracy[ind]97 report=classification_report(My_test, y_pred1)98 return best_score, report99############################################ SVM Classifier kernel= linear ################################################################100def linear_svm(X_train,y_train,MX_test,My_test):101 svclassifier1 = SVC(kernel='linear', gamma='auto',probability=True) 102 svclassifier1.fit(X_train, y_train) 103 y_pred1 = svclassifier1.predict(MX_test) 104 Accuracy=metrics.accuracy_score(My_test, y_pred1)105 #matrix=confusion_matrix(My_test, y_pred1)106 report=classification_report(My_test, y_pred1)107 return Accuracy,report108###########################################SVM Classifier kernel= sigmoid ################################################################109def sigmoid_svm(X_train,y_train,MX_test,My_test):110 svclassifier2 = SVC(kernel='sigmoid', gamma='auto',probability=True) 111 svclassifier2.fit(X_train, y_train) 112 y_pred2 = svclassifier2.predict(MX_test) 113 Accuracy=metrics.accuracy_score(My_test, y_pred2)114 #matrix=confusion_matrix(My_test, y_pred1)115 report=classification_report(My_test, y_pred2)116 return Accuracy,report117###################################################SVM Classifier kernel= gaussien ####################################################118def gaussien_svm(X_train,y_train,MX_test,My_test):119 svclassifier = SVC(kernel='rbf', gamma='auto',probability=True) 120 svclassifier.fit(X_train, y_train) 121 y_pred = svclassifier.predict(MX_test) 122 Accuracy=metrics.accuracy_score(My_test, y_pred)123 #matrix=confusion_matrix(My_test, y_pred1)124 report=classification_report(My_test, y_pred)125 return Accuracy,report126############################################## MLPClassifier #################################################################127def MLPClassifier(X_train,y_train,MX_test,My_test):128 epochs = [1,5,10, 15, 20,25]# List to store the average RMSE for each value of max_depth:129 scaler = StandardScaler()130 scaler.fit(X_train)131 X1_train = scaler.transform(X_train)132 X1_test = scaler.transform(MX_test)133 accuracy = []134 for n in epochs:135 #clf = DecisionTreeClassifier(max_depth = depth, random_state = 0)136 mlp = MLPClassifier(hidden_layer_sizes=(15,15,n),max_iter=200, solver='lbfgs')137 #clf.fit(X_train, y_train) 138 mlp.fit(X1_train,y_train)139 #y_pred1=clf.predict(MX_test)140 predictions = mlp.predict(X1_test)141 score = metrics.accuracy_score(My_test, predictions)142 accuracy.append(score)143 ind=accuracy.index(max(accuracy))144 best_score=accuracy[ind]145 return accuracy, best_score146#########################################################################################################################147def plot_data(data,predicted):148# plot with various axes scales149 plt.figure(figsize=[8,4],dpi=700)150 #pred= predicted.predict_proba(data)[:,1]151 fpr, tpr, thresholds = roc_curve(data,predicted)152 plt.figure()153 plt.plot(fpr, tpr, label='logistic regression(area = %0.2f)' % logit_roc_auc)154 plt.plot([0, 1], [0, 1],'r--')155 plt.xlim([0.0, 1.0])156 plt.ylim([0.0, 1.05])157 plt.xlabel('False Positive Rate')158 plt.ylabel('True Positive Rate')159 plt.title('Courbe ROC')160 plt.legend(loc="lower right")161 #plt.savefig('Log_ROC')...
predict.py
Source:predict.py
1# This Python 3 environment comes with many helpful analytics libraries installed2# For example, here's several helpful packages to load in 3import pandas as pd4import numpy as np5import random6# submit CSV to hdfs7import submittools as sub8import re 9# Input data files are available with function competitionData10from Turing import competitionData, userData11train=pd.read_csv(competitionData('/upload-dataset/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1Êý¾Ý¼¯/train.csv'))12test=pd.read_csv(competitionData('/upload-dataset/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1Êý¾Ý¼¯/test.csv'))13sample=pd.read_csv(competitionData('/upload-dataset/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1Êý¾Ý¼¯/sample.csv'))14lost_user_info=pd.read_csv(competitionData('/upload-dataset/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1/¶«·½¹úÐű¸ßУ´óÊý¾ÝËã·¨Ìâ1Êý¾Ý¼¯/lost_user_info.csv'))15# print("ѵÁ·¼¯ÐÐÊýºÍÁÐÊý£¡")16# print(train.shape[0],train.shape[1])17#Êý¾ÝÇåÏ´18# my_train=train[train.dtypes[train.dtypes!=np.object].index]19my_train = train.copy()20# print(my_train)#ѵÁ·¼¯×Ô±äÁ¿21re_train=my_train.pop('IS_LOST')22#print(re_train)#ѵÁ·¼¯Óû§ÊÇ·ñÁ÷ʧ½á¹û23# print(my_train.columns.values.tolist())24del my_train['USER_ID']25del my_train['USER_ID_OLD']26del my_train['CUST_ID']27del my_train['DEVICE_NUMBER']28# del my_train['SERVICE_TYPE']29# del my_train['SERVICE_TYPE_OLD']30del my_train['LEVEL_A']31del my_train['CYCLE_ID'] #ɾ³ýתÍøʱ¼ä32#ɾ³ý´óÊý³¢ÊÔ33# del my_train['PRODUCT_ID']34# del my_train['PRODUCT_CLASS']35my_train.loc[my_train[my_train['USER_STATUS'] == '**'].index,['USER_STATUS']] = 1136my_train.loc[my_train[my_train['TRANS_ID'] != 0].index,['TRANS_ID']] = 137#SERVICE_TYPE±£Áô·Ç×Ö·û´®38STtmplist = []39for seroldtmp in my_train['SERVICE_TYPE']:40 STtmplist.append(re.sub("\D","",seroldtmp))41my_train['SERVICE_TYPE'] = STtmplist 42#SERVICE_TYPE_OLD±£Áô·Ç×Ö·û´®43STOtmplist = []44for seroldtmp in my_train['SERVICE_TYPE_OLD']:45 STOtmplist.append(re.sub("\D","",seroldtmp))46my_train['SERVICE_TYPE_OLD'] = STOtmplist 47# print(my_train['SERVICE_TYPE'])48my_train['TOTAL_TIMES'] = round(my_train['TOTAL_TIMES'])49my_train['INTER_TIMES'] = round(my_train['INTER_TIMES'])50my_train['TOTAL_SMS'] = round(my_train['TOTAL_SMS'])51my_train['RATIO_FLUX'] = round(my_train['RATIO_FLUX'])52my_train['TOTAL_FLUX'] = round(my_train['TOTAL_FLUX'])53my_train['INTER_FLUX'] = round(my_train['INTER_FLUX'])54del my_train['FLUX_2G']55del my_train['FLUX_3G']56del my_train['FLUX_4G']57my_train['RATIO_FEE'] = round(my_train['RATIO_FEE'])58del my_train['TOTAL_FEE']59del my_train['TOTAL_TIME_VAR']60del my_train['IMEI']61del my_train['MANU_NAME']62del my_train['CHNL_ID']63#ɾ³ýÈÕÆÚ64del my_train['COMP_START_DATE']65del my_train['COMP_END_DATE']66del my_train['ACT_START_DATE']67del my_train['ACT_END_DATE']68# my_train = my_train.drop(my_train[my_train.CERT_AGE == 0].index)#ɾ³ýÄêÁäΪ0µÄÐÐ69# my_train.loc[my_train[my_train['CERT_AGE'] == 0].index,['CERT_AGE']] = random.randint(25,50) #ÐÞ¸ÄÄêÁäΪ0µÄÐÐ70# my_train['IMEI']/= 10000000000071# my_train.loc[my_train[my_train['ACT_START_DATE']>201806010].index,['ACT_START_DATE']] /= 1000000#ºÏԼʱ¼ä¹æ·¶»¯72# my_train.loc[my_train[my_train['ACT_END_DATE']>201806010].index,['ACT_END_DATE']] /= 1000000#ºÏԼʱ¼ä¹æ·¶»¯73# my_train.loc[my_train[my_train['CALL_RING']<0].index,['CALL_RING']] = 0 #½»ÍùȦ¸ºÊý¸ÄΪ074# del my_train['IS_ACCT_AFT']75my_test=test[my_train.columns]76#test²âÊÔ¼¯¹æ·¶»¯77#СÊýÈ¡Õû78my_test.loc[my_test[my_test['USER_STATUS'] == '**'].index,['USER_STATUS']] = 1179my_test.loc[my_test[my_test['TRANS_ID'] != 0].index,['TRANS_ID']] = 180my_test['TOTAL_TIMES'] = round(my_test['TOTAL_TIMES'])81my_test['INTER_TIMES'] = round(my_test['INTER_TIMES'])82my_test['TOTAL_SMS'] = round(my_test['TOTAL_SMS'])83my_test['RATIO_FLUX'] = round(my_test['RATIO_FLUX'])84my_test['TOTAL_FLUX'] = round(my_test['TOTAL_FLUX'])85my_test['INTER_FLUX'] = round(my_test['INTER_FLUX'])86my_test['RATIO_FEE'] = round(my_test['RATIO_FEE'])87#SERVICE_TYPE±£Áô·Ç×Ö·û´®88STtmplist = []89for seroldtmp in my_test['SERVICE_TYPE']:90 STtmplist.append(re.sub("\D","",seroldtmp))91my_test['SERVICE_TYPE'] = STtmplist 92#SERVICE_TYPE_OLD±£Áô·Ç×Ö·û´®93STOtmplist = []94for seroldtmp in my_test['SERVICE_TYPE_OLD']:95 STOtmplist.append(re.sub("\D","",seroldtmp))96my_test['SERVICE_TYPE_OLD'] = STOtmplist 97# my_test.loc[my_test[my_test['CERT_AGE'] == 0].index,['CERT_AGE']] = random.randint(25,50) #ÐÞ¸ÄÄêÁäΪ0µÄÐÐ98# my_test['IMEI'] /= 10000000000099# my_test.loc[my_test[my_test['ACT_START_DATE']>201806010].index,['ACT_START_DATE']] /= 1000000#ºÏԼʱ¼ä¹æ·¶»¯100# my_test.loc[my_test[my_test['ACT_END_DATE']>201806010].index,['ACT_END_DATE']] /= 1000000#ºÏԼʱ¼ä¹æ·¶»¯101# my_test.loc[my_test[my_test['CALL_RING']<0].index,['CALL_RING']] = 0 #½»ÍùȦ¸ºÊý¸ÄΪ0102print("ѵÁ·¼¯ÐÐÊýºÍÁÐÊý£¡")103print(my_train.shape[0],my_train.shape[1])104print("²âÊÔ¼¯ÐÐÊýºÍÁÐÊý£º")105print(my_test.shape[0],my_test.shape[1])106# print(my_test['CERT_AGE'])107print("ѵÁ·¼¯ÐÐÊýºÍÁÐÊý£¡")108print(my_train.shape[0],my_train.shape[1])109print(my_test.shape[0],my_test.shape[1])110# print(my_test['CERT_AGE'])hape[1])111print("²âÊÔ¼¯ÐÐÊýºÍÁÐÊý£º")112#·½·¨ËÄ ¾ö²ßÊ÷ÉÁÖ+Íø¸ñËÑË÷+KFlod½»²æÑéÖ¤113from sklearn.ensemble import RandomForestClassifier114from sklearn.grid_search import GridSearchCV115from sklearn.cross_validation import StratifiedKFold116random_forest_classifier = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',117 max_depth=20, max_features='auto', max_leaf_nodes=None,118 min_impurity_decrease=0.0, min_impurity_split=None,119 min_samples_leaf=1, min_samples_split=2,120 min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=-1,121 oob_score=False, random_state=False, verbose=0, warm_start=True)122parameter_grid = {'n_estimators': [37,39,42],123 'criterion': ['gini','entropy'],124 'max_features': [15,16,17],125 'warm_start':[True,False]}126cross_validation = StratifiedKFold(re_train, n_folds=10,shuffle=True,random_state=2000)127grid_search = GridSearchCV(random_forest_classifier,128 param_grid=parameter_grid,129 cv=cross_validation)130grid_search.fit(my_train, re_train)131predict = grid_search.predict(my_test)132submits=test[['USER_ID']]133# submits['IS_LOST']=prediction_xgb134submits['IS_LOST']=predict135submits['IS_LOST']=submits['IS_LOST'].apply(lambda x: int(1) if x>0.5 else int(0))136print(submits['IS_LOST'].value_counts())137try:138 sub.submit(submits, notebookId) #A°ñ139except NameError:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!