What is purpose of Standstill in VRP? I am trying to understand below rule in VRP example. What is previousStandstill?
rule "distanceToPreviousStandstill"
when
$customer : Customer(previousStandstill != null, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
then
scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end
previousStandstill is Vehicle or another Customer.
Ex.
ROUTE = VEHICLE(depot) -> CUSTOMER A -> CUSTOMER B -> CUSTOMER C -> CUSTOMER D
previousStandstill for CUSTOMER B is CUSTOMER A
previousStandstill for CUSTOMER A is VEHICLE
Vehicle location is the same as the location of depot.
So this rule add soft score for all distance in route except last part from CUSTOMER D to Vehicle
Related
Example:
Students want to enroll in multiple courses of different course groups (Math, English, Spanish, History) and have issued preferences for each course group (ENG-1 > ENG-2 means course ENG-1 is preferred to course ENG-2).
Student A:
MATH-2 > MATH-4 > MATH-1 > ... > MATH-9
ENG-3 > ENG-4 > ENG-1 > ... > ENG-2
Student B:
ENG-1 > ENG-2 > ENG-4 > ... > ENG-3
SPA-4 > SPA-6 > SPA-3 > ... > SPA-2
HIST-1 > HIST-3 > HIST-2 ... > HIST-5
Student C:
...
Is it possible for a planning variable of each student (planning entity) to be a combination of each of their preferences? I.e. student A would be assigned MATH-2 and ENG-3, student B would be assigned ENG-1, SPA-4, and HIST-1, if the constraints allow for this.
Yes (and no). Technically no, because #PlanningVariable can only hold a single value.
But YES, OptaPlanner can handle your use case. You just need to choose the right way to map your domain to Java classes. You need to model a N:M relation between Student and Course:
Student A needs to enroll in 2 courses (one from the MATH group and one from the ENG group).
Student B needs to enroll in 3 courses (ENG, SPA, HIST).
etc.
You can model this type of relationship with the CourseAssignment class, which is your #PlanningEntity. It could look like this:
#PlanningEntity
class CourseAssignment {
final Student student; // e.g. Ann
final CourseGroup courseGroup; // e.g. MATH
#PlanningVariable(valueRangeProviderRefs = { "courseRange" })
Course course; // changed by Solver - could be MATH-1, MATH-2, ENG-1, HIST-...
}
Since the number of course assignments is known for each student and it's fixed, you'd simply create 2 CourseAssignment instances for Student A, 3 instances for Student B, etc.
Next, design your constraints to penalize each courseAssignment with a hard score penalty, if courseAssignment.course.group != courseAssignment.courseGroup and with a soft score penalty based on courseAssignment.student.getPreference(courseAssignment.course).
Basically, i have 3 models:
[A] - one2many -> [B] - one2many -> [C]
When a field in model A change, Model B dropdown of C elements should be filtered by some domain.
How can i do that?
More specific. In Inventory / Dashboard -> Click on Internal Transfers -> Create a new one, then:
see inventory movement form
Let try add a domain to your Initial Demand-> Product fields like:
< field name='product'
domain="[('source_location','=',parent.source_location[0][2] or
False/>
If source_location is a field in Product object
Here are the facts:
There are many companies.
Each company can have many businesses.
There are many addresses.
You don't know which businesses are owned by which companies (or the name of the company).
However, you do know the address of each business and you know a business might trade at more than one address.
Forming relationships between addresses:
If a business has the same address as another then, for the purpose of this question, we will say that they are owned by the same company.
ie A link is formed between two addresses when a business uses both addresses.
So ,an address "A" might be linked to many other addresses.
Note that:
6a. the addresses that address "A" links to might also be linked to one OR MORE addresses.
6b. One of the addresses ""A" links to might link back to "A" via a third address (ie two business that use both these addresses)
A complex example of this is shown in the picture attached. In this picture, there are only two companies. One has the red business, the other has the blue, green and black business.
Here is some example data in tableBA ( I have attached a photo to describe these relationships)
BUSINESS Address
A 1
A 2
B 1
C 3
D 4 < four businesses sharing the same address
E 4
F 4
G 4
W 2
W 5
X 5
X 6
So I want to created code that will create the following output. The output has one name per company and lists the business names that are in the company.
ie there is one row for every complete chain of addresses
A,B,W,X
D,E,F,G
C
This question is an simplification/ improvement on another SO question here.
This answer to the other question uses a combination of SQL and VBA code to solve the problem, because MS Access doesn't support recursive joining.
How can this be done with pure SQL, either with recursive joining or some other technique (not with a stored procedure)?
This is an SQL Server answer.
Quote from this answer:
To clarify:
A business can have multiple addresses
Any business in a business group (AKA company) shares an address with any other business in the group
A business group can own multiple businesses
Each business is only associated with one business group (corollary to second point)
SQL Fiddle with sample data
We can refer to each business group by the first (smallest name in
alphabetical order) business in the group. Let's call this the key
business. After we've identified the key business for each business,
we can group by the key business and get the results.
In order to get the key business:
Generate a list of pairs of businesses where both businesses are in the same group, based on any shared address. This list should exclude
the following (see next point for why):
A -> B, when we have B -> A
A -> A
The left side of the pairs should be unique: each business should appear on the left side of the pair no more than once, if at all.
For each business, follow the pairs from one to the next until the right business is never the left business in any other pair. That is
the key business.
That is the reason for the exclusions in the first point. If we have both A -> B and B -> A, we'll get to a never-ending loop.
Same goes for A -> A.
The following query will return the key business for each business:
WITH Pairs AS (
SELECT Businesses.Business AS Business2, MIN(Businesses_1.Business) AS Business1
FROM Businesses
INNER JOIN Businesses AS Businesses_1 ON Businesses.Address = Businesses_1.Address
WHERE Businesses.Business > Businesses_1.Business
GROUP BY Businesses.Business
),
KeyBusinesses AS (
SELECT Business2 AS Business, Business1 AS KeyBusiness
FROM Pairs
UNION ALL
SELECT Pairs.Business2, KeyBusinesses.KeyBusiness
FROM Pairs
INNER JOIN KeyBusinesses ON Pairs.Business1 = KeyBusinesses.Business
)
SELECT Businesses.*, ISNULL(KeyBusinesses.KeyBusiness, Businesses.Business) AS KeyBusiness
FROM Businesses
LEFT JOIN KeyBusinesses ON Businesses.Business = KeyBusinesses.Business
SQL Fiddle
This is a crude and crappy solution (probably not efficient), but it gets the correct results. It works by maintaining 2 maps to a Company (where a Company contains a list of business-names). First maps business-address to company, 2nd maps business-name to company. if the company is NOT found in either map, a new company is created: -
package test;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
import java.util.stream.Collectors;
public class Companies {
public static List<Company> listCompanies(List<Business> businesses) {
List<Company> companies = new ArrayList<>();
Map<String, Company> companyByAddress = new HashMap<>(); // map allows many addresses to map to same company
Map<String, Company> companyByBusiness = new HashMap<>(); // map allows many businesses to map to same company
for (Business business : businesses) {
Company company = companyByAddress.get(business.address);
if (company == null)
company = companyByBusiness.get(business.name);
if (company != null) {
company.addAddress(business.name);
} else {
company = new Company();
company.addAddress(business.name);
companies.add(company);
}
companyByBusiness.put(business.name, company);
companyByAddress.put(business.address, company);
}
return companies;
}
#Test
public void testOne() {
List<Business> businesses = new ArrayList<>();
businesses.add(new Business("A", "1"));
businesses.add(new Business("A", "2"));
businesses.add(new Business("B", "1"));
businesses.add(new Business("C", "3"));
businesses.add(new Business("D", "4"));
businesses.add(new Business("E", "4"));
businesses.add(new Business("F", "4"));
businesses.add(new Business("G", "4"));
businesses.add(new Business("W", "2"));
businesses.add(new Business("W", "5"));
businesses.add(new Business("X", "5"));
businesses.add(new Business("X", "6"));
List<Company> companies = listCompanies(businesses);
Assert.assertEquals("A, B, W, X", companies.get(0));
Assert.assertEquals("C", companies.get(1));
Assert.assertEquals("D, E, F, G", companies.get(2));
}
static class Business {
private final String name;
private final String address;
Business(String business, String address) {
this.name = business;
this.address = address;
}
}
static class Company {
private final Set<String> addresses; // Being a "Set", each address will occur only once
Company() {
this.addresses = new LinkedHashSet<>(); // A "LinkedHashSet" preserves insertion order
}
void addAddress(String address) {
addresses.add(address);
}
#Override
public String toString() {
return addresses.stream().collect(Collectors.joining(", "));
}
}
}
This is my Z schema for Appointment DB.
|--AppointmentDB----------------
|attendees : P Person /** those involved in the appointment **/
|
|/** a new TYPE object to store attendees, schedule and purpose **/
|appointments : P APPOINTMENT
|hasAppointment : Person <-> APPOINTMENT
|schedule : APPOINTMENT -> DateTime
|purpose : APPOINTMENT -> Report
|
|/** a forward relation compositions to relate attendees with purpose and schedule **/
|attendeePurpose : hasAppointment;purpose
|attendeeSchedule : hasAppointment;schedule
|-----------------------------
|attendees ⊆ dom(hasAppointment)
|attendees ⊆ dom(attendeePurpose)
|appointments ⊆ ran(hasAppointment)
|-----------------------------
I would like to create a search function that finds an appointment based on the name of the attendees.
I want the search function to return all the details of the appointment object.
How do I design it?
Here is my take :
|--FindAppointment---------------------------------------------------
|ΞAppointmentDB
|attendees? : Person
|appointmentAttendees! : P Person
|appointmentPurpose! : Report
|appointmentSchedule! : DateTime
|-----------------------------
|/** if name of any attendees is given, then it must exist in appointments' domain
|respectively before this function can run**/
|attendees? ∈ dom(attendees)
|
|/** return the set of attendees of the same APPOINTMENT using attendees? as input **/
|appointmentAttendees! = hasAppointment~(|{attendees?}|)
|
|/** Get the image of both forward relational compositions according to set of
|attendees?**/
|appointmentPurpose! = attendeePurpose(|{attendees?}|)
|appointmentSchedule! = attendeeSchedule(|{attendees?}|)
|----------------------------------------------------------------------
Have you type checked your specification?
Your declaration subject? : P Person states that subject? is a set of persons, but subject? : dom(attendees) implies that subject? is a single person.
If you want to have either none or one person given you could introduce a datatype analogous to the Maybe monad in functional programming languages (or null values in other programming languages):
MaybePerson ::= NoPerson | JustPerson <<Person>>
Then you can declare an input like
subject? : MaybePerson
Then I would to suggest to restrain the possible solutions for one input
subject? : ran(JustPerson) => schedule! : schedule(|{ JustPerson~ subject? }|)
If subject? is a set of persons you can achieve the same with:
subject? /= {} => schedule! : schedule(|subject?|)
And then just do the same for the other possible input. You can add also a condition that not both entries should be NoPerson resp. not both input sets should be empty.
I need to make an one-to-one relationship between two tables, it means, each passenger can only reserve one seat on a flight.
I have 4 tables:
Passenger : passengerId(PK), passengerName, PassengerAddress
Seat : seatId(PK), seatClass, flightId(Fk)
Flight : flightId(PK), flightDate
Reseveration : flight(PK), seatId(PK), passengerID(FK), reserveDate
Passenger : passengerId(PK),passengerName,PassengerAddress
//in this table make passneger id primary key as passenger will be unique with his details
Seat : seatId(PK),seatClass,flightId(Fk)
//in this table add a field say passenger id so that a passenger who is allotted in a flight will be mained here eg pasneger x in flight y.,also you can maintain a time for fligh such that if a passneger wantes to travel two times a day in a same flight
Fligth : flight Id(PK),flight Date
//make flight id unique hare
Reseveration: flight(PK),seatId(PK),passenger ID(FK),reserveDate
//this will be the final table reservation where you have all the details with no duplicacy.