problem_test.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require 'svm_toolkit'
  2. require 'minitest/autorun'
  3. include SvmToolkit
  4. class TestProblem < MiniTest::Test
  5. def test_raise_exception
  6. assert_raises(ArgumentError) { Problem.from_array([], []) }
  7. assert_raises(ArgumentError) { Problem.from_array([], [1,2,3]) }
  8. assert_raises(ArgumentError) { Problem.from_array([[1],[2],[3]], [1,2]) }
  9. assert_raises(ArgumentError) { Problem.from_array([[1],[2,3],[4]], [1,2,3])}
  10. end
  11. def test_create_problem_from_array
  12. problem = Problem.from_array([[1,2],[2,3],[3,4]], [1,2,3])
  13. assert_equal(3, problem.size)
  14. assert_equal(2, problem.x[1][0].value)
  15. assert_equal(1, problem.y[0])
  16. end
  17. def test_rescale_problem
  18. problem = Problem.from_array(
  19. [[1,2,3], [1.5, 4, 3.1], [2.0, 6, 3.2]],
  20. [1, 0, 1]
  21. )
  22. assert_equal(3, problem.size)
  23. assert_equal(1, problem.x[0][0].value)
  24. assert_equal(2.0, problem.x[2][0].value)
  25. problem.rescale(-1.0, 1.0)
  26. assert_equal(3, problem.size)
  27. assert_equal(-1, problem.x[0][0].value)
  28. assert_equal(0, problem.x[1][0].value)
  29. assert_equal(1, problem.x[2][0].value)
  30. assert_equal(-1, problem.x[0][1].value)
  31. assert_equal(0, problem.x[1][1].value)
  32. assert_equal(1, problem.x[2][1].value)
  33. assert_equal(-1, problem.x[0][1].value)
  34. assert_equal(0, problem.x[1][1].value)
  35. assert_equal(1, problem.x[2][1].value)
  36. problem.rescale(0.0, 1.0)
  37. assert_equal(3, problem.size)
  38. assert_equal(0, problem.x[0][0].value)
  39. assert_equal(0.5, problem.x[1][0].value)
  40. assert_equal(1.0, problem.x[2][0].value)
  41. assert_equal(0, problem.x[0][1].value)
  42. assert_equal(0.5, problem.x[1][1].value)
  43. assert_equal(1.0, problem.x[2][1].value)
  44. assert_equal(0, problem.x[0][1].value)
  45. assert_equal(0.5, problem.x[1][1].value)
  46. assert_equal(1.0, problem.x[2][1].value)
  47. end
  48. def test_merge_problems
  49. problem1 = Problem.from_array(
  50. [[1,2,3], [1.5, 4, 3.1], [2.0, 6, 3.2]],
  51. [1, 0, 1]
  52. )
  53. problem2 = Problem.from_array(
  54. [[1,2,3,4]], [1]
  55. )
  56. # problems with differing numbers of features cannot be merged
  57. assert_raises(ArgumentError) { problem1.merge(problem2) }
  58. problem3 = Problem.from_array(
  59. [[1,2,3], [1.5, 4, 3.1], [2.0, 6, 3.2]],
  60. [1, 0, 1]
  61. )
  62. # confirm merging of two problems
  63. problem1.merge problem3
  64. problem4 = problem1.merge problem3
  65. assert_equal(6, problem4.size)
  66. [[1,2,3], [1.5, 4, 3.1], [2.0, 6, 3.2],
  67. [1,2,3], [1.5, 4, 3.1], [2.0, 6, 3.2]
  68. ].each_with_index do |instance, i|
  69. instance.each_with_index do |v, j|
  70. assert_equal(v, problem4.x[i][j].value)
  71. end
  72. end
  73. [1,0,1,1,0,1].each_with_index do |v, i|
  74. assert_equal(v, problem4.y[i])
  75. end
  76. end
  77. end