Semaphore - Showing it is always true that S >= 0 - process

I was reading online about Semaphores and I found this question that I found really interesting where I need to show that:
For any semaphore S, it is always true that S >= 0
I am having trouble trying to understand how I could do that?
From what I have read online this statement is true but what I find difficult understanding is why?
Would anyone be able to shed some light?

Related

Why does Gurobi's best bound for the objective goes over the optimal objective value

I am solving an integer programming problem in Gurobi for objective minimization. I know that there is an integer solution that gives an objective of 322.48, and I verified this by also setting that solution as constraints and solving with Gurobi. However, when solving, Gurobi's best bound starts from 322.48, but always goes beyond it, even going to 323.80, when in fact I know that a better solution exists. Does anyone know why this would be happening?
I run Gurobi with the following parameters:
opt.Params.Method = 2
opt.Params.Threads = 1
opt.Params.MIPFocus = 1
opt.Params.MIPGap = 1e-9
opt.Params.IntegralityFocus = 1
opt.Params.IntFeasTol = 1e-9
opt.Params.FeasibilityTol = 1e-9
When I set Params.Method to default, the relaxation objective itself turns out to be 323.60, which is incorrect as I previously verified that there is a solution with 322.48 objective. Something is off, does anyone have any idea what?

Why is ScoreManager changing the solution to a worse score?

I'm using solverManager to continually save the best score:
solverManager.solveAndListen(
SINGLETON_TIME_TABLE_ID,
this::findById,
this::save
)
My save() method just updates a global reference to the best solution and nothing else happens to it.
However, when I go to retrieve the best solution and get the score details, it does not always get the best solution:
val solution: MySolution = findById(SINGLETON_TIME_TABLE_ID)
println(solution.score) // prints 1100
scoreManager.updateScore(solution) // Sets the score
println(solution.score) // prints 1020 (it's now worse than before)
Simply calling scoreManager.updateScore(solution) seems to bring back a worse solution. In the above example, the score might go from 1100 down to 1020. Is there an obvious explanation for this?
I'm using SimpleScore and this happens well after all planning variables are assigned.
I'm using a variable listener ArrivalTimeUpdatingVariableListener : VariableListener.
Not sure what else is relevant. Thanks
This has all warning signs of a score corruption. Please run your solver for a couple minutes with <environmentMode>FULL_ASSERT</environmentMode>. If you'll see exceptions being thrown, you know that your constraints have a bug in them. What that bug is, that is impossible for me to know unless I see those constraints.

wxMaxima: understanding ratcoef

Ran into the following behaviour from ratcoef that I don't understand, and was hoping someone could help clarify what's happening.
ratcoef(3*sqrt(x)+5/sqrt(x),x,1/2);
> 3
as epected. However,
ratcoef(x^(3/2)+3*sqrt(x)+5/sqrt(x),x,1/2);
> 0
and yet changing the first power from 3/2 to 3 gives:
ratcoef(x^(3)+3*sqrt(x)+5/sqrt(x),x,1/2);
> 3
The documentation clearly says to not use ratcoef for negative coefficients, but I can't see how the limitation would explain these results. Any suggestions would be very much appreciated!

Score calculation difference while solving vs. explaining the score

I have a DRL using HardMediumSoftScore, and have found that in some cases there is a difference in score result while calculating and afterwards while explaining the score.
SolverFactory<Problem> solverFactory=solverFactory.cloneSolverFactory();
Solver<Problem> solver = requestSolverFactory.buildSolver();
Problem solved = solver.solve( problem );
At the end of this snippet, the Solver logs some kind of score, e.g:
Solving ended: time spent (100648), best score (-339hard/0medium/0soft)...
After the above snippet, I immediately have:
ScoreDirector<Roster> scoreDirector = solver.getScoreDirectorFactory().buildScoreDirector();
scoreDirector.setWorkingSolution( solved );
scoreDirector.calculateScore();
for( ConstraintMatchTotal constraintMatchTotal : scoreDirector.getConstraintMatchTotals() )
{
LOG.debug( "{} {} {}",constraintMatchTotal.getConstraintName(),constraintMatchTotal.getConstraintMatchCount(), constraintMatchTotal.getScoreTotal() );
}
Here hard scores always match, but sometimes there are also medium scores in the explanation, which are not there at the end of solving, like:
hardConstraint 3 -339hard/0medium/0soft
mediumConstraint 14 0hard/-1582medium/0soft
Anyone got an idea?
In the solver config, turn on environmentMode FULL_ASSERT (or FAST_ASSERT) and let it run long enough. I suspect you 'll get a score corruption exception. Fix that first.
(Read the score corruption error message analysis a couple of times to understand it.)

misunderstanding how the threads work

I have a problem, big problem with threads in vb.net
First of all I want to tell that I didn't work with threads before (just on the school), I read lot of pages about it, but none of them could help me for my problem.
My main question here is understand the logic, and after if is possible, solve the problem that I have, both are related, then I going to explain the problem.
The code hasn't comments and/or documentation related, and is a program developed lot of years ago and the guy that did this is not working on the office, nobody knows how it works :S
I have a list called listOfProccess, and when is only 1 works fine.
in the callback function in QueueUserWorkItem fill the information about p, then execute the thread, I suppose
That list contain an array with information type
listOfProccess[].type = 'a/b/c/d/e/f/g/'
also the list include an ID.
Code:
If listOfProccess.Count > 0 Then
Threading.ThreadPool.SetMinThreads(1, 1)
Threading.ThreadPool.SetMaxThreads(4, 4)
For Each p In listOfProccess
Try
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(Object p.function))
Catch e As Exception
sendMail("mail#mail", "alerts#mail.ie", "", e.StackTrace)
End Try
Next
Problems:
I have two problems here:
Sometimes execute an item in the list i.e. 'a' in a infinite loop, and expends all resources of the machine, but if i close and restart again, works, I didn't know if is a problem with the threads or not, sincerely I think that is other thing, because this problem starts two or tree weeks ago, and the program still running during a year.
This one i think is related about threads, if I have two (or more) p on the list like this:
p[1].type = 'a/b/c/d/e/f/g/'
p[1].ID = 1
p[2].type = 'ww/xx/ff/yy/aa/rr/'
p[2].ID =2
when the system execute something like this, the way that it follows is 'random' ie. takes for the first one, a, b,c and after this it does ww, and come back to the first. the problem is bigger if I have more items in the list, like 4 or 5; this is not a very big problem, because the program works, it not works 100% fine, but works, is more to try the understand why it works on this way.
Any help is welcome.
The second problem is a race condition problem, as you can't guarantee the order of the execution for the threads, there is a non-zero probability that your threads will replace each other's values. There are a lot of wayss to solve this issue: algorythms (either lock-oriented and lock-free), synchronization techniques and so on, and there is no silver-bullet solution for this.
First problem is unclear for me, as I can't understand what exactly do you mean by a infinite loop - may be this can happen if you link items to deleted (from other thread) ones and there is no way to go out of your task, as the links in a list are broken. This still should be solved with sychronization.
I think your should start with MSDN articles or some book about multithreading in general, and after that you should split your program step by step for a small parts you understand.
Update:
p.function - about the infinite loop you should consider the code of this delegate. If there is a condition to restart work, you should check a recursion limit. For example, if there is an optimistic locking algorythm, your code can find out that the update it tried isn't valid, and restart it. And if the links are broken, it will never end it's task, and will stay forever in an infinite loop.