The error that i was getting is that, all agents become idle at some point.
however if i eliminate the attribute that checks for the idleness of agents, i get a little more off the desired output.
I got the problem from UVA
http://uva.onlinejud...lem&problem=763
package queuea;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
class RequestSet{
private int id;
private int qty;
private int firstReq;
private int timeReq;
private int nextReq;
private int sucReq;
private int initialized;
public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
this.id=id;
this.qty=qty;
this.firstReq=firstReq;
this.timeReq=timeReq;
this.nextReq=nextReq;
sucReq=nextReq;
initialized=0;
}
public void decReq(){//Decreases the time attributes
if(firstReq!=0)
firstReq-=1;
if(nextReq!=0 && firstReq==0)
nextReq-=1;
}
public int getInitial(){//Determines if the Request set has already enqueued its first element
return initialized;
}
public void setInitial(){
initialized=1;
}
public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
qty--;
}
public void restoreReq(){//Replenishes the time remaining until the next Request
nextReq=sucReq;
}
public int getID(){
return id;
}
public int getQty(){
return qty;
}
public int getFReq(){
return firstReq;
}
public int getTimeReq(){
return timeReq;
}
public int getNReq(){
return nextReq;
}
}//End of Class "Request Set"
class Request{
private int topicID;
private int minsNeeded;
public Request(int id, int mins){
topicID = id;
minsNeeded = mins;
}
public void Process(){
minsNeeded-=1;
}
public int getMins(){
return minsNeeded;
}
public int getId(){
return topicID;
}
}//End of Class "Request"
class Server{
private int id;
private int topicsQty;
private ArrayList<Integer> specialty;
private int available;
private int recentJobAgo;
private int occTil;
private int listStanding;
public Server(int id, int qty, int stand){
this.id=id;
topicsQty=qty;
available=1;
specialty = new ArrayList();
occTil=0;
listStanding=stand;
}
public Server(){
}
public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
return listStanding;
}
public int getRecJ(){//Return the time elapsed after the most recent job
return recentJobAgo;
}
public void display(){
int i;
for(i=0;i<topicsQty;i++){
System.out.printf("%d ",specialty.get(i));
}
}
public void progress(){
if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
recentJobAgo+=1;
else if(available==0 && occTil==0){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
available=1;
}
else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
occTil--;
}
public int isAvailable(){
return available;
}
public void occupy(int time){//Agent begins servicing a request
available=0;
recentJobAgo=0;
occTil=time;
}
public void doneReq(){
available=1;
}
public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
int i;
specialty.add(topicID);
}
public int canDo(int id){//returns 1 if the topicID is in the agent's ArrayList of topic names
int i, result=0;
for(i=0;i<topicsQty;i++){
if(id==specialty.get(i) && available==1){
result=1;
}
}
return result;
}
}//End of Class "Server"
int topics, servers, i, minutes=0, result=0, check=0;
Scanner sc = new Scanner(System.in);
int topicID, noReqs, bFirst, timeReq, betReqs, set=0;
//topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between reqs
int agentID, lrndTopics, j, aTopic;
//agentID, # of priority topics
RequestSet[] requests;
Server[] agent;
Server temp = new Server();
Queue<Request> spool = new LinkedList<Request>();
topics=sc.nextInt();
requests = new RequestSet[topics];
for(i=0; i<topics; i++)
{
topicID=sc.nextInt();
noReqs=sc.nextInt();
bFirst=sc.nextInt();
timeReq=sc.nextInt();
betReqs=sc.nextInt();
requests[i] = new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs);
}//Gets inputs for the request sets
servers=sc.nextInt();
agent = new Server[servers];
for(i=0; i<servers; i++)
{
agentID=sc.nextInt();
lrndTopics=sc.nextInt();
agent[i] = new Server(agentID, lrndTopics,i);
for(j=0;j<lrndTopics; j++)
{
aTopic=sc.nextInt();
agent[i].learnTopics(aTopic);
}
}//Gets inputs for the agent set
//Request processing section
do{
if(set==1)
minutes++;
System.out.println("minute: "+minutes);
for(i=0;i<topics;i++)
{
if(requests[i].getFReq()==0 && requests[i].getInitial()==0)
{
spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
requests[i].decQty();
requests[i].restoreReq();
requests[i].setInitial();
System.out.println("Request["+i+"] added at ["+minutes+"]");
}
else if(requests[i].getInitial()==1 && requests[i].getNReq()==0 && requests[i].getQty()>0)
{
spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
requests[i].decQty();
requests[i].restoreReq();
System.out.println("Request["+i+"] added at ["+minutes+"]");
}
else
{
requests[i].decReq();
}
}
//Sorts the agents according to their idle time. Most idle places first
for(i=1;i< servers;i++)
{
for(j=0;j< servers-1;j++)
if(agent[j].getRecJ()<agent[j+1].getRecJ())
{
temp=agent[j];
agent[j]=agent[j+1];
agent[j+1]=temp;
}
}
//Swaps agents when they have equal idle time; however, the agent that was inputted first places first
for(i=0;i< servers-1;i++)
{
if(agent[i].getRecJ()==agent[i+1].getRecJ()){
if(agent[i].getPosition()>agent[i+1].getPosition()){
temp=agent[i];
agent[i]=agent[i+1];
agent[i+1]=temp;
}
}
}
//Decreases all the time attributes
for(i=0;i<servers;i++)
{
agent[i].progress();
}
//Checks all agents if they're qualified to service a topic
for(i=0;i<servers;i++)
{
if(spool.peek()!=null)
{
if(agent[i].canDo(spool.peek().getId())==1)
{
agent[i].occupy(spool.peek().getMins());
spool.remove();
}
}
}
//minutes++;
set=1;
check=0;
result=0;
//Checks all the requests sets' number of remaining topics
for(j=0;j<topics;j++){
if(requests[j].getQty()!=0)
check=1;
}
//Checks if all the agents are occupied
for(i=0;i<servers;i++)
{
if(agent[i].isAvailable()==0)
result=1;
}
}while(check==1 && result==1);
System.out.println("Minutes: "+minutes+" "+check+" "+result);
}
}

New Topic/Question
Reply


MultiQuote






|