Optaplanner, How to catch the current time at the beginning of the rule to use it in score? - optaplanner

I have something like that:
scoreHolder.addSoftConstraintMatch(kcontext, (System.currentTimeMillis()-$time.getTime()));
I want to use the current time at the beginning of firing the rule only, and not to be updated during running the rule. just to catch the current time at the first moment the rule is fired and does not change till the end of solving.
I'm using optaplanner 6.1.
thanks in advance.

That would break OptaPlanner, as the Score of the same Solution would change over time (which also implies that comparing 2 different Solutions can not be done fairly - so if a new working score is compared to the best score (which was calculated x seconds ago) it breaks).
Instead, before the solver starts, set the current time millis in a singleton:
myParametrization.setStartingMillis(System.currentMillis());
... = solver.solve(...);
and add that as a problem fact and use it in the score rules (see examination example's InstitutionParameterization).

Related

How to create a binary window for an event in Python

I have some sensor data and I know when the main event happens, but there's some noise before and after this event, and so I want to create a binary window where I can then just through away all the non important information.
Here is a toy example.
eg=pd.DataFrame({"Seconds":[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5],
"Event":[0,0,1,0,0,0,0,1,0,0,0,1],
"Start":[0,1,0,0,0,0,1,0,0,0,1,0],
"End": [0,0,0,1,0,0,0,0,1,0,0,0],
"Want": [0,1,1,1,0,0,1,1,1,0,1,1]})
I have generated my start and end with shifting. What I cannot seem to create in Python is my Want. The start and the end are more than 1 row away from the event, albeit they are fixed intervals.
In excel I am able to create a 3 nested that effectively does the following:
fup_bottom["Want"]=np.where(fup_bottom.End==1,0,
np.where(fup_bottom.Start==1,1,
np.where(fup_bottom.Want.shift(1)==1,1,0)))
In python this doesn't work because the "Want" doesn't exist yet.
I have tried recording the seconds where Start is 1 and forward filling, and then similarly where End is one and backward filling. However, this leads to Want always being 1 after the first event.
I greatly appreciate any help on this. I've been trying to figure this out for a couple of hours and am stuck.
J

How to use indivdual condition in AnyLogic agent based simulation

I am trying to simulate a customer by using agents. In the statechart I built, I would like to apply to each individual agent the waiting time spent in the system by defining different variables:
WatingTimeStart, WaitingTimeEnd and WaitingTime
In order to assign the waiting time to each agent I am using the following command in the transition prior to the state I would like to apply the condition:
this.WaitingTimeStart=time();
In the next State I am then using the following:
this.WaitingTimeEnd=time();
this.WaitingTime=this.WaitingTimeEnd-this.WaitingTimeStart;
Followed by the next transition with the condition (TolerarableWaitingTime is a pre-defined Variable)
this.WaitingTime>TolerarableWaitingTime;
My Problem is the transition does not accept the condition and is not processing the agents to the next state.
I probably make a mistake in:
assinging the variable WaitingTime to each agent
applying the condition correcly
Thanks a lot for any thoughts.
Bastian
It was difficult to understand your question, but here it goes: first, you don't need to use "this", you can just do in the transition previous to the state in question:
WaitingTimeStart=time();
also by convention your variables should start with a low case letter, so it should be waitingTimeStart.
But you don't really even need that code and you are overcomplicating yourself... if you want to apply a waiting time (or a delay) you don't need a conditional transition, you can just use a timeout transition instead, where the timeout time is equal to TolerarableWaitingTime

Confusion with writing a game loop

I'm working on a 2D video game framework, and I've never written a game loop before. Most frameworks I've ever looked in to seem to implement both a draw and update methods.
For my project I implemented a loop that calls these 2 methods. I noticed with other frameworks, these methods don't always get called alternating. Some frameworks will have update run way more than draw does. Also, most of these types of frameworks will run at 60FPS. I figure I'll need some sort of sleep in here.
My question is, what is the best method for implementing this type of loop? Do I call draw then update, or vice versa? In my case, I'm writing a wrapper around SDL2, so maybe that library requires something to be setup in a certain way?
Here's some "pseudo" code I'm thinking of for the implementation.
loop do
clear_screen
draw
update
sleep(16.milliseconds)
break if window_is_closed
end
Though my project is being written in Crystal-Lang, I'm more looking for a general concept that could be applied to any language.
It depends what you want to achieve. Some games prefer the game logic to run more frequently than the frame rate (I believe Source games do this), for some games you may want the game logic to run less frequently (the only example of this I can think of is the servers of some multiplayer games, quite famously Overwatch).
It's important to consider as well that this is a question of resolution, not speed. A game with logic rate 120 and frame rate 60 is not necessarily running at x2 speed, any time critical operations within the game logic should be done relative to the clock*, not the tic rate, or your game will literally go into slow motion if the frames take too long to render.
I would recommend writing a loop like this:
loop do
time_until_update = (update_interval + time_of_last_update) - current_time
time_until_draw = (draw_interval + time_of_last_draw) - current_time
work_done = false
# Update the game if it's been enough time
if time_until_update <= 0
update
time_of_last_update = current_time
work_done = true
end
# Draw the screen if it's been enough time
if time_until_draw <= 0
clear_screen
draw
time_of_last_draw = current_time
work_done = true
end
# Nothing to do, sleep for the smallest period
if work_done == false
smaller = time_until_update
if time_until_draw < smaller
smaller = time_until_draw
end
sleep_for(smaller)
end
# Leave, maybe
break if window_is_closed
end
You don't want to wait for 16ms every frame otherwise you might end up over-waiting if the frame takes a non-trivial amount of time to complete. The work_done variable is so that we know whether or not the intervals we calculated at the start of the loop are still valid, we may have done 5ms of work, which would throw our sleeping completely off so in that scenario we go back around and calculate fresh values.
* You may want to abstractify the clock, using the clock directly can have some weird effects, for example if you save the game and you save the last time you used a magical power as a clock time, it will instantly come off cooldown when you load the save, as that is now minutes, hours or even days in the past. Similar issues exist with the process being suspended by the operating system.

Hyperopt set timeouts and modify space during execution

if someone can help on:
How to set a timeout for each individual test ? a timeout for the total experiment ?
How to setup a progressive strategy which would eliminate/prune a % of worst scoring branches of search space at different stage of the experiment (while using current optimization algorithms) ? ie. at 30% of the max total experiment, it could remove 50% of the worst scoring classifiers and all its branch of hyperparameters to remove it from upcoming tests. Then, same process at 60%...
Thanks a lot!
Following my exchange on hyperopt's github:
there is not a per-trial timeout but hyperopt-sklearn implements its own solution by just wrapping the function. Please look for "fn_with_timeout" at https://github.com/hyperopt/hyperopt-sklearn/ .
from issue 210: "the optimizers are stateless, and fmin stores all state of the experiment in the trials object. So if you remove some experiments from the trials object, it's as if they never happened. use fmin's "max_evals" parameter to interrupt search as often as you need to make these sorts of modifications. It should be fine to use repeated calls with e.g. max_evals increasing by 1 every time if you want really fine grained control."
Thanks for looking into this, #doxav. I've written some code that addresses question 1, taking part of fn_with_timeout from hyperopt-sklearn and adapting it for standard Hyperopt cost functions.
You can find it here:
https://gist.github.com/hunse/247d91d14aaa8f32b24533767353e35d

If passing a negative number to taskDelay function in vxworks, what happens?

Noted that the parameter of taskDelay is of type int, which means the number could be negative. Just wondering how the function is going to react when passing a negative number.
Most functions would validate the input, and just return early/return 0/set the parameter in question to a default value.
I presume there's no critical need to do this in production, and you probably have some code lying around that you could test with.... why not give it a go?
The documentation doesn't address it, and the only error codes they do define doesn't cover this case. The most correct answer therefore is that the results are undefined.
See the VxWorks / Tornado II FAQ for this gem, however:
taskDelay(-1) shows another bug in
the vxWorks timer/tick code. It has
the (side) effect of setting vxTicks
to zero. This corrupts the localtime
(and probably other things). In fact
taskDelay(x) will have the same effect
if vxTicks + x >= 0x100000000. If the
system clock rate is 100Hz this
happens after about 500 days (because
vxTicks wraps). At faster clock rates
it will happen sooner. Anyone trying
for several years uptime?
Oh there is an undocumented upper
limit on the clock rate. At rates
above 4294 select() will fail to
convert its 'usec' time into the
correct number of ticks. (From: David
Laight, dsl#tadpole.co.uk)
Assuming this bug is old, I would hope that it would either return an error or do the same thing as taskDelay(0), which puts your task at the end of the ready queue.
The task delay tick will be VIRTUALLY 10,9,..,1,0 for taskDelay(10).
The task delay tick will be VIRTUALLY -10,-11,...,-2147483648,2147483647,...,1,0 for taskDelay(-10).