Ejemplo n.º 1
0
 def test_numberOfBricksTypes(self):
     bricks = list([LegoBrick(1, 1)])
     col = LegoBrickCollection()
     col.initialize(10, bricks)
     self.assertEqual(col.getNumberOfBricksTypes(), len(bricks))
     col = LegoBrickCollection()
     col.initialize(10, [])
     self.assertEqual(col.getNumberOfBricksTypes(), 0)
Ejemplo n.º 2
0
 def __createBrickCollection(self, area: int) -> LegoBrickCollection:
     bricks = []
     bricks.append(LegoBrick(2, 3))
     bricks.append(LegoBrick(3, 2))
     bricks.append(LegoBrick(2, 2))
     collection = LegoBrickCollection()
     collection.initialize(area, bricks, uniform=True)
     return collection
Ejemplo n.º 3
0
 def __createBrickLayout(self, width: int, height: int) -> LegoBrickLayout:
     bricks = []
     bricks.append(LegoBrick(1, 1))
     bricks.append(LegoBrick(1, 2))
     bricks.append(LegoBrick(2, 2))
     collection = LegoBrickCollection()
     collection.initialize(width * height, bricks, uniform=True)
     self.assertTrue(collection.isInitialized())
     layout = LegoBrickLayout()
     layout.initialize(width, height, collection)
     self.assertTrue(layout.isInitialized())
     return layout
Ejemplo n.º 4
0
 def test_copy(self):
     col = LegoBrickCollection()
     colCopy = col.copy()
     self.assertEqual(col.isInitialized(), colCopy.isInitialized())
     col.initialize(5, list([LegoBrick(1, 1)]), False)
     colCopy = col.copy()
     self.assertEqual(col.isInitialized(), colCopy.isInitialized())
     self.assertEqual(col.getAmountOfAvailableBricks(),
                      colCopy.getAmountOfAvailableBricks())
Ejemplo n.º 5
0
 def test_returnBrick(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1)]))
     b = col.getRandomBrick()
     self.assertFalse(col.returnBrick(LegoBrick(1, 1)))
     self.assertTrue(col.returnBrick(b))
     self.assertFalse(col.returnBrick(b))
Ejemplo n.º 6
0
 def __init__(self,
              width: int,
              height: int,
              brickCollection: LegoBrickCollection,
              populationSize: int,
              mutationThreshold=float):
     if width < 1:
         raise ValueError("width must be bigger then 1!")
     self.__width = width
     if height < 1:
         raise ValueError("height must be bigger then 1!")
     self.__height = height
     if brickCollection is None:
         raise TypeError("brick collection is none!")
     if not brickCollection.isInitialized():
         raise NotInitializedException(
             "Received brick collection not initialized!")
     self.__brickCollection = brickCollection
     if populationSize < 1:
         raise ValueError("population size must be bigger then 1!")
     if populationSize % 2 != 0:
         populationSize += 1
     self.__populationSize = populationSize
     if mutationThreshold < 0.0 or mutationThreshold > 1.0:
         raise ValueError("mutation threshold must be in range [0.0,1.0]!")
     self.__mutationThreshold = mutationThreshold
Ejemplo n.º 7
0
    def initialize(self, width: int, height: int,
                   brickCollection: LegoBrickCollection):
        """
        LegoBrickLayout instance initialization.
        This function should be called once, more calls will be meaningless.

        Parameters
        ----------
        width : int
            The brick width.
        height : int
            The brick height.
        brickCollection : LegoBrickCollection
            An collection of bricks to create the layer

        Raises
        ------
        ValueError
            If the width or the height isn't bigger then 0.
        TypeError
            If the List[LegoBrick] is None
        """

        if self.__initialized:
            return

        if width < 1:
            raise ValueError("width must be bigger then 1!")
        self.__width = width
        if height < 1:
            raise ValueError("height must be bigger then 1!")
        self.__height = height
        if brickCollection is None:
            raise TypeError("brick collection is none!")
        if not brickCollection.isInitialized():
            raise NotInitializedException(
                "Received brick collection not initialized!")

        self.__brickCollection = brickCollection.copy()
        self.__layout = []
        self.__coveredArea = 0
        self.__area = np.zeros((width, height), dtype=np.int32)

        if self.__brickCollection.getAmountOfAvailableBricks() != 0:
            self.__createRandomLayout()

        self.__initialized = True
Ejemplo n.º 8
0
 def test_randomBrickId(self):
     col = LegoBrickCollection()
     col.initialize(5, list([LegoBrick(1, 1)]))
     colCopy = col.copy()
     self.assertNotEqual(col.getRandomBrick().getId(),
                         colCopy.getRandomBrick().getId())
     self.assertEqual(col.getRandomBrick().getId() + 1,
                      colCopy.getRandomBrick().getId())
     self.assertEqual(colCopy.getRandomBrick().getId() + 1,
                      col.getRandomBrick().getId())
Ejemplo n.º 9
0
 def test_addMutationToFullLayer(self):
     width, height = 5, 5
     collection = LegoBrickCollection()
     collection.initialize(width * height * 2, [LegoBrick(1, 1)],
                           uniform=True)
     self.assertTrue(collection.isInitialized())
     layout = LegoBrickLayout()
     layout.initialize(width, height, collection)
     self.assertTrue(layout.isInitialized())
     copy = layout.copy()
     sizeBeforeMutation = len(layout.getAreaBricks())
     if GaUtils.addMutation(layout):
         self.assertEqual(sizeBeforeMutation + 1,
                          len(layout.getAreaBricks()))
         self.assertFalse(copy.hasSameCoverage(layout))
     else:
         self.assertEqual(sizeBeforeMutation, len(layout.getAreaBricks()))
         self.assertTrue(copy.hasSameCoverage(layout))
Ejemplo n.º 10
0
 def test_nonUniformCollection(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1), LegoBrick(1, 2)]), False)
     amount = col.getAmountOfAvailableBricks()
     while amount != 0:
         rnd = col.getRandomBrick()
         self.assertIsNotNone(rnd)
         amount -= 1
     self.assertEqual(amount, col.getAmountOfAvailableBricks())
     rnd = col.getRandomBrick()
     self.assertIsNone(rnd)
Ejemplo n.º 11
0
    def test_wrongInitialization(self):
        throws = False
        try:
            col = LegoBrickCollection()
            col.initialize(0, [])
        except ValueError as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection constructor didn't throw ValueError on illegal parameter"
        )

        throws = False
        try:
            col = LegoBrickCollection()
            col.initialize(10, None)
        except TypeError as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection constructor didn't throw TypeError on illegal parameter"
        )
Ejemplo n.º 12
0
    def test_initialization(self):
        col = LegoBrickCollection()
        self.assertFalse(col.isInitialized())

        try:
            col.initialize(10, [])
        except Exception as e:
            self.fail(
                "LegoBrickCollection constructor raise exception on unexpected place"
            )
        self.assertTrue(col.isInitialized())

        col = LegoBrickCollection()
        self.assertFalse(col.isInitialized())
        try:
            col.initialize(50, list([LegoBrick(1, 1)]))
        except Exception as e:
            self.fail(
                "LegoBrickCollection constructor raise exception on unexpected place"
            )
        self.assertTrue(col.isInitialized())
Ejemplo n.º 13
0
def generateCollection(width: int, height: int,
                       bricks: List[LegoBrick]) -> LegoBrickCollection:
    brickCollection = LegoBrickCollection()
    brickCollection.initialize(width * height, bricks, uniform=True)
    assert brickCollection.isInitialized()
    return brickCollection
Ejemplo n.º 14
0
    def test_notInitializeExceptions(self):
        col = LegoBrickCollection()
        throws = False
        try:
            col.getAmountOfAvailableBricks()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getAmountOfAvailableBricks() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getNumberOfBricksTypes()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getNumberOfBricksTypes() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getRandomBrick()
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getRandomBrick() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.getBrick(1, 1)
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.getBrick() didn't throw NotInitializedException after calling method before initialization"
        )

        throws = False
        try:
            col.returnBrick(LegoBrick(1, 1))
        except NotInitializedException as e:
            throws = True
        self.assertTrue(
            throws,
            "LegoBrickCollection.returnBrick() didn't throw NotInitializedException after calling method before initialization"
        )
Ejemplo n.º 15
0
 def test_emptyCollection(self):
     col = LegoBrickCollection()
     col.initialize(10, [])
     self.assertEqual(col.getAmountOfAvailableBricks(), 0)
     rnd = col.getRandomBrick()
     self.assertIsNone(rnd)
Ejemplo n.º 16
0
 def test_amountOfBricks(self):
     bricks = list([LegoBrick(1, 1)])
     col = LegoBrickCollection()
     col.initialize(10, bricks)
     startAmount = col.getAmountOfAvailableBricks()
     rndBrick = col.getRandomBrick()
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount - 1)
     col.returnBrick(LegoBrick(12, 12))
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount - 1)
     col.returnBrick(rndBrick)
     self.assertEqual(col.getAmountOfAvailableBricks(), startAmount)
Ejemplo n.º 17
0
 def test_getSpecificBrick(self):
     col = LegoBrickCollection()
     col.initialize(10, list([LegoBrick(1, 1)]))
     self.assertIsNotNone(col.getBrick(1, 1))
     self.assertIsNone(col.getBrick(2, 1))