Solution time in CPLEX - runtime

I want to find the solution time of my model in CPLEX and I used the following code:
float temp;
execute{
var before = new Date();
temp = before.getTime();
}
// solve the model
execute{
var after = new Date();
writeln("solving time ~= ",after.getTime()-temp);
}
But the result is : 1.5592e+ 12, which is a huge number. So, do you know how can I reach to the the solution time in second or millisecond?

This is more of a pure javascript question rather than something related to CPLEX. The number you're getting is in milliseconds, but you can convert that into seconds, minutes, etc. using the techniques described at stackoverflow.com/questions/41632942. For example:
var timeDiff = after.getTime() - temp;
// Convert from milliseconds to seconds.
timeDiff /= 1000;
// Display diff in seconds.
writeln("solving time ~= ", timeDiff);

Related

Amibroker: Daily Loss Limit

I want to implement an afl code to find Daily Loss Limit in intraday trading. I will use the code for backtesting around 200 days.
I have the following code but it is with mistakes.
// identify new day
dn = DateNum();
newDay = dn != Ref( dn,-1);
// Daily Loss Limit
dll = Optimize("dll", 0, 5, 10000, 5 );
EquityCount = 10000;
for( i = 0; i < BarCount; i++ )
{
// signals
Buy = ....;
Sell = ....;
diff = (Equity(1) - Ref(Equity(1), -1));
EquityCount = EquityCount + diff;
// allow only dll
Buy = Buy AND EquityCount > dll;
}
Any help will be appreciated.
Thanks.
First your code is completely wrong.
Secondly Equity() function is single security function. It is obsolete.
Use custom backtest interface of AmiBroker instead. See AmiBroker help.

Swift 2 get minutes and seconds from double

I am currently storing time in seconds (for example 70.149 seconds). How would I easily get the minutes and seconds? What I mean is 70.149 seconds is 1min and 10sec. How would I be able to do this easily in swift?
Here is an example of what I want to do.
let time:Double = 70.149 //This can be any value
let mins:Int = time.minutes //In this case it would be 1
let secs:Int = time.seconds //And this would be 10
How would I do this using Swift 2 OS X (not iOS)
Something like this:
let temp:Int = Int(time + 0.5) // Rounding
let mins:Int = temp / 60
let secs:Int = temp % 60
This would be a relatively simple solution. It's worth noting this would truncate partial seconds. You'd have to use floating point math on the third line and call trunc()/ceil()/floor() on the result before conversion to an Int if you wanted control over that.
let time:Double = 70.149 //This can be any value
let mins:Int = Int(time) / 60 //In this case it would be 1
let secs:Int = Int(time - Double(mins * 60)) //And this would be 10
Swift 5
The date components formatter has lots of advantages when displaying human readable text to a user. This function will take a double.
func humanReadable(time:Double) -> String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .full
if let str = formatter.string(from: time) {
return str
}
return "" // empty string if number fails
}
// will output '1 minute, 10 seconds' for 70.149

How to set time to a variable without the date

I am trying to set a specific time to two variables but can't seem to correctly format the syntax. I want to be able to set Shift 1 and Shift 2 off of certain times indicated below.
I want to only be able to use these times in an IF statement so that a radio button can be checked by default. Day Shift button and a Night Shift button. If the current time is in between shift 1, then day shift button is checked.
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Thanks Any help is appreciated.
You do not need to compare Date objects for your use,
just compare the hours as integers to the integer from now.getHours():
var now= new Date().getHours();
if(now>6 && now<19){
//check day shift button;
}
// else check niteshift button
You may try this:
http://jsfiddle.net/apq59j9u/
Date.prototype.currentTime = function(){
return ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ this.getMinutes()+((this.getHours()>12)?('PM'):'AM'); };
var d1= new Date();
var d2 = new Date();
d1.setHours(7);
d1.setMinutes(10);
d2.setHours(19);
d2.setMinutes(10);
alert(d1.currentTime());
alert(d2.currentTime());
Getting the Data
There are a number of different ways to do this. First here is a way to get the data from the date object:
var d = new Date();
var n = d.toTimeString();
Unfortunately, this will output something like "12:30:30 GMT-0500 (Eastern Standard Time)"
You can also try the getHours() and getMinutes functions to get the hours and minutes in your current timezone.
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
Setting the Data
This is pretty easy to do, just as getting the data from a date object. Use the following code to set the time to what you would like. Replace the numbers where you see 11 to conform to your needs.
NOTE: This time is in military style hours! 1 = 1am, 13 = 1pm, ect.
var d = new Date();
d.setHours(11);
d.setMinutes(11);
d.setSeconds(11);
Result: 11:11:11

Swift: Repeat action after a random period of time

Previously, with Objective-C I could use performSelector: in order to repeat an action after a random period of time which could vary between 1-3 seconds. But since I'm not able to use performSelector: in Swift, I've tried using "NSTimer.scheduledTimerWithTimeInterval". And it works in order to repeat the action. But there is a problem. On set the time variable to call a function that will generate a random number. But it seems that NSTimer uses the same number every time it repeats the action.
What that means is that the action is not performed randomly, but instead, after a set period of time that is generated randomly at the beginning of the game and it is used during all the game.
The question is: Is there any way to set the NSTimer to create a random number every time it executes the action? Or should I use a different method? Thanks!
#LearnCocos2D is right... use SKActions or the update method in your scene. Here is a basic example of using update to repeat an action after a random period of time.
class YourScene:SKScene {
// Time of last update(currentTime:) call
var lastUpdateTime = NSTimeInterval(0)
// Seconds elapsed since last action
var timeSinceLastAction = NSTimeInterval(0)
// Seconds before performing next action. Choose a default value
var timeUntilNextAction = NSTimeInterval(4)
override func update(currentTime: NSTimeInterval) {
let delta = currentTime - lastUpdateTime
lastUpdateTime = currentTime
timeSinceLastAction += delta
if timeSinceLastAction >= timeUntilNextAction {
// perform your action
// reset
timeSinceLastAction = NSTimeInterval(0)
// Randomize seconds until next action
timeUntilNextAction = CDouble(arc4random_uniform(6))
}
}
}
use let wait = SKAction.waitForDuration(sec, withRange: dur) in your code. SKAction.waitForDuration with withRange parameter will compute random time interval with average time = sec and possible range = dur
Generate a random time yourself and use dispatch_after to do the action.
For more information on dispatch_after, see here. Basically you can use this instead of performSelector

how to find time difference in titanium

I am new to titanium.
I want to find time difference in titanium. Ex 12.00 AM- 12.00 PM should give me 12 hours.
But I'm not able to get how to find it in titanium.
I'm trying
function calculatetime(chkintime,chkouttime)
{
var difference = chkintime - chkouttime;
Ti.API.info(':'+difference);
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
Ti.API.info(':'+hoursDifference);
Ti.API.info(':'+minutesDifference);
var time=hoursDifference+':'+minutesDifference;
return time;
}
It sometimes gives correct answer while sometimes negative values.
here chkintime and chkouttime values are in miliseconds e.g. 1355495784321
It's no different from finding a time difference in JavaScript. (In fact, it is finding a time difference in JavaScript.)
Check time difference in Javascript
Past that, a nice way to calculate the number of days between X and Y is to find out the MS difference, then add that time to a set date, like January 1st, 2000. Then you can really easily pull the number of years, months, days, hours, minutes, and seconds. There will be some inaccuracy caused by leap years, but if you're dealing with small period, it doesn't matter at all.
var start = new Date('February, 22, 2011 2:00 PM');
var end = new Date('February, 22, 2011 4:00 PM');
var ms = end - start;
var niceDate = new Date(new Date('January 1, 2000').getTime() + ms);
var years = niceDate.getFullYear() - 2000;
var months = niceDate.getMonth();
var days = niceDate.getDate();
var hours = niceDate.getHours();
var minutes = niceDate.getMinutes();
var seconds = niceDate.getSeconds();
alert(years + ' years,\n'
+ months + ' months,\n'
+ days + ' days,\n'
+ hours + ' hours,\n'
+ minutes + ' min,\n'
+ seconds + ' sec');

Resources