Search in sources :

Example 31 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapImpl method findNeighboringAreas.

public MutablePair<AreaId, AreaId> findNeighboringAreas(final WalkPosition p) {
    final MutablePair<AreaId, AreaId> result = new MutablePair<>(null, null);
    final WalkPosition[] deltas = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(+1, 0), new WalkPosition(0, +1) };
    for (final WalkPosition delta : deltas) {
        if (getData().getMapData().isValid(p.add(delta))) {
            final AreaId areaId = getData().getMiniTile(p.add(delta), CheckMode.NO_CHECK).getAreaId();
            if (areaId.intValue() > 0) {
                if (result.getLeft() == null) {
                    result.setLeft(areaId);
                } else if (!result.getLeft().equals(areaId)) {
                    if (result.getRight() == null || ((areaId.intValue() < result.getRight().intValue()))) {
                        result.setRight(areaId);
                    }
                }
            }
        }
    }
    return result;
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) AreaId(bwem.area.typedef.AreaId) WalkPosition(org.openbw.bwapi4j.WalkPosition)

Example 32 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapInitializerImpl method getSortedMiniTilesByDescendingAltitude.

@Override
public List<MutablePair<WalkPosition, MiniTile>> getSortedMiniTilesByDescendingAltitude() {
    final List<MutablePair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude = new ArrayList<>();
    for (int y = 0; y < getData().getMapData().getWalkSize().getY(); ++y) {
        for (int x = 0; x < getData().getMapData().getWalkSize().getX(); ++x) {
            final WalkPosition w = new WalkPosition(x, y);
            final MiniTile miniTile = ((TerrainDataInitializer) getData()).getMiniTile_(w, CheckMode.NO_CHECK);
            if (((MiniTileImpl) miniTile).isAreaIdMissing()) {
                miniTilesByDescendingAltitude.add(new MutablePair<>(w, miniTile));
            }
        }
    }
    miniTilesByDescendingAltitude.sort(new PairGenericMiniTileAltitudeComparator<>());
    Collections.reverse(miniTilesByDescendingAltitude);
    return miniTilesByDescendingAltitude;
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) WalkPosition(org.openbw.bwapi4j.WalkPosition) MiniTile(bwem.tile.MiniTile) MiniTileImpl(bwem.tile.MiniTileImpl)

Example 33 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapInitializerImpl method createAreas.

// Initializes Graph with the valid and big enough areas in tempAreaList.
@Override
public void createAreas(final List<TempAreaInfo> tempAreaList, final int areaMinMiniTiles) {
    final List<MutablePair<WalkPosition, Integer>> areasList = new ArrayList<>();
    int newAreaId = 1;
    int newTinyAreaId = -2;
    for (final TempAreaInfo tempArea : tempAreaList) {
        if (tempArea.isValid()) {
            if (tempArea.getSize() >= areaMinMiniTiles) {
                // bwem_assert(newAreaId <= tempArea.id());
                if (!(newAreaId <= tempArea.getId().intValue())) {
                    throw new IllegalStateException();
                }
                if (newAreaId != tempArea.getId().intValue()) {
                    replaceAreaIds(tempArea.getWalkPositionWithHighestAltitude(), new AreaId(newAreaId));
                }
                areasList.add(new MutablePair<>(tempArea.getWalkPositionWithHighestAltitude(), tempArea.getSize()));
                ++newAreaId;
            } else {
                replaceAreaIds(tempArea.getWalkPositionWithHighestAltitude(), new AreaId(newTinyAreaId));
                --newTinyAreaId;
            }
        }
    }
    getGraph().createAreas(areasList);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) AreaId(bwem.area.typedef.AreaId) TempAreaInfo(bwem.area.TempAreaInfo)

Example 34 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapInitializerImpl method computeTempAreas.

@Override
public List<TempAreaInfo> computeTempAreas(final List<MutablePair<WalkPosition, MiniTile>> miniTilesByDescendingAltitude) {
    final List<TempAreaInfo> tempAreaList = new ArrayList<>();
    // tempAreaList[0] left unused, as AreaIds are > 0
    tempAreaList.add(new TempAreaInfo());
    for (final MutablePair<WalkPosition, MiniTile> current : miniTilesByDescendingAltitude) {
        final WalkPosition pos = new WalkPosition(current.getLeft().getX(), current.getLeft().getY());
        final MiniTile cur = current.getRight();
        final MutablePair<AreaId, AreaId> neighboringAreas = findNeighboringAreas(pos);
        if (neighboringAreas.getLeft() == null) {
            // no neighboring area : creates of a new area
            tempAreaList.add(new TempAreaInfo(new AreaId(tempAreaList.size()), cur, pos));
        } else if (neighboringAreas.getRight() == null) {
            // one neighboring area : adds cur to the existing area
            tempAreaList.get(neighboringAreas.getLeft().intValue()).add(cur);
        } else {
            // two neighboring areas : adds cur to one of them  &  possible merging
            AreaId smaller = neighboringAreas.getLeft();
            AreaId bigger = neighboringAreas.getRight();
            if (tempAreaList.get(smaller.intValue()).getSize() > tempAreaList.get(bigger.intValue()).getSize()) {
                AreaId smallerTmp = smaller;
                smaller = bigger;
                bigger = smallerTmp;
            }
            // Condition for the neighboring areas to merge:
            // any_of(StartingLocations().begin(), StartingLocations().end(),
            // [&pos](const TilePosition & startingLoc)
            // { return dist(TilePosition(pos), startingLoc + TilePosition(2, 1)) <=
            // 3;})
            boolean cppAlgorithmStdAnyOf = getData().getMapData().getStartingLocations().stream().anyMatch(startingLoc -> BwemExt.dist(pos.toTilePosition(), startingLoc.add(new TilePosition(2, 1))) <= 3.0);
            final int curAltitude = cur.getAltitude().intValue();
            final int biggerHighestAltitude = tempAreaList.get(bigger.intValue()).getHighestAltitude().intValue();
            final int smallerHighestAltitude = tempAreaList.get(smaller.intValue()).getHighestAltitude().intValue();
            if ((tempAreaList.get(smaller.intValue()).getSize() < 80) || (smallerHighestAltitude < 80) || ((double) curAltitude / (double) biggerHighestAltitude >= 0.90) || ((double) curAltitude / (double) smallerHighestAltitude >= 0.90) || cppAlgorithmStdAnyOf) {
                // adds cur to the absorbing area:
                tempAreaList.get(bigger.intValue()).add(cur);
                // merges the two neighboring areas:
                replaceAreaIds(tempAreaList.get(smaller.intValue()).getWalkPositionWithHighestAltitude(), bigger);
                tempAreaList.get(bigger.intValue()).merge(tempAreaList.get(smaller.intValue()));
            } else {
                // no merge : cur starts or continues the frontier between the two neighboring
                // areas
                // adds cur to the chosen Area:
                tempAreaList.get(chooseNeighboringArea(smaller, bigger).intValue()).add(cur);
                super.rawFrontier.add(new MutablePair<>(neighboringAreas, pos));
            }
        }
    }
    // Remove from the frontier obsolete positions
    rawFrontier.removeIf(f -> f.getLeft().getLeft().equals(f.getLeft().getRight()));
    return tempAreaList;
}
Also used : Utils(bwem.util.Utils) Altitude(bwem.typedef.Altitude) Player(org.openbw.bwapi4j.Player) PairGenericAltitudeComparator(bwem.util.PairGenericAltitudeComparator) TilePosition(org.openbw.bwapi4j.TilePosition) TileData(bwem.tile.TileData) ArrayList(java.util.ArrayList) CheckMode(bwem.CheckMode) MutablePair(org.apache.commons.lang3.tuple.MutablePair) AreaId(bwem.area.typedef.AreaId) StaticBuilding(bwem.unit.StaticBuilding) Neutral(bwem.unit.Neutral) BWMap(org.openbw.bwapi4j.BWMap) MapDrawer(org.openbw.bwapi4j.MapDrawer) TempAreaInfo(bwem.area.TempAreaInfo) UnitImpl(org.openbw.bwapi4j.unit.UnitImpl) PairGenericMiniTileAltitudeComparator(bwem.util.PairGenericMiniTileAltitudeComparator) WalkPosition(org.openbw.bwapi4j.WalkPosition) Mineral(bwem.unit.Mineral) Collection(java.util.Collection) Timer(bwem.util.Timer) MineralPatch(org.openbw.bwapi4j.unit.MineralPatch) MiniTileImpl(bwem.tile.MiniTileImpl) TileImpl(bwem.tile.TileImpl) List(java.util.List) Logger(org.apache.logging.log4j.Logger) PlayerUnit(org.openbw.bwapi4j.unit.PlayerUnit) VespeneGeyser(org.openbw.bwapi4j.unit.VespeneGeyser) BwemExt(bwem.util.BwemExt) TileDataImpl(bwem.tile.TileDataImpl) NeutralDataImpl(bwem.unit.NeutralDataImpl) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MiniTile(bwem.tile.MiniTile) NeutralImpl(bwem.unit.NeutralImpl) MutablePair(org.apache.commons.lang3.tuple.MutablePair) TilePosition(org.openbw.bwapi4j.TilePosition) ArrayList(java.util.ArrayList) WalkPosition(org.openbw.bwapi4j.WalkPosition) MiniTile(bwem.tile.MiniTile) AreaId(bwem.area.typedef.AreaId) TempAreaInfo(bwem.area.TempAreaInfo)

Example 35 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class PairGenericMiniTileAltitudeComparatorTest method testPairGenericMiniTileAltitudeComparator.

@Test
public void testPairGenericMiniTileAltitudeComparator() {
    final List<MutablePair<WalkPosition, MiniTile>> list = new ArrayList<>();
    final MiniTile m1 = new MiniTileImpl();
    ((MiniTileImpl) m1).setAreaId(new AreaId(2));
    ((MiniTileImpl) m1).setAltitude(new Altitude(5));
    final MiniTile m2 = new MiniTileImpl();
    ((MiniTileImpl) m2).setAreaId(new AreaId(1));
    ((MiniTileImpl) m2).setAltitude(new Altitude(2));
    final MiniTile m3 = new MiniTileImpl();
    ((MiniTileImpl) m3).setAreaId(new AreaId(3));
    ((MiniTileImpl) m3).setAltitude(new Altitude(7));
    final MiniTile m4 = new MiniTileImpl();
    ((MiniTileImpl) m4).setAreaId(new AreaId(5));
    ((MiniTileImpl) m4).setAltitude(new Altitude(1));
    final MiniTile m5 = new MiniTileImpl();
    ((MiniTileImpl) m5).setAreaId(new AreaId(4));
    ((MiniTileImpl) m5).setAltitude(new Altitude(8));
    list.add(new MutablePair<>(new WalkPosition(5, 15), m1));
    list.add(new MutablePair<>(new WalkPosition(2, 98), m2));
    list.add(new MutablePair<>(new WalkPosition(63, 123), m3));
    list.add(new MutablePair<>(new WalkPosition(103, 435), m4));
    list.add(new MutablePair<>(new WalkPosition(89, 77), m5));
    // Sort by descending order.
    Collections.sort(list, new PairGenericMiniTileAltitudeComparator<>());
    Collections.reverse(list);
    Assert.assertEquals(new MutablePair<>(new WalkPosition(89, 77), m5), list.get(0));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(63, 123), m3), list.get(1));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(5, 15), m1), list.get(2));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(2, 98), m2), list.get(3));
    Assert.assertEquals(new MutablePair<>(new WalkPosition(103, 435), m4), list.get(4));
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) MiniTile(bwem.tile.MiniTile) AreaId(bwem.area.typedef.AreaId) WalkPosition(org.openbw.bwapi4j.WalkPosition) MiniTileImpl(bwem.tile.MiniTileImpl) Altitude(bwem.typedef.Altitude) Test(org.junit.Test)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)116 Pair (org.apache.commons.lang3.tuple.Pair)49 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 Message (com.microsoft.azure.sdk.iot.device.Message)27 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)27 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)23 List (java.util.List)20 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 Map (java.util.Map)14 IOException (java.io.IOException)13 WalkPosition (org.openbw.bwapi4j.WalkPosition)9 MiniTile (bwem.tile.MiniTile)8 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 Collectors (java.util.stream.Collectors)7 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5