- Added log instrumentation to devexe
- Added a hill climbing option to optimise - Added hrpt files SKIPPED: psi/libpsi.a psi/sinqhmdriv.c psi/tabledrive.c
This commit is contained in:
278
optimise.c
278
optimise.c
@ -22,6 +22,7 @@
|
||||
#include "scan.i"
|
||||
#include "fitcenter.h"
|
||||
#include "optimise.h"
|
||||
#include "stdscan.h"
|
||||
|
||||
#ifdef CYGNUS
|
||||
#define MAXFLOAT 9999999.99
|
||||
@ -83,7 +84,6 @@
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
pOptimise CreateOptimiser(pCounter pCount)
|
||||
@ -466,6 +466,197 @@
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*------------------------------------------------------------------------
|
||||
* We use the scan object here for counting. The reason is that this
|
||||
* handles well in the common case of a single counter but also has
|
||||
* provisions for other counting methods through the scan modules
|
||||
* scripting mechanism
|
||||
* ------------------------------------------------------------------------*/
|
||||
static long ClimbCount(pOptimise self, SConnection *pCon)
|
||||
{
|
||||
int status;
|
||||
long data[1];
|
||||
int (*CollectFunc)(pScanData self, int iPoint) = NULL;
|
||||
|
||||
SilentPrepare(self->pScanner);
|
||||
|
||||
status = self->pScanner->ScanCount(self->pScanner,0);
|
||||
if(status != 1)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
if(self->pScanner->CollectScanData == CollectScanData)
|
||||
{
|
||||
CollectFunc = self->pScanner->CollectScanData;
|
||||
self->pScanner->CollectScanData = CollectSilent;
|
||||
}
|
||||
status = self->pScanner->CollectScanData(self->pScanner,0);
|
||||
if(CollectFunc != NULL)
|
||||
{
|
||||
self->pScanner->CollectScanData = CollectFunc;
|
||||
}
|
||||
if(status != 1)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
GetScanCounts(self->pScanner,data,1);
|
||||
return data[0];
|
||||
}
|
||||
/*------------------------------------------------------------------------*/
|
||||
static int ClimbDrive(SConnection *pCon,char *name, float value)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = Start2Run(pCon,pServ->pSics,name,value);
|
||||
if(status != 1)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
status = Wait4Success(GetExecutor());
|
||||
if(status != DEVDONE)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
static int ClimbVariable(pOptimise self, SConnection *pCon, int i)
|
||||
{
|
||||
pOVarEntry pOvar;
|
||||
void *pData;
|
||||
int status, direction = 1;
|
||||
long oneCount, twoCount, lastCount, currentCount;
|
||||
float varValue, startValue;
|
||||
char buffer[256];
|
||||
int (*CollectFunc)(pScanData self, int iPoint) = NULL;
|
||||
|
||||
assert(self);
|
||||
assert( (i >= 0) && (i < self->iVar));
|
||||
assert(pCon);
|
||||
|
||||
/* get variable data */
|
||||
DynarGet(self->pVariables,i,&pData);
|
||||
pOvar = (pOVarEntry)pData;
|
||||
startValue = pOvar->fCenter;
|
||||
|
||||
/*
|
||||
* prepare scan object
|
||||
*/
|
||||
self->pScanner->pCon = pCon;
|
||||
self->pScanner->pSics = pServ->pSics;
|
||||
self->pScanner->iNP = 1;
|
||||
self->pScanner->iMode = self->eCount;
|
||||
self->pScanner->fPreset = self->fPreset;
|
||||
|
||||
/*
|
||||
* test for upwards direction
|
||||
*/
|
||||
varValue = pOvar->fCenter + pOvar->fStep;
|
||||
status = ClimbDrive(pCon,pOvar->pName,varValue);
|
||||
if(!status)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
oneCount = ClimbCount(self,pCon);
|
||||
if(oneCount < 0)
|
||||
{
|
||||
return SCANERROR;
|
||||
}
|
||||
if(SCGetInterrupt(pCon) != eContinue)
|
||||
{
|
||||
return SCANABORT;
|
||||
}
|
||||
varValue = pOvar->fCenter - pOvar->fStep;
|
||||
status = ClimbDrive(pCon,pOvar->pName,varValue);
|
||||
if(!status)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
twoCount = ClimbCount(self,pCon);
|
||||
if(SCGetInterrupt(pCon) != eContinue)
|
||||
{
|
||||
return SCANABORT;
|
||||
}
|
||||
if(twoCount < 0)
|
||||
{
|
||||
return SCANERROR;
|
||||
}
|
||||
if(oneCount > twoCount)
|
||||
{
|
||||
direction = 1;
|
||||
lastCount = oneCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
direction = -1;
|
||||
lastCount = twoCount;
|
||||
}
|
||||
|
||||
/*
|
||||
* drive to the last best position
|
||||
*/
|
||||
varValue = pOvar->fCenter + direction*pOvar->fStep;
|
||||
status = ClimbDrive(pCon,pOvar->pName,varValue);
|
||||
if(!status)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
currentCount = lastCount;
|
||||
|
||||
/*
|
||||
* climb upwards as long as possible
|
||||
*/
|
||||
while(1)
|
||||
{
|
||||
pOvar->fCenter = varValue;
|
||||
varValue = pOvar->fCenter + direction * pOvar->fStep;
|
||||
status = ClimbDrive(pCon,pOvar->pName,varValue);
|
||||
if(!status)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
if(SCGetInterrupt(pCon) != eContinue)
|
||||
{
|
||||
return SCANABORT;
|
||||
}
|
||||
currentCount = ClimbCount(self,pCon);
|
||||
if(currentCount < 0)
|
||||
{
|
||||
return SCANERROR;
|
||||
}
|
||||
if(SCGetInterrupt(pCon) != eContinue)
|
||||
{
|
||||
return SCANABORT;
|
||||
}
|
||||
snprintf(buffer,255,"Climbing %s, value = %f, count = %ld",
|
||||
pOvar->pName, varValue, currentCount);
|
||||
SCWrite(pCon,buffer,eWarning);
|
||||
|
||||
if(currentCount <= lastCount)
|
||||
{
|
||||
/*
|
||||
* we are finished. Drive to previous position and
|
||||
* break
|
||||
*/
|
||||
status = ClimbDrive(pCon,pOvar->pName,pOvar->fCenter);
|
||||
if(!status)
|
||||
{
|
||||
return DRIVEERROR;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* go on, we are not over the top yet
|
||||
*/
|
||||
lastCount = currentCount;
|
||||
}
|
||||
}
|
||||
pOvar->fShift = ABS(startValue - pOvar->fCenter);
|
||||
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int CheckSuccess(pOptimise self)
|
||||
{
|
||||
@ -530,6 +721,45 @@
|
||||
}
|
||||
return MAXCYCLE;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int OptimiserClimb(pOptimise self, SConnection *pCon)
|
||||
{
|
||||
int i, iRet, iCycle, iRedoVar = 0;
|
||||
char pBueffel[256];
|
||||
|
||||
assert(self);
|
||||
|
||||
if(self->iVar < 1)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: Nothing to optimise",eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
iRet = OptimiserInit(self,pCon);
|
||||
if(!iRet)
|
||||
{
|
||||
return iRet;
|
||||
}
|
||||
for(iCycle = 0; iCycle < self->iMaxCycles; iCycle++)
|
||||
{
|
||||
sprintf(pBueffel,"Optimiser cycle %d of %d started",iCycle, self->iMaxCycles);
|
||||
SCWrite(pCon,pBueffel,eStatus);
|
||||
for(i = iRedoVar; i < self->iVar; i++)
|
||||
{
|
||||
iRet = ClimbVariable(self,pCon,i);
|
||||
if(iRet <= 0)
|
||||
{
|
||||
return iRet;
|
||||
}
|
||||
}
|
||||
iRet = CheckSuccess(self);
|
||||
if(iRet)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return MAXCYCLE;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
int MakeOptimiser(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
@ -717,6 +947,52 @@
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*-------- climb */
|
||||
else if(strcmp(argv[1],"climb") == 0)
|
||||
{
|
||||
/* check rights */
|
||||
if(!SCMatchRights(pCon,usUser))
|
||||
{
|
||||
SCWrite(pCon,"ERROR: You are not aurhorised to do this!",eError);
|
||||
return 0;
|
||||
}
|
||||
iRet = OptimiserClimb(self,pCon);
|
||||
switch(iRet)
|
||||
{
|
||||
case PEAKLOST:
|
||||
SCWrite(pCon,"ERROR: lost the peak, sorry!",eError);
|
||||
return 0;
|
||||
break;
|
||||
case MAXCYCLE:
|
||||
sprintf(pBueffel,"ERROR: could not optimise peak in %d cycles",
|
||||
self->iMaxCycles);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
break;
|
||||
case SCANERROR:
|
||||
SCWrite(pCon,"ERROR: failed to scan the peak",eError);
|
||||
return 0;
|
||||
break;
|
||||
case SCANABORT:
|
||||
SCWrite(pCon,"ERROR: Scan was aborted, Optimiser follows",eError);
|
||||
return 0;
|
||||
break;
|
||||
case DRIVEERROR:
|
||||
SCWrite(pCon,"ERROR: Failure to drive variable to new position",eError);
|
||||
return 0;
|
||||
break;
|
||||
case 1:
|
||||
SCWrite(pCon,"At long last, I finished optimising the peak",eValue);
|
||||
return 1;
|
||||
break;
|
||||
default:
|
||||
SCWrite(pCon,"ERROR: Unidentified error krept into Optimiser",eError);
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
SCWrite(pCon,"Optimiser climbed successfully",eValue);
|
||||
return 1;
|
||||
}
|
||||
/* ------ count mode */
|
||||
if(strcmp(argv[1],"countmode") == 0)
|
||||
{
|
||||
|
Reference in New Issue
Block a user