001/*******************************************************************************
002 * Copyright (c) 2014 Opt4J
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in all
012 * copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020 * SOFTWARE.
021 *******************************************************************************/
022 
023
024package org.opt4j.operators.neighbor;
025
026import java.util.Collections;
027import java.util.Random;
028
029import org.opt4j.core.common.random.Rand;
030import org.opt4j.core.genotype.PermutationGenotype;
031
032import com.google.inject.Inject;
033
034/**
035 * <p>
036 * Neighbor for the {@link PermutationGenotype}. Reverts a sublist.
037 * </p>
038 * 
039 * <p>
040 * Given a permutation {@code 1 2 3 4 5 6 7 8}, this might result in
041 * {@code 1 2 7 6 5 4 3 8}.
042 * </p>
043 * 
044 * @author lukasiewycz
045 * 
046 */
047public class NeighborPermutationRevert implements NeighborPermutation {
048
049        protected final Random random;
050
051        /**
052         * Constructs a {@link NeighborPermutationRevert} operator for the
053         * {@link PermutationGenotype}.
054         * 
055         * @param random
056         *            the random number generator
057         */
058        @Inject
059        public NeighborPermutationRevert(Rand random) {
060                this.random = random;
061        }
062
063        /*
064         * (non-Javadoc)
065         * 
066         * @see
067         * org.opt4j.operator.neighbor.Neighbor#neighbor(org.opt4j.core.Genotype)
068         */
069        @Override
070        public void neighbor(PermutationGenotype<?> genotype) {
071                int size = genotype.size();
072
073                if (size > 1) {
074                        int a = random.nextInt(size - 1);
075                        int b;
076                        do {
077                                b = a + random.nextInt(size - a);
078                        } while (b == a);
079
080                        while (a < b) {
081                                Collections.swap(genotype, a, b);
082                                a++;
083                                b--;
084                        }
085                }
086
087        }
088
089}