local.laer.app.knapsack.SimpleKnapsackTest.java Source code

Java tutorial

Introduction

Here is the source code for local.laer.app.knapsack.SimpleKnapsackTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package local.laer.app.knapsack;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.logging.Logger;
import local.laer.app.knapsack.domain.support.Area;
import local.laer.app.knapsack.domain.support.KnapsackProblem;
import local.laer.app.knapsack.domain.support.Shape;
import local.laer.app.knapsack.support.CharRepresentationMatrix;
import local.laer.app.knapsack.support.IntFunctions;
import local.laer.app.knapsack.support.KnapsackSolution;
import local.laer.app.knapsack.support.OffsetIntMatrix;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Ignore;
import org.junit.Test;

/**
 *
 * @author Lars Eriksson (larsq.eriksson@gmail.com)
 */
public class SimpleKnapsackTest {

    private final static Logger LOG = Logger.getLogger(SimpleKnapsackTest.class.getPackage().getName());

    public SimpleKnapsackTest() {
    }

    @Test
    @Ignore
    public void testKnappsack() throws JsonProcessingException {
        // CD
        // AA
        // BB
        // BB
        Shape s11 = new Shape(1, 1, 1);
        Shape s22 = new Shape(2, 2, 1);
        Shape s12 = new Shape(1, 2, 1);

        KnapsackProblem p = new KnapsackProblem();
        p.setArea(new Area(4, 2, 1));
        p.addShapes(s11, "a1", "a2");
        p.addShapes(s12, "a3");
        p.addShapes(s22, "a4");

        SimpleKnapsack.Runtime rt = new SimpleKnapsack.Runtime(p);

        KnapsackSolution solution = rt.process();

        OffsetIntMatrix m = solution.asIntMatrix();
        LOG.info("\n" + IntFunctions.toString(m));

        CharRepresentationMatrix charMatrix = new CharRepresentationMatrix(m);
        LOG.info("\n" + String.join("\n", charMatrix.toLines()));

        assertThat(m.getInt(0, 0), is(4));
        assertThat(m.getInt(0, 1), is(3));

        assertThat(m.getInt(1, 0), is(1));
        assertThat(m.getInt(1, 1), is(1));

        assertThat(m.getInt(2, 0), is(2));
        assertThat(m.getInt(2, 1), is(2));

        assertThat(m.getInt(3, 0), is(2));
        assertThat(m.getInt(3, 1), is(2));

    }
}