0

I’ve been trying to work on in_direct_sequence relation and I think the code is correct… But I’m not getting the desired sequence I want. This is how my code looks like:-

public class InDirectSequence implements HardActivityConstraint {

private StateManager stateManager;
private List<String> jobIds;

public InDirectSequence(StateManager stateManager, List<String> jobIds) {
    this.stateManager = stateManager;
    this.jobIds = jobIds;
}

    @Override
    public ConstraintsStatus fulfilled (JobInsertionContext iFacts, TourActivity prevAct, TourActivity
    newAct, TourActivity nextAct,double prevActDepTime){

    Job thisJob = iFacts.getJob();
    if (jobIds.contains(thisJob.getId())) {
        for (String jobId : jobIds) {
            VehicleRoute route = stateManager.getProblemState(stateManager.createStateId(jobId), VehicleRoute.class);
            // if the jobs are assigned to a different route, reject this route
            if (route != null && route != iFacts.getRoute()) return ConstraintsStatus.NOT_FULFILLED;

            if(route != null){

                if (newAct instanceof TourActivity.JobActivity) {
                    Job newJob = ((TourActivity.JobActivity) newAct).getJob();

                    // If the job is in the list
                    if (jobIds.contains(newJob.getId())) {
                        int newIndex = jobIds.indexOf(newJob.getId());

                        // If there is a previous activity and it's a job
                        if (prevAct instanceof TourActivity.JobActivity) {
                            Job prevJob = ((TourActivity.JobActivity) prevAct).getJob();

                            // If the previous job is not in the list or the new job's index is not after the previous job's index
                            if (!jobIds.contains(prevJob.getId()) || jobIds.indexOf(prevJob.getId()) >= newIndex || jobIds.indexOf(prevJob.getId()) + 1  != newIndex) {
                                return ConstraintsStatus.NOT_FULFILLED;
                            }
                        }

                        // If there is a next activity and it's a job
                        if (nextAct instanceof TourActivity.JobActivity) {
                            Job nextJob = ((TourActivity.JobActivity) nextAct).getJob();

                            // If the next job is not in the list or the new job's index is not before the next job's index
                            if (!jobIds.contains(nextJob.getId()) || jobIds.indexOf(nextJob.getId()) <= newIndex || jobIds.indexOf(nextJob.getId()) - 1 != newIndex ) {
                                return ConstraintsStatus.NOT_FULFILLED;
                            }
                        }
                    }
                }

                return ConstraintsStatus.FULFILLED;
            }
        }
    }
        return ConstraintsStatus.FULFILLED;
    }
}

And these are the jobIds I want to be in direct sequence:

ConstraintManager constraintManager = new ConstraintManager(vehicleRoutingProblem,stateManager);
            List<String> jobIds = new ArrayList<>();
            jobIds.add("62");
            jobIds.add("63");
          
            constraintManager.addConstraint(new InDirectSequence(stateManager,jobIds), ConstraintManager.Priority.CRITICAL);

I think the logic is correct, but its not giving me the desired output. Please review and suggest…!

0 Answers0