Optaplanner - Can we specify the sequence order for visits or add dependency/relation constraints to the visit - optaplanner

I have four customer visits A, B, C, D after calling the optaplanner solve method i got the optimal solution like as below
B -> C -> A -> D
But in my use case we need to arrive VISIT_C after completion of VISIT_B and VISIT_A

Related

Infer order of insertion into BST

It seems several possible orders of insertion would create the BST in the image below.
E.g: F C S B E R D
What conditions regarding the order would ensure the same result please? I'm thinking as long as the entries for a given level are entered before those on the next level?

Google Query Language: filter by date

I'm trying to add a filter by date in a Google Visualization API query, but I'm doing something wrong with the syntax...
This is the code without the date filter:
query.setQuery('SELECT A, B, C, D, E, F, G where upper(A) like upper("keyword") or upper(F) like upper("keyword") order by B DESC');
I want to add an AND and also add the condition that date in ColB must be >= of 1st Aug 2016.
So I tried with:
query.setQuery('SELECT A, B, C, D, E, F, G where upper(A) like upper("keyword") or upper(F) like upper("keyword") AND upper(B) >= date "2016-08-01" order by B DESC');
But the syntax is probably wrong as the query gets interrupted.
If B is a date your error is:
Unable to parse query string for Function QUERY parameter 2: upper takes a text parameter
To solve it just remove upper function.
IF B is just a string then automatic type casting is done and query should run without problems.

Get distance and duration to closest matrix in SQL

I have an logic to find the most optimised to perform delivery.
Lets say, I have location A,B,C. So need distance and duration from A to B, B to A, A to C, C to A, B to C and C to B.
I know how to come out with above query. Example result would be NewMatrix in fiddle.
http://sqlfiddle.com/#!6/9cce7/1
I have a table where I store current matrix we have based on past deliveries. (AppMatrix in table above)
So I need to lookup distance and duration in this table, to find closest matching origin and destination. I have created following function which works just perfect to get my answer :
SELECT TOP 1 Distance, ([Time]/60) as Duration FROM [AppMatrix]
ORDER BY ABS([OriginSiteLat] - #OriginLat) + ABS([OriginSiteLng] - #OriginLong)
,ABS([DestSiteLat] - #DestinationLat) + ABS([DestSiteLng] - #DestinationLong)
The problem is slowness. Since I need to perform these call with each matrix (I can have 700 different deliveries in a day, 700*700 = 14000, this just too slow - it takes few hours to return result)
I'm working on best how to limit the data, but any advise on how to optimize performance is appreciated. Maybe advice on how to use spatial here would help.
This is my current code :
SELECT * FROM CT as A
INNER JOIN CT AS B ON A.Latitude <> B.Latitude AND A.Longitude<>B.Longitude
CROSS APPLY [dbo].[ufn_ClosestLocation](A.Latitude,A.Longitude, B.Latitude, B.Longitude) R

Getting ordered subset of records

I have table which basically describes a bus route. The table includes all the stops of a particular route.
This SQLFiddle shows an example. The data shows the flow in one direction, but a route can also be in the other direction.
I need to get a subset of this route (or all of it) depending on the start and end stations. So for example, I might have these:
A -> M (subset would be B -> L)
A -> K (subset would be B -> J)
C -> H (subset would be D -> G)
I -> M (subset would be J -> L)
However, if the direction is from, for example, H -> B, the subset would need to be G -> C.
The sequence column is for this purpose.
Is there a clean and easy way to do this using just SQL?
Assuming I understand your question, it seems simple enough.
To get a subset just use a between...and operator on the sequence column:
SELECT stop_from, stop_to
FROM routes
WHERE sequence BETWEEN 2 AND 11
ORDER BY sequence
To get the subset in the opposite direction, just order by desc, and select the stop_to as stop_from, and stop_from as stop_to:
SELECT stop_to As stop_from, stop_from As stop_to
FROM routes
WHERE sequence BETWEEN 3 AND 7
ORDER BY sequence DESC
See fiddle here

Best practice for calculate user rate and more

I am building an application that shares some stuff...
Each Object can be rated has a rating 1..5 start. I keep for each the number of rates per star so can calculate the Avg rate.
So per Obj I have: Avg rate and total rate.
I need to get the top10 rated Obj - so can do it using AvgRate+TotalRate (those who has these values as top10).
I want to have in the server an sql table like this:
ObjId (index), totalRate, AvgRate...
If possible to have this table sorted so that can get the top10 as the first 10?
How can query the top10 with the calculation I want?
Also - I need to get the top10 per users. So per user I have all the Obj he shared so can have all of the rates of these Obj - with all info per Obj as mentioned before.
I need to know how to calculate a user rate, and also - how to fast get the top10.
Any ideas?
Later Edit: Sorry, didn't understand your question when writing this answer, gonna leave it still for others..
What's your formula for TotalRate ? And what do you mean by "so can do it using AvgRate+TotalRate" Why are you summing an average to TotalRate - whatever that is?
Best practice is to always compute the sums/averages incrementally.
I would model Obj like this:
A total number of rates received
B total sum of points received
C average (float: B/A )
D - foreign key to user (author/owner of Obj)
When object receives rate X, you then recompute A = A + 1, B = B + X, C = B/A.
In the same manner pre-compute aggregate sums/average. So if Obj belongs to user, create the same fields (A, B, C) to User model/table, and when Obj receives rate X, also update A, B, C values for user D (owner of Obj). Then, when selecting top 10 users, you do not need to join with Obj table (which may get huge), you only select users - descending by B or C column, limit 10.