Thanks Geoffrey
I solved this problem. In case anyone is interested, I have attached the
fixed test with your suggestions.

Best regards




2013/11/4 Geoffrey De Smet <[email protected]>

>
> On 04-11-13 14:24, Juan Ignacio Barisich wrote:
>
>  You are right.
> Do you think that is a bug? Or is there a hack to solve this?
>
> User problem :)
> Do something like:
>
> toRemove.getPreviousStandstill().setNextStandstill(toRemove.getNextStandstill);
>
> toRemove.getNextStandstill().setPreviousStandstill(toRemove.getPreviousStandstill);
> It might need a cast and instanceof check to Customer.
>
>
>
>  2013/11/4 Geoffrey De Smet <[email protected]>
>
>>
>> On 04-11-13 13:10, Juan Ignacio Barisich wrote:
>>
>>    Geoffrey, thanks for response.
>>  The the Customer "toRemove" instance IS the same instance as which is
>> in the workingSolution's Customer list. In fact, in the test example I have
>> attached, I get that instance via:
>>
>> final Customer toRemove = workingSolution.getCustomerList().get(0);
>>
>>  Ok, then that's not the problem indeed.
>>
>>
>>  I try to reproduce the hack in CloudBalancing's problemFactChange
>> implementation, but with no success. I mean, I tried:
>>
>>     // to clean the next variable of previous in the chain
>>     Standstill prev = toRemove.getPreviousStandstill();
>>    scoreDirector.beforeEntityRemoved(prev);
>>    prev.setNextCustomer(null);
>>    scoreDirector.afterEntityRemoved(prev);
>>
>>  but this throw an exception like:
>> java.lang.IllegalStateException: The entity (1(after 66)) has a variable
>> (previousStandstill) with value (66(after 51)) which has a mappedBy
>> variable (nextCustomer) with a value (null) which is not that entity.
>>
>>  Ah, that explains it.
>>
>> Although you remove the customer instance toRemove,
>> one of the other customers still has it as previousStandstill
>> and/or one of the other customers still has it as nextStandstill.
>>
>>
>>  Also, i try to clean the previous variable of the following item in the
>> chain:
>>
>>    // to clean the previous variable of the following customer in the
>> chain
>>    Customer nextCustomer = toRemove.getNextCustomer();
>>    if (nextCustomer != null) {
>>       scoreDirector.beforeEntityRemoved(nextCustomer);
>>       nextCustomer.setPreviousStandstill(null);
>>       scoreDirector.beforeEntityRemoved(nextCustomer);
>>    }
>>
>> but this throw an exception like:
>> java.lang.IllegalStateException: The ScoreDirector (class
>> org.optaplanner.core.impl.score.director.common.TrailingEntityMapSupport)
>> is corrupted, because the entity (10(after null)) for chained
>> planningVariable (previousStandstill) cannot be retracted: it was never
>> inserted.
>>
>>  Can you take a look to the test I have attached and guide me to solve
>> this?
>>
>> Thanks a lot.
>>  Regards
>>
>>
>>
>>
>>
>>
>>
>> 2013/11/4 Geoffrey De Smet <[email protected]>
>>
>>>
>>> On 01-11-13 19:38, Juan Ignacio Barisich wrote:
>>>
>>>     Hi everybody
>>>  I am using optaplanner in real-time style. My application domain is
>>> similar to the "Vehicle Routing" example (
>>> http://docs.jboss.org/drools/release/6.0.0.CR5/optaplanner-docs/html_single/index.html#vehicleRouting).
>>> I mean, I am using chained variables and shadow variables. I have a problem
>>> while trying to remove a fact (an entity), via a ProblemFactChange:
>>>
>>> java.lang.IllegalArgumentException: The entity instance (...) was never
>>> added to this ScoreDirector. Usually the cause is that that specific
>>> instance was not in your Solution's entities.
>>>
>>>  The fact removing that I made is like:
>>>
>>> solver.addProblemFactChange(new ProblemFactChange() {
>>>
>>>             @Override
>>>             public void doChange(ScoreDirector scoreDirector) {
>>>                 ...
>>>                 scoreDirector.beforeEntityRemoved(toRemove);
>>>                 workingSolution.getCustomerList().remove(toRemove);
>>>
>>>  the remove() method will make no difference because the Customer
>>> "toRemove" instance
>>> is not the same instance as which is in the workingSolution's Customer
>>> list.
>>>
>>> See the CloudBalancing's PRoblemFactChange implementation how to solve
>>> this.
>>> And one of the warnings in the Real-time planning section of the docs to
>>> understand it better:)
>>>
>>>                    scoreDirector.afterEntityRemoved(toRemove);
>>>             }
>>>         });
>>>
>>>  I think the problem is there. Do you know what I'm doing wrong?
>>>  I have attached a test case based on the "Vehicle Routing". To run it,
>>> just put that file into the
>>> optaplanner-distribution-6.0.0.CR5/examples/sources/src/test/java/org/optaplanner/examples/vehiclerouting/app/
>>> directory.
>>>
>>>  <http://drools.46999.n3.nabble.com/file/n4017760/serverlog.zip>I'd
>>> appreciate any ideas on how to resolve this.
>>> Best regards,
>>>
>>>  Juan Ignacio Barisich
>>>
>>>
>>>
>>>
>>>
>>>
>>>  _______________________________________________
>>> rules-users mailing 
>>> [email protected]https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> [email protected]
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>
>>
>>
>> _______________________________________________
>> rules-users mailing 
>> [email protected]https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>>
>> _______________________________________________
>> rules-users mailing list
>> [email protected]
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
>
> _______________________________________________
> rules-users mailing 
> [email protected]https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> _______________________________________________
> rules-users mailing list
> [email protected]
> https://lists.jboss.org/mailman/listinfo/rules-users
>
package org.optaplanner.examples.vehiclerouting.app;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Before;
import org.junit.Test;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
import org.optaplanner.core.api.solver.Solver;
import org.optaplanner.core.api.solver.SolverFactory;
import org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig;
import org.optaplanner.core.config.solver.EnvironmentMode;
import org.optaplanner.core.config.solver.SolverConfig;
import org.optaplanner.core.config.solver.XmlSolverFactory;
import org.optaplanner.core.config.termination.TerminationConfig;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.core.impl.solution.Solution;
import org.optaplanner.core.impl.solver.ProblemFactChange;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.vehiclerouting.domain.Customer;
import org.optaplanner.examples.vehiclerouting.domain.Standstill;
import org.optaplanner.examples.vehiclerouting.domain.timewindowed.TimeWindowedVehicleRoutingSolution;
import org.optaplanner.examples.vehiclerouting.persistence.VehicleRoutingDao;
import org.optaplanner.examples.vehiclerouting.solver.score.VehicleRoutingSimpleScoreCalculator;

/**
 * Test of bug while removing an entity on real-time, chained with shadow
 * variables entities.
 * 
 * @author [email protected]
 */
public class VehicleRoutingBugTest {

	protected SolutionDao solutionDao;
	private ExecutorService executor = Executors.newFixedThreadPool(1);
	protected File unsolvedDataFile;

	protected SolverFactory buildSolverFactory() {
		SolverFactory solverFactory = new XmlSolverFactory(
				createSolverConfigResource());
		TerminationConfig terminationConfig = new TerminationConfig();
		solverFactory.getSolverConfig().setTerminationConfig(terminationConfig);
		return solverFactory;
	}

	protected ScoreDirectorFactoryConfig createOverwritingAssertionScoreDirectorFactory() {
		ScoreDirectorFactoryConfig assertionScoreDirectorFactoryConfig = new ScoreDirectorFactoryConfig();
		assertionScoreDirectorFactoryConfig
				.setSimpleScoreCalculatorClass(VehicleRoutingSimpleScoreCalculator.class);
		return assertionScoreDirectorFactoryConfig;
	}

	protected SolutionDao createSolutionDao() {
		return new VehicleRoutingDao();
	}

	protected String createSolverConfigResource() {
		return "/org/optaplanner/examples/vehiclerouting/solver/vehicleRoutingSolverConfig.xml";
	}

	private void removeFirstEntity(final Solver solver) {
		solver.addProblemFactChange(new ProblemFactChange() {

			@Override
			public void doChange(ScoreDirector scoreDirector) {
				TimeWindowedVehicleRoutingSolution workingSolution = (TimeWindowedVehicleRoutingSolution) scoreDirector
						.getWorkingSolution();
				// e.g., pick the first entity
				final Customer toRemove = workingSolution.getCustomerList()
						.get(0);
				// clean the previous element
				Standstill previous = toRemove.getPreviousStandstill();
				if (previous != null) {
					scoreDirector.beforeProblemFactChanged(previous);
					previous.setNextCustomer(toRemove.getNextCustomer());
					scoreDirector.afterProblemFactChanged(previous);
				}
				// clean the next element
				Customer next = toRemove.getNextCustomer();
				if (next != null) {
					scoreDirector.beforeProblemFactChanged(next);
					next.setPreviousStandstill(toRemove.getPreviousStandstill());
					scoreDirector.afterProblemFactChanged(next);
				}
				// remove the item
				scoreDirector.beforeProblemFactRemoved(toRemove);
				workingSolution.getCustomerList().remove(toRemove);
				scoreDirector.beforeProblemFactRemoved(toRemove);
			}
		});
	}

	@Before
	public void setUp() {
		solutionDao = createSolutionDao();
	}

	@Test
	public void testBug() throws InterruptedException {
		SolverFactory solverFactory = buildSolverFactory();
		@SuppressWarnings("unchecked")
		final Solution<HardSoftScore> planningProblem = solutionDao
				.readSolution(new File(
						"data/vehiclerouting/unsolved/cvrptw-100customers-A.xml"));
		solverFactory.getSolverConfig().getTerminationConfig()
				.setMaximumMinutesSpend(2L);
		SolverConfig solverConfig = solverFactory.getSolverConfig();
		solverConfig
				.setEnvironmentMode(EnvironmentMode.NON_INTRUSIVE_FULL_ASSERT);
		ScoreDirectorFactoryConfig assertionScoreDirectorFactory = createOverwritingAssertionScoreDirectorFactory();
		if ((assertionScoreDirectorFactory != null)
				&& EnvironmentMode.NON_INTRUSIVE_FULL_ASSERT.isAsserted()) {
			solverConfig.getScoreDirectorFactoryConfig()
					.setAssertionScoreDirectorFactory(
							assertionScoreDirectorFactory);
		}
		final Solver solver = solverFactory.buildSolver();
		solver.setPlanningProblem(planningProblem);
		executor.submit(new Runnable() {
			@Override
			public void run() {
				try {
					// Wait until solver works some time
					Thread.sleep(1000 * 5);
				} catch (InterruptedException e) {
					throw new UnsupportedOperationException();
				}
				// Remove ten items
				for (int i = 0; i < 10; i++) {
					removeFirstEntity(solver);
				}
			}
		});
		solver.solve();
	}

}
_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to